Android - ListView and tag

I am using ArrayListAdapter for ListView objects. I am wondering if there is an easy way to store some extra data for each list item as a tag object.

+3
source share
2 answers

Usually you use a string array or list as data for a list of arrays, and the adapter method getItem(position)returns the corresponding string.

But you can use any array of objects or a list as input, and thus pass any data as an element of the list. For instance:

class MyListItem {

    private int mId;
    private Object mData;
    private String mListItemName;

    public MyListItem(int id, Object data, String name) {
        mId = id;
        mData = data;
        mListItemName = name;
    }

    @Override
    public String toString() {
        return mListItemName;
    }
}

You can pass the array to the array MyListItemadapter and use toString()to get the names for the elements. Item data can be obtained with (MyListItem) adapter.getItem(position).

+9

, . , onItemClick() . .

: SimpleAdapter. Unfiltered, onItemClick() ListView, , ListView. onItemClick , . getItemAtPosition(), , . getItemAtPosition() CursorAdapter.

+1

All Articles