user10718906
user10718906

Reputation:

ListView won't let me set <List> as my list

I have a list:

  Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> pkgAppsList = getApplicationContext().getPackageManager().queryIntentActivities(mainIntent, 0);

I want to set this in my adapter. I have tried this:

adapter = new ArrayAdapter<List>(this, R.layout.listview_row_customizations, pkgAppsList) {

But I'm getting an error cannot resolve constructor array adapter...

How do I fix this?

Upvotes: 0

Views: 35

Answers (2)

Guy4444
Guy4444

Reputation: 1649

The List in new ArrayAdapter is not correct, instead write your object class type (ResolveInfo).

if you need custom layout (I guessed this because you are not using a string list but an object with a few parameters) - follow this instructions:

build your own custom ArrayAdapter:

    public class MyAdapter extends ArrayAdapter<ResolveInfo> {

    public MyAdapter(Context context, List<ResolveInfo> list) {
        super(context, 0, list);
    }

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

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
        }

        TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
        TextView tvPriority = (TextView) convertView.findViewById(R.id.tvPriority);

        tvName.setText(resolveInfo.resolvePackageName);
        tvPriority.setText("" + resolveInfo.priority);

        return convertView;
    }
}

create a layout named item.xml:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvPriority"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:text="TextView" />
</RelativeLayout>

In Activity, call it by:

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> pkgAppsList = getApplicationContext().getPackageManager().queryIntentActivities(mainIntent, 0);
        MyAdapter myAdapter = new MyAdapter(this, pkgAppsList);

Upvotes: 0

PushpikaWan
PushpikaWan

Reputation: 2545

try this

adapter = new ArrayAdapter<ResolveInfo>(this, R.layout.listview_row_customizations, pkgAppsList)

Upvotes: 1

Related Questions