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

This code displays all applications available but I would like to work on the result. On every line you've got com.android.* and it's that part I would like to cut.

The problem is when I tried to use substring(10) for example it doesn't change the result.

i tried that way and with Log i success the substring but when i display it on the screen it just show me another layout with all his buttons i almost get it with this code i display with Log exactly what i want to put on screens but there is just a black board while i got the right result with the Log i can't see the reason

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);
Sharp
  • 121
  • 1
  • 2
  • 16
  • 1
    What were you calling substring(10) *on*? I don't see any string manipulation code in the sample you've provided. – Ian McLaird Mar 11 '13 at 15:56

2 Answers2

2

You have the right idea to use substring. Here's how I would go about doing it:

// 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 ]
}

The value of trimLength evaluates to 12, so I'm not sure how you got 10, but this should work.

wchargin
  • 15,589
  • 12
  • 71
  • 110
  • i got errors on String package = info.activityName.packageName; // Now, trim it with substring and the trim length. String trimmed = package.substring(trimLength); – Sharp Mar 12 '13 at 09:24
  • @Bob: Could you please update the original question (click *Edit*) and paste the stack trace of the error? – wchargin Mar 12 '13 at 14:35
0

thank you WChargin with your help i did this there are small differences but i don't know their deep diffrence anyway that code runs well

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



    }

the problem with that solution is that i've got a string at the end and of course i can manage to create an array of all strings and give it to an adapter but there were other operation i couldn't make anymore because it were using an arraylist of ResolveInfoand not an arraylist of string add your own listener to a list https://stackoverflow.com/questions/15383453/dealing-with-adapter-listview-and-listener

Community
  • 1
  • 1
Sharp
  • 121
  • 1
  • 2
  • 16