views:

603

answers:

1

hi,

I am creating a list .. the elements of the list are drawn from sqlite database .. I populate the list using ArrayList and ArrayAdapter ...upon clicking the items on the list I want to be able to fire an intent containing info about the item clicked ... info like the index number of the item ..

using the method : onItemClick(AdapterView av, View v, int index, long arg)

I do get index of the item clicked . however it is of the list currently displayed . the problem comes when I do setFilterTextEnabled(true) , and on the app type in some text to to search some item ..and then click it ..rather than giving me the index of the item on the original list it gives me the index on filtered list..

following is the snippet of code:

myListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int index, long arg) {
            Intent lyricsViewIntent = new Intent(iginga.this, LyricsPage.class);

            lyricsViewIntent.putExtra("title", songList.get((int)arg).getTitle());
            lyricsViewIntent.putExtra("id", songList.get((int)arg).getSongId());
            startActivity(lyricsViewIntent);
        }
    });

    myListView.setTextFilterEnabled(true);

Is there any way I can get the original index /position of the item instead of the one showing in filtered text ...when filtered.

+1  A: 

One of the ways of doing this could be Tagging the view with its index when drawing them View#setTag() and reading them when ever you want with View#getTag()

Samuh
but how do i do that.. I mean m drawing the view by attaching arrayadapter the listview. and that arrayadapter contains the arrayist of my objects .. after I create list from database i just call notifyDataSetChanged() on arrayadapter to inform attached listview of data changed .. so how do i set tag to each item of the listview ...?
Abhinav
Well you can provide your own implementation of ArrayAdapter and inside getView() you can do this.Also, I think, if the objects in your list were Comparable, you could have compared them directly and gotten their position.
Samuh
thanks ... can you enlighten me more on the getting position through comparison.. all objects (items) have an int id in them .. a primary key .. since I am getting data from sqlite ..m just feeding the corresponding primary key into the object as well...
Abhinav
You can make your (POJO)class implement equals method for comparision: returning true when primary keys(ids) of object being compared are same. Then, when you get a handle on this object, in the Filtered ListView, you can find its index/position in the original list using ArrayList's indexOf(Object) method.
Samuh
FYI, IMO you can avoid all this by providing your own implementation ArrayAdapter.
Samuh