-2

I've created a listview which shows all currently installed apps on the android device. The code also retrieves the icon of the app, but I cannot display it within the list because it can only display strings. How can I alter my code so that it also displays the app icon next to the name?

additionally after I do this I also want to add a checkbox next to each app title and icon

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


    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) : "???");
        list.add(title);
    }

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

thanks

user3343264
  • 71
  • 1
  • 13
  • you cannot convert normal list-view into custom list..You must create separate layout & Java class. for more info:http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CB0QFjAA&url=http%3A%2F%2Fwww.androidhive.info%2F2012%2F02%2Fandroid-custom-listview-with-image-and-text%2F&ei=jjAQVdmyDdSKuASAoYKIAg&usg=AFQjCNHsk11ptgks53a2ymPMCZRb-AA2Zw&bvm=bv.88528373,d.c2E – Ranjithkumar Mar 23 '15 at 15:27

1 Answers1

0

You need 3 things:

  1. a single row layout
  2. a class to store your App-Name & Icon
  3. a custom list adapter

custom_row_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/nav_item_bg"
android:clickable="true"
android:orientation="horizontal">

<ImageView
    android:id="@+id/icon"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="16dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="16dp"
    android:textSize="16sp" />

</LinearLayout>

AppDetails.java

public class AppDetails {
    String name;
    Drawable icon;

    public AppDetails(String name, Drawable icon) {
        this.name = name;
        this.icon = icon;
    }
}

AppDetailsAdapter.java

public class AppDetailsAdapter extends BaseAdapter {

private ArrayList<AppDetails> data;
private Context context;

public AppDetailsAdapter(Context context, List<AppDetails> data){
    this.context = context;
    this.data = data;
}

@Override
public int getCount(){
    return data.size();
}

@Override
public Object getItem(int position){
    return data.get(position);
}

@Override
public long getItemId(int position){
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View view = convertView;

    if(view == null){
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.custom_row_layout, null);
    }

    ImageView icon = (ImageView) view.findViewById(R.id.icon);
    TextView text = (TextView) view.findViewById(R.id.text);

    final AppDetails item = data.get(position);
    text.setText(item.name); 
    icon.setImageDrawable(item.icon);

    return view;
}

To implement this to your Code you should do something like:

List<AppDetails> appList = new ArrayList<>();
for (Object object : pkgAppsList)
{
    //This is your foreach loop over all apps
    //.. Add these 2 line at the end of your loop
    AppDetails tmp = new AppDetails(strAppName, icon);
    appList.add(tmp)
}

And instead of using an exsistig ArrayAdapter you should use the one i created above by:

ListView listview = (ListView) findViewById(R.id.listView);
AppDetailsAdapter adapter = new AppDetailsAdapter(this, appList);
listview.setAdapter(adapter);
HeW
  • 448
  • 2
  • 12
  • I've been trying to understand this but I can't see how to apply it to my code, I use a for loop to retrieve the app name and app icon, where would I place this loop in your code? Where you comment apply text? I'm not sure what you mean by "and item from the "item" object – user3343264 Mar 23 '15 at 17:52
  • @user3343264 Ive edited my code above. I try'd to explaine how to include in your existing code. – HeW Mar 23 '15 at 18:02
  • getting an error on public DeviceItem in app details, return type is required, not too sure how to fix – user3343264 Mar 23 '15 at 18:33
  • Oh sorry .. Just rename DeviceItem to AppDetails. That should to the trick – HeW Mar 23 '15 at 18:48
  • oh ok yeah. I'm just confused about where to place the code you wrote, the segment which starts with List appList = new ArrayList<>(); should I place that within my loop where it retrieves the app details? and the last 2 chunks of code you wrote I don't know where to place them. I'm sorry about my ignorance on this – user3343264 Mar 23 '15 at 18:54
  • No you have to Initialize the appList outside your Loop. Replace the final AppDetailsAdapter adapter =... Line of code with your "final ArrayAdapter adapter =...". The last two lines of code come inside the Adapter Class where I wrote the comment. – HeW Mar 23 '15 at 18:59
  • ok. I have 2 errors, AppDetails tmp = new AppDetails(); because it expects string and drawable in the brackets and after final AppDetailsAdapter in the brackets too, "...in apppdetailsadapter cannot be applied to java.util.list – user3343264 Mar 23 '15 at 19:13
  • Ok the first error: replace AppDetails tmp = new AppDetails(); with AppDetails tmp = new AppDetails(strAppName, icon); and delete the two lines tmp.name = strAppName and tmp.icon = icon. And a few more details for the second error pls. It seems you dont have many experience in android/java/oop. Maybe we should start a chat to solve your issues – HeW Mar 23 '15 at 19:19
  • ok so (this, appList); is underlined in the final AppDetailsAdapter, the message states "AppDetailsAdapter (Context, java.util.arraylist cannot be applied to (MainActivity, java.util.list) And yes this is my first project that isn't simple hello worlds or anything. I apparently don't have enough points to start a chat sadly – user3343264 Mar 23 '15 at 19:25
  • Oh i see it. Inside this line: "public AppDetailsAdapter(Context context, ArrayList data){..." delete the "Array" before List -> "public AppDetailsAdapter(Context context, List data){.." – HeW Mar 23 '15 at 19:28
  • thank you so much, just need to try and get my head around how exactly it all works now so I can add buttons next to each item, you're a life saver – user3343264 Mar 23 '15 at 19:44