2013-03-11 46 views
2
final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null); 
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ; 
final PackageManager pm = getApplicationContext().getPackageManager(); 
ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>) pm.queryIntentActivities(mainIntent, 0); 

//Drawable iconApp = resolveInfo.activityInfo.loadIcon(getPackageManager()); 

ApplicationAdapter adapter = new ApplicationAdapter(this, listP); 

adapter.addListener(this); 
ListView list = (ListView)findViewById(R.id.list); 

list.setAdapter(adapter); 

此代碼顯示所有可用的應用程序,但我想處理結果。在每一行你都有com.android.*,這是我想要削減的部分。對字符串進行操作以刪除前綴

問題是,當我試圖使用substring(10)例如它不會改變結果。

我試過的方式和登錄我成功的子字符串,但是當我在屏幕上顯示出來,它只是告訴我他所有的按鈕 另一個佈局,我幾乎與此代碼我用日誌顯示得到它正是我想要把在屏幕上但只是一個黑色的板,而我得到的日誌 正確的結果我看不到的原因

public void onCreatebis() { 

     setContentView(R.layout.main); 
     final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null); 
     mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ; 
     final PackageManager pm = getApplicationContext().getPackageManager(); 
     ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>) pm.queryIntentActivities(mainIntent, 0); 
     final int trimLength = "com.android.".length(); 
     ArrayList<String> maliste = new ArrayList(); 
     int size=listP.size(); 
     size=maliste.size(); 
     //String []maliste=new String[listP.size()]; 
     // Loop over each item. 
     for (ResolveInfo info : listP) { 
      // Get the (full, qualified) package name. 
      String packag = info.activityInfo.applicationInfo.packageName; 

      // Now, trim it with substring and the trim length. 
      String trimmed = packag.substring(trimLength); 
      for(int i=0;i<maliste.size();i++){ 
       maliste.set(i, trimmed); 
      } 
      Log.v("trimmed", trimmed); 

      // [ do whatever you want with the trimmed name ] 
     } 




     ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, maliste); 

     ListView list = (ListView)findViewById(R.id.list); 

     list.setAdapter(adapter2); 
+1

你叫什麼子字符串(10)*在*上?我沒有看到您提供的示例中的任何字符串操作代碼。 – 2013-03-11 15:56:24

回答

2

你必須使用substring正確的觀念。這是我怎麼會去這樣做:

// Get the length of text to trim. 
final int trimLength = "com.android.".length(); 

// Loop over each item. 
for (ResolveInfo info : listP) { 
    // Get the (full, qualified) package name. 
    String package = info.activityName.packageName; 

    // Now, trim it with substring and the trim length. 
    String trimmed = package.substring(trimLength); 

    // [ do whatever you want with the trimmed name ] 
} 

trimLength值計算結果爲12,所以我不知道你是怎麼10,但這應該工作。

+0

我在String package = info.activityName.packageName; //現在,用子字符串和修剪長度修剪它。 String trimmed = package.substring(trimLength); – Sharp 2013-03-12 09:24:58

+0

@Bob:你能否更新原始問題(點擊*編輯*)並粘貼錯誤的堆棧跟蹤? – wchargin 2013-03-12 14:35:43

0

謝謝WChargin在你們的幫助我這樣做也有小的差異,但我不知道他們的深diffrence反正代碼運行良好

public void onCreatebis() { 

     setContentView(R.layout.main); 
     final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null); 
     mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ; 
     final PackageManager pm = getApplicationContext().getPackageManager(); 
     ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>) pm.queryIntentActivities(mainIntent, 0); 
     final int trimLength = "com.android.".length(); 
     ArrayList<String> maliste = new ArrayList<String>(); 


     //String []maliste=new String[listP.size()]; 
     // Loop over each item. 
     for (ResolveInfo info : listP) { 
      // Get the (full, qualified) package name. 
      String packag = info.activityInfo.applicationInfo.packageName; 

      // Now, trim it with substring and the trim length. 
      String trimmed = packag.substring(trimLength); 
      maliste.add(trimmed); 



     } 

     ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, maliste); 

     ListView list = (ListView)findViewById(R.id.list); 

     list.setAdapter(adapter2); 



    } 

與解決的問題是,我有一個字符串在結束,當然我可以設法創建一個所有字符串的數組,並將其提供給適配器,但還有其他操作,我不能再做,因爲它使用了ResolveInfo的數組列表,而不是一個字符串的數組列表 add your own listener to a list https://stackoverflow.com/questions/15383453/dealing-with-adapter-listview-and-listener