Hi,
I'm using a listview for the user to mark several items before clicking ok, and each time an item is marked I want to save the position of that item. I've been trying doing this by having a bool[] with as many items as the list and changing the value of the bool in the array with the same position as the list item when that item is clicked. This works fine whenever I click an item that isn't the top one, but when the top item is clicked all items in the bool[] are changed. When I printed the position to the console from my click event handler I saw something strange. Whenever I clicked the top item the position of all items in the list was printed twice before the top item (position 0) was printed again.
Can anyone explain to me why this is happening? and how to solve it? Any help is appreciated...
Here's the code in question:
public class CheckedListAdapter : BaseAdapter <string>
{
Activity context;
List<string> rettsId;
List<string> titleStr = new List<string>();
List<int> iconId = new List<int>();
public CheckedListAdapter(Activity context, List<string> rettsId, List<string> titleStr, List<int> iconId) : base()
{
this.context = context;
this.rettsId = rettsId;
this.titleStr = titleStr;
this.iconId = iconId;
}
public override long GetItemId(int position)
{
return position;
}
public override string this[int position]
{
get { return titleStr[position]; }
}
public override int Count
{
get { return titleStr.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
ViewHolder vh;
View view = convertView; // re-use an existing view, if one is available
if (view == null) // otherwise create a new one
{
view = context.LayoutInflater.Inflate(Resource.Layout.retts_list_row_check, null);
vh = new ViewHolder();
vh.Initialize(view);
view.Tag = vh;
}
vh = (ViewHolder)view.Tag;
vh.Bind (context, rettsId[position], titleStr [position], iconId [position], position);
return view;
private class ViewHolder : Java.Lang.Object
{
TextView tvTitle;
ImageView ivIcon;
CheckBox cbChoice;
public void Initialize(View view)
{
tvTitle = view.FindViewById<TextView> (Resource.Id.textViewRettsTitle);
ivIcon = view.FindViewById<ImageView> (Resource.Id.imageViewRettsIcon);
cbChoice = view.FindViewById<CheckBox> (Resource.Id.checkBoxRetts);
}
public void Bind(Activity context, string rettsId, string titleStr, int iconId, int position)
{
tvTitle.Text = titleStr;
ivIcon.SetImageResource (iconId);
tvTitle.Click += (sender, e) => CheckItemClick(sender, e, position);
ivIcon.Click += (sender, e) => CheckItemClick(sender, e, position);
cbChoice.Click += (sender, e) => CheckItemClick(sender, e, position);
}
private void CheckItemClick(object sender, EventArgs e, int position)
{
cbChoice.Checked = true;
CurrentStats.SetSymptomYN(true, position);
Console.WriteLine (position);
}
}
}