Question

I am using an ArrayAdapter for my listview, and there is something I do not really understand. Here is an example of my code

      class ListAdapter extends ArrayAdapter<Item> {

            public ListAdapter(Context context, ArrayList<Item> items) {
                super(context, R.layout.list_item, 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);
                }
                ...some code here....
            }
       }

and my question is: why do I need to inflate layout in "getView" if I already set its Id in the constructor in "super" call.? After I set the layout-Id via "super", is this Id stored somewhere in ArrayAdapter? So I do not understand what this "resource id" in super call for..

Thanks for explaining :) Best Regards

Was it helpful?

Solution

You need to override getView when you want to display in your listview something of different from just a text. Example when you want to want to show an image with a text (You know Whatsapp? It's a listview).

If you don't need it, maybe you don't need to override ArrayAdapter at all. Just use the class as it is and stop. You can use any object you want, ArrayAdapter will use toString() of the class passed to show the text.

ArrayAdapter<Items> adapter = new ArrayAdapter<Items> ();
listView.setAdapter (adapter);

use Items.toString() to say what you want to show as text.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top