Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ArrayAdapter to Listview - Android

I have a class that extends ArrayAdapter . I can not set the ListAdapter to my ListView because I don't know what the code should be when I try to create the ListAdapter. Here is my current code.

class Item {
                String username;
                String number;
                String content;

            }


             class ListAdapter extends ArrayAdapter<Item> {

                public ListAdapter(Context context, int textViewResourceId) {
                    super(context, textViewResourceId);
                    // TODO Auto-generated constructor stub
                }

                private List<Item> items;

                public ListAdapter(Context context, int resource, List<Item> items) {

                    super(context, resource, items);

                    this.items = items;

                }

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

                    View v = convertView;

                    if (v == null) {

                        LayoutInflater vi;
                        vi = LayoutInflater.from(getContext());
                        v = vi.inflate(R.layout.list_item, null);

                    }

                    Item p = items.get(position);

                    if (p != null) {

                        TextView tt = (TextView) v.findViewById(R.id.id);
                        TextView tt1 = (TextView) v.findViewById(R.id.categoryId);
                        TextView tt3 = (TextView) v.findViewById(R.id.description);

                        if (tt != null) {
                            tt.setText(p.getId());
                        }
                        if (tt1 != null) {

                            tt1.setText(p.getCategory().getId());
                        }
                        if (tt3 != null) {

                            tt3.setText(p.getDescription());
                        }
                    }

                    return v;

                }
             }

Here is where I get the problem, trying to declare the ListAdapter

List <Item> tempList = new ArrayList <Item>();

                ListView yourListView = (ListView) findViewById(android.R.id.list);

                // Here is the problem
                ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item, List<tempList> //the error occurs right here);

                yourListView .setAdapter(customAdapter);

1 Answers

Change

ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item,List<tempList>)

to

ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item, tempList); 
//...........

REmove list just add tempList

like image 170
sanky jain Avatar answered May 05 '26 03:05

sanky jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!