对字符串进行操作以删除前缀

时间:2013-03-11 12:28:32

标签: java android string substring

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 i成功了子串但是当我在屏幕上显示它时它只显示我的所有按钮的另一个布局 我几乎得到了这个代码我用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);

2 个答案:

答案 0 :(得分: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的,但这应该有效。

答案 1 :(得分: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的arraylist而不是字符串的arraylist add your own listener to a list https://stackoverflow.com/questions/15383453/dealing-with-adapter-listview-and-listener