Search code examples
androidandroid-listviewlistadapter

using string to identify the elements of a listView in android


As the question suggests, is it possible to identify the elements of a listView using string ids or something similar to id? I know the typical signature is

public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {...}

But I need to have string ids or such. How would I do that?

Elaboration (sorry if still unclear)

The problem is my underlying list is a rotating list (i.e. circular list)

myList.addFirst(myList.deleteLast());

So the id/index of an item keeps actually floating around. But I need an id that is essentially a part of the element so that no matter the rotation, I can always retrieve the exactly element I mean to.


Solution

  • Create an Array List of strings. Add the strings id for listview in it.

    Add a hidden textview in your listitems.

    Add all the string id from Array List to your listview hidden textview.

    Access the string id as below :

    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        TextView anyId = (TextView) view.findViewById(R.id.anyID);
        String val_Id = anyId.getText().toString();
    

    Using ArrayList allows to handle dynamic listview.