Jon P, you can read me like an Android Book.

Yes it's is a List<Headword> and yes you're right, RemoveAt is the way to go in 
case there are multiple of the 'same' item in the list.

The problem though was that I was attaching the event handlers multiple times 
because I wasn't detaching the handler when convertView parameter is passed, so 
it was getting called twice with the current position, and whatever was 
attached last time.


OK, last thing. When I delete the item, I want the listview to update and NOT 
scroll to the top. Recreating the adapter with the now updated list updates the 
view, but scrolls to the top (and is inefficient). All the SO articles and 
docco say to use ArrayAdapter<T>.NotifyDataSetChanged(). I find that method 
doesn't work as I expect though:


I can do along the lines of (sorry not on the coding computer):

lvHeadwords.Adapter = new MyCustomAdapterHeadwords(Headwords); //which inherits 
ArrayAdapter<T>


//Then in response to a delete click....

Headwords.RemoveAt(position)
(((MyCustomAdapterHeadwords)lvHeadwords.Adapter).NotifyDataSetChanged();
Assert.AreEqual(lvHeadwords.Adapter.Count, Headwords.Count); //Boom, they're 
not.... and the UI hasn't updated



That's supposed to work right? As long as the adapter and Headwords are 
pointing to the same reference? What's the most efficient way to sync the list 
and the adapter?



________________________________
 From: Jonathan Pryor <j...@xamarin.com>
To: Steven Pack <steven_john_p...@yahoo.com.au>; Discussions related to Mono 
for Android <monodroid@lists.ximian.com> 
Sent: Wednesday, 8 August 2012 2:02 AM
Subject: Re: [mono-android] Detecting and responding to image clicks in a list 
view item.
 
On Aug 7, 2012, at 2:39 AM, Steven Pack <steven_john_p...@yahoo.com.au> wrote:
> and deleting the item:
> 
> void DeleteItem(int position)
>         {
>             _log.Debug(Tag, "Deleting from position: " + position);
> 
>             var item = Headwords[position];
>             Headwords.Remove(item);
> 
>             RefreshListView();
>         }
> 
> 
> The DeleteItem method throws IndexOutOfRangeExceptions, firstly because it 
> gets called multiple times and also because the position index doesn't 
> actually represent the real index in the list somehow.

What is Headwords? What is Headword?

If I assume that Headwords is a List<Headword>, List<T>.Remove() [0] uses 
List<T>.IndexOf() which uses Array.IndexOf() [1] which uses 
EqualityComparer<T>.Equals(). If Headword doesn't implement 
IEquatable<Headword>, then EqualityComparer<Headword> will in turn use 
object.Equals(), which defaults to reference equality, which may or may not be 
correct.

Furthermore, if you have the same Headword instance in Headwords multiple 
times, List<T>.IndexOf() will return the _first_ index, which may not be the 
index you want.

In any event, you should probably  be using Headwords.RemoveAt(position), _not_ 
Headwords.Remove(Headwords[position]). The semantics are not equivalent between 
those expressions if/when the same element is present multiple times in the 
list.

- Jon

[0] 
https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Collections.Generic/List.cs#L501
[1] 
https://github.com/mono/mono/blob/master/mcs/class/corlib/System/Array.cs#L2810
_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to