0

I have a ListView which has a autocomplete feature using array adapter, whichworks perfectly fine. My problem is, when i click on the list item after the filtering of listview, i get the position index 0 for the 1st item,1 for the second item and so on for the items in the filtered list (position obtained in onlistitemclick listener).

How do i get the position index of the selected item relative to the list before filtering?

2 Answers2

2

It would be better if you post the code(at least the snippet in which the adapter is set up). I assume that you have used something like the below.

String[] items={"Item 1", "Item 2", "Item 3"};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState)

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
}

If this is the case, setting the tags and identifying the positions might not be possible. If you can have string validations directly rather than finding positions that is okay. But you said you have over 100 items.

So, better create a custom adapter that extends an ArrayAdapter or some other. In that CustomAdapter, in the getView(int position, View convertView, ViewGroup parent) method you set each item to a TextView by textView.setText(String[position]); and after this set tag for each view(textView) by textView.setTag(position);

Finally you set the custom adapter with listView.setAdapter(customAdapter); in your activity class, so that you can have getTag() to find the position which doesn't change even after filtering your items. Let's see if I could edit my answer after you could post the code, in case you don't wanna go with my present suggestion.

Enthusiast
  • 249
  • 1
  • 8
0

When you inflate the view set a tag for the view with the position that the item was in the list before filtering.

Example

view.setTag(originalPosition);    
view.setTag(resource_id, originalPosition);

When you access the view through the relative position, retrieve the absolute position using view.getTag()

Deepak Bala
  • 11,095
  • 2
  • 38
  • 49
  • can you please elaborate your answer? to which view should i add the tag?There are over 100 items in the listview – Srinivas Kattimani Oct 13 '12 at 13:15
  • Take a look at this method[1]. It is part of the adapter that populates a view. Assume you have a TextView in every list item. You can tag the TextView with the item position while returning the inflated row-view in getView() [1] - public View getView(int position, View convertView, ViewGroup parent) – Deepak Bala Oct 13 '12 at 14:38