0

I am having so much trouble with a block of code. I am searching for a listview with the list of installed applications name list. I have searched a lot. Later I have found some code. I don't know what is happening here. It just crush... Please don't laugh at my code.

listView = (ListView)findViewById(R.id.listView);
    PackageManager packageManager = this.getPackageManager();
    List<ApplicationInfo> appList = packageManager.getInstalledApplications(0);
    List<String> myStrinrgArray = new ArrayList<String>();
    Iterator<ApplicationInfo> it = appList.iterator();
    while (it.hasNext()){
        ApplicationInfo applicationInfo = (ApplicationInfo)it.next();
        String appName = packageManager.getApplicationLabel(applicationInfo).toString();
        myStrinrgArray.add(appName);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, myStrinrgArray);
        listView.setAdapter(arrayAdapter);
    }

Please help anyone if possible for you... I'll be glad at that.

NOTE: I have launched it in the emulator

2 Answers2

0

You probably should look into what is happening in the code you are writing. But there is one "obvious" bug - you are reseting the ArrayAdapter for each item, which will not work.

Try this change in the loop:

while (it.hasNext()){
    ApplicationInfo applicationInfo = (ApplicationInfo)it.next();
    String appName = packageManager.getApplicationLabel(applicationInfo).toString();
    myStrinrgArray.add(appName);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, myStrinrgArray);
listView.setAdapter(arrayAdapter);
Jim
  • 10,172
  • 1
  • 27
  • 36
  • Error... LogCat link : (https://www.dropbox.com/s/ouyn0ufvxut4lmu/LogCat2.txt?dl=0) – user5281686 Sep 04 '15 at 13:44
  • that means "simple_list_item_2" is not a `TextView` - see this: http://stackoverflow.com/questions/9280965/arrayadapter-requires-the-resource-id-to-be-a-textview-xml-problems – Jim Sep 04 '15 at 20:08
0

this one should work:

listView = (ListView)findViewById(R.id.listView);
PackageManager packageManager = this.getPackageManager();
List<ApplicationInfo> appList = packageManager.getInstalledApplications(0);
List<String> myStrinrgArray = new ArrayList<>();

for (ApplicationInfo applicationInfo : appList) {
    String appName = packageManager.getApplicationLabel(applicationInfo).toString();
    myStrinrgArray.add(appName);
}

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
            myStrinrgArray);
listView.setAdapter(arrayAdapter);

Note that you must use simple_list_item_1 for single-line-listitems. And of course create the arrayAdapter after the loop! (I replaced while-loop and iterator with a easier for-loop)

  • Yes yes yes... It works. With android.R.layout.simple_list_item_1. Thanks brother. – user5281686 Sep 04 '15 at 13:50
  • But can you tell me the difference between list_item_1 and list_item_2? – user5281686 Sep 04 '15 at 13:52
  • Ok man. Here i got it. http://stackoverflow.com/questions/11722885/what-is-difference-between-android-r-layout-simple-list-item-1-and-android-r-lay – user5281686 Sep 04 '15 at 13:57
  • yeah simple_list_item_2 is for 2 lines. It doesn't work (as far as I know) with ArrayAdapter (because arrays have only 1 dimension), but with a SimpleCursorAdapter. – Matthias Miro Sep 04 '15 at 13:58