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)例如它并没有改变结果时。

我尝试了这种方式,并且使用 Log 我成功了子字符串,但是当我在屏幕上显示它时,它只是向我显示了另一个带有他所有按钮的布局只是一块黑板,而我用日志得到了正确的结果,我看不出原因

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);
4

2 回答 2

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 的,但这应该可以。

于 2013-03-11T16:16:09.360 回答
0

谢谢你 WChargin 在你的帮助下我做到了,有一些小的差异,但我不知道它们的深度差异,代码运行良好

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 的数组列表而不是字符串 的数组列表将您自己的侦听器添加到列表 https://stackoverflow.com/questions/15383453/dealing-with-adapter-listview-and-listener

于 2013-03-12T11:04:52.070 回答