Skip to content Skip to sidebar Skip to footer

Setting Unique Id To Listview Android

I have a ListView which shows person information from database. Is that possible to add a particular ID like personid to each item in listview, so that I can use that ID to be pas

Solution 1:

When you use SimpleAdapter, you provide it with an ArrayList of Maps. What you should do is include your personid as an entry in your Map, you don't have to bind it to any display element in your row, so it does not have to be displayed.

For example if your map is called rowMap, and the id is called personId

rowMap.put("person_id", personId);
adapterList.add(rowMap);

Then when your item is selected, you retrieve the Map using the position that is passed to your listener. Once you have that, just call

map = adapterList.get(pos);selectedId = map.get("person_id");

Updated with a little more info on the SimpleAdapter

Here is a little more info on the SimpleAdapter, below is the doc on the constructor for reference.

The idea is that you create an ArrayList of Maps, each map entry has to have at least the data you want to display, but it can contain more data than what you want to display.

The from , to arrays that you provide adapt the map entry that you provide to the view that you provide. If you include extra info in the map entry it is simply ignored and not displayed. So in this case, assuming you don't want to display the personid, you would add personid to the Map entry, but not add it to the from , and to arrays

public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)

Added in API level 1 Constructor

Parameters

context: The context where the View associated with this SimpleAdapter is running

data: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"

resource: Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"

from: A list of column names that will be added to the Map associated with each item.

to: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

Post a Comment for "Setting Unique Id To Listview Android"