Kacy
Kacy

Reputation: 3430

How do I add Views to a ListView?

Every example/tutorial I've come across just adds Strings to ListViews, but I want to add inflated Views. I've tested the inflated view and it looks fine if I add it to some ViewGroup like a RelativeLayout using addView(). But adding the inflated view to the ListView just displays it in the form of text:

enter image description here

ArrayList<View> friend_request_items = new ArrayList<View>();  //a list containing the items we want to add 

ListView friend_request_list = (ListView) findViewById( R.id.friend_request_list );

ArrayAdapter<View> friend_request_adapter = new ArrayAdapter<View>( this, android.R.layout.simple_list_item_1, friend_request_items);

friend_request_list.setAdapter( friend_request_adapter );

//friend_template is just my own xml file containing the view I'm inflating
View friend_request_item = LayoutInflater.from(this).inflate(R.layout.friend_template, null);

friend_request_items.add( friend_request_item ); 
friend_request_adapter.notifyDataSetChanged();

I think the problem is the layout I'm passing into the adapter, but I don't know what to change it to (or if this is even the solution).

Upvotes: 0

Views: 942

Answers (2)

mes
mes

Reputation: 3621

Here is a nice tutorial using a custom ArrayAdapter and custom xml layout Using lists in Android. You should subclass an ArrayAdapter and inflate a row inside a getView method, it is a common and efficient solution, and instead of passing a view as a generic parameter of a list, you should pass a data class, for Example ArrayAdapter<FriendRequest> or something like that

public class FriendRequestsAdapter extends ArrayAdapter<FriendRequest> {
private LayoutInflater inflater;

public FriendRequestsAdapter(Context context, List<FriendRequest> requests) {
    super(context, R.layout.friend_request_row, requests);
    this.inflater = ((Activity)context).getLayoutInflater();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = inflater.inflate(R.layout.friend_request_row, parent, false);
    }

    FriendRequest item = getItem(position);
    TextView textView = (TextView) convertView.findViewById(R.id.textView);

    return convertView;
}

}

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006674

Every example/tutorial I've come across just adds Strings to ListViews

There are other examples out there, though you are unlikely to find examples of what you are proposing.

I want to add inflated Views

A ListAdapter returns "inflated Views" from its getView() method. It is up to you to determine exactly how it does that.

Moreover, the point behind a ListView is to be able to recycle list row Views, as lists can be large and rows are expensive in terms of heap space. Your code appears to create all the row views up front, which is only suitable for small data sets.

But adding the inflated view to the ListView just displays it in the form of text

When ListView calls getView() on ArrayAdapter, ArrayAdapter gets the model for the desired row, calls toString() on it, and pours that into a TextView identified by your other constructor parameters (in this case, a TextView formatted as defined in android.R.id.simple_list_item_1).

I think the problem is the layout I'm passing into the adapter, but I don't know what to change it to (or if this is even the solution).

First, I strongly encourage you to reconsider your original mindset ("I want to add inflated Views"). Bear in mind that you have to deal with configuration changes (e.g., screen rotation, locale changes) and process termination, and your "inflated Views" go away in either case. A ListView and ListAdapter are designed to convert model data into appropriate visual representations, and do so in a memory-efficient fashion.

That being said, if you are utterly convinced in your overall approach, you will need to create a custom BaseAdapter or ArrayAdapter subclass, where in getView() you return the desired "inflated View" for a given position.

Upvotes: 1

Related Questions