Hi all, long time no post.
I have a listview. and the items are defined as a textview with a cross image
over on the right. Like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/headword_textview"
android:layout_gravity="fill_horizontal"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="5dp"
android:textColor="@android:color/black"
android:textSize="16sp"
android:contentDescription="Bookmark" />
<ImageView
android:id="@+id/delete_headword_list_item_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="@drawable/clear"
android:layout_gravity="center_vertical"
android:contentDescription="Delete bookmark button" />
</LinearLayout>
The little cross is an image, and on clicking it, I'd like to delete the
corresponding item from the list. My problem is detecting the image click. Or,
more specifically, attaching and detaching the event handlers and getting the
position right. My code works fine when there is only a single screen, but as I
soon as the
list is longer than a screen, and GetView keeps getting called, the event
handler keeps getting re-attached, and then the positions don't match the index
in the list.
Populating the listview is like this:
var adapter = new ListActivityRowArrayAdapter<Headword>
(Registry.CustomTypeface, Registry.Settings.TextSize, this,
Resource.Layout.headword_list_item, Resource.Id.headword_textview, Headwords);
lvHeadwords.Adapter = adapter;
The custom adapter, in which I customise the textview using some funky fonts
and attach the event handler is like such:
public class ListActivityRowArrayAdapter<T> : ArrayAdapter<T>
{
Typeface _typeface;
float _textSize;
HeadwordListActivityBase _activity;
public ListActivityRowArrayAdapter (Typeface typeface, float
textSize, HeadwordListActivityBase context, int resourceId, int
textViewResourceId, ICollection<T> objects)
: base (context, resourceId, textViewResourceId,
objects.ToArray())
{
_activity = context;
_typeface = typeface;
_textSize = textSize;
}
public override View GetView (int position, View convertView,
ViewGroup parent)
{
View row = base.GetView (position, convertView, parent);
var textView = row.FindViewById(Resource.Id.headword_textview)
as TextView;
if (textView != null && (textView.Typeface != _typeface ||
textView.TextSize != _textSize))
{
textView.SetTypeface (_typeface, 0);
textView.TextSize = _textSize;
}
var deleteImage =
row.FindViewById(Resource.Id.delete_headword_list_item_button);
if (deleteImage != null)
{
_log.Debug(Tag, "Attaching handler. Position is:" +
position);
deleteImage.Click += (sender, e) =>
_activity.DeleteItem(position);
}
return row;
}
}
}
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.
Any pointers would be appreciated.
_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com
UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid