2013-03-11 65 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

相关问题