peteratkinson1
peteratkinson1

Reputation: 90

Struggling to get my List View working properly

Before I start. I do apologise for not uploading any code. But I know already understand the problem I have. I just don't know how to sort it.

The problem I'm having is that I have a List View, which is populated by many rows. Each row has a Contacts name, how many contacts the user has in common. A contact display picture and an action button.

When the user clicks on the action button. They are promoted with a Popup Box. Displaying a further four more options; unfriend, block, message, and cancel.

When the user clicks on the unfriend or block button. The row that is associated with that button is then removed from the List View. Sounds simple right?

The List View also has a search filter, which the user may use. The can search by Contact names. Whilst the user is inserting values, the list view is refreshed consecutively, displaying Contacts that contain the values inserted into the search function. Same rules apply, the user may remove, block... Etc.

The problem I face is, when the user tries to unfriend a user by searching their contact name first. It tries to remove a Contact at position 0. Now as we may know, this searched contact may actually be at position 7 in the Array List. And thus, causes a problem. It removes the wrong user. Now, if the user doesn't use the search function. It actually works, the correct position is removed from the list. And ole Joe is happy.

I clear the ArrayList first before reading contacts that contain entered search values.

I'm sorry that I can't upload code, but my Mac isn't available at this time, and thus I can't access my project SC.

Could anybody give me some kind of indication on where I could have possibly went wrong?

Thanks, Peter.

Upvotes: 0

Views: 51

Answers (1)

Aniruddha
Aniruddha

Reputation: 4487

First set the tag in getView method if you have adapter

textView.setTag(position) // set the tag (assuming you have textView in row, or you can set the tag for any other control)

Then while removing

 int position = (Integer)v.getTag();  // get the tag that you had set before.
 your_arraylist.remove(position);  // remove
 // your remaining code
 notifyDataSetChanged();

Hope this gives you some idea. Happy coding.

Upvotes: 1

Related Questions