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)
时,它不会改变结果。
我尝试了这种方式,并且通过 Log 我成功获得了子字符串,但是当我将其显示在屏幕上时,它只是向我显示了带有所有按钮的另一个布局 我几乎用这段代码得到了它,我用日志显示了我想要在屏幕上显示的内容,但是只有一 block 黑板,而我用日志得到了正确的结果 我看不出原因
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);
最佳答案
您使用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 的,但这应该可行。
关于java - 对字符串进行删除前缀的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15338576/