简体   繁体   中英

android studio how to print list of installed apps into a listView

I've found some code on the internet to obtain details of currently installed apps, I'm not sure how to implement this into a list. I've created the list in the xml file but I'm not too sure how to implement the data into the listView. The code to obtain the app data is -

     final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  final List pkgAppsList = getPackageManager().queryIntentActivities(      mainIntent, 0);
  for (Object object : pkgAppsList) 
  {
ResolveInfo info = (ResolveInfo) object;
Drawable icon    = getBaseContext().getPackageManager().getApplicationIcon(info.activityInfo.applicationInfo);
String strAppName   = info.activityInfo.applicationInfo.publicSourceDir.toString();
String strPackageName  = info.activityInfo.applicationInfo.packageName.toString();
final String title  = (String)((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");

}

You can first create a list of String like this:

    final ArrayList<String> list = new ArrayList<String>();

then inside your code, add strAppName into the list by list.add(strAppName);

and make an array adapter and set it to your listview:

    final ListView listview = (ListView) findViewById(R.id.listview);
    final ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM