how can i find items from listView

  • Thread starter Thread starter Shahzaib Younis
  • Start date Start date
S

Shahzaib Younis

ListView = new ListView();

ListViewItem lvi = new ListViewItem();
lvi.add("1");
lvi.add("hello");

listView.items.add(lvi);

I can add items this way, but when I use indexOf or contains method to find
element in the list, I get nothing.
listView.indexOf(lvi);

Any idea what I'm missing here.
 
You are missing the fact that ListViewItem is a referenced object. When you
use new ListViewItem you create a new object. The Items collection is
searched via comparing the item.GetHashValue(). When you create a new item
it is exactly that - a new item.. It does not exist in Items collection. The
solution would be to maintain a hashtable that uses item text as an index
and the item itself as the element. When you populate the listview,
simulataneously populate the hashtable. When you need to find an item by its
text, search for it in the hashtable and then use IndexOf.

You can also use LVM_FINDITEM message but it's a bit of bother to maintain
the data structure
 
Back
Top