Removing a list in xamarin android

Hi i had a problem deleting items in a listview on android, im using xamarin android, i have a custom row with a delete button but when i click on it deleting the last item and not the selected item.

here is my code

public override View GetView(int position, View convertView, ViewGroup parent)
    {
        Button btnDelete;
        var item = _items[position];
        var view = convertView;
        if (view == null)
        {
            view = _content.LayoutInflater.Inflate(Resource.Layout.CustomRow, null);
            view.FindViewById<TextView>(Resource.Id.PostTitle).Text = item.PostTitle;
            view.FindViewById<TextView>(Resource.Id.PostMessage).Text = item.PostMessage;
            view.FindViewById<TextView>(Resource.Id.PostDate).Text = item.PostDate;
            view.FindViewById<TextView>(Resource.Id.AuthorName).Text = item.AuthorName;
            view.FindViewById<ImageView>(Resource.Id.PostAvatar).SetImageResource(item.ImageResourceId);
            view.FindViewById<TextView>(Resource.Id.Date_Read).Text = item.DateRead;
            btnDelete = view.FindViewById<Button>(Resource.Id.btnDelete);
            btnDelete.Click += (sender, args) =>
            {
                var delPos = (int) (((Button) sender).GetTag(Resource.Id.btnDelete));
                _items.RemoveAt(delPos);
                NotifyDataSetChanged();
            };
        }
        else
        {
            btnDelete = view.FindViewById<Button>(Resource.Id.btnDelete);

            btnDelete.SetTag(Resource.Id.btnDelete, position);
        }

        return view;
    }

      

or you can tell guys how to get the id of the selected row in the listview, i think this is the problem.

btnDelete.Click += (sender, args) =>
            {
                var delPos = (int) (((Button) sender).GetTag(Resource.Id.btnDelete));
                _items.RemoveAt(delPos);
                NotifyDataSetChanged();
            };

      

+3


source to share


3 answers


solve it just by putting this line of code

view.FindViewById<TextView>(Resource.Id.PostTitle).Text = item.PostTitle;
        view.FindViewById<TextView>(Resource.Id.PostMessage).Text = item.PostMessage;
        view.FindViewById<TextView>(Resource.Id.PostDate).Text = item.PostDate;
        view.FindViewById<TextView>(Resource.Id.AuthorName).Text = item.AuthorName;
        view.FindViewById<ImageView>(Resource.Id.PostAvatar).SetImageResource(item.ImageResourceId);
        view.FindViewById<TextView>(Resource.Id.Date_Read).Text = item.DateRead;

      



outside the if statement if(view == null

+3


source


I would suggest that you have to move the set of tags outside of the if-else. From:

    else
    {
        btnDelete = view.FindViewById<Button>(Resource.Id.btnDelete);

        btnDelete.SetTag(Resource.Id.btnDelete, position);
    }

      

in



    else
    {
        btnDelete = view.FindViewById<Button>(Resource.Id.btnDelete);
    }

    btnDelete.SetTag(Resource.Id.btnDelete, position);

      

Also try changing the tag to this:

// to set
btnDelete.Tag = position;

// to retrieve
var position = (int)((sender as Button).Tag);

      

+1


source


First, you need to take the else code and move it outside the else. Because in the current form, if this is the first time this view is displayed, this view will not have a tag.

else
{
    btnDelete = view.FindViewById<Button>(Resource.Id.btnDelete);
    btnDelete.SetTag(Resource.Id.btnDelete, position);
}

      

To:

btnDelete = view.FindViewById<Button>(Resource.Id.btnDelete);
btnDelete.SetTag(Resource.Id.btnDelete, position);

      

After that, I recommend putting a breakpoint in your code where you are setting and getting the position to check if its setting and correct position match. You can find the error from there.

Also to make your list clickable you need to add this code to your xml, to the view group containing the list item:

android:descendantFocusability="blocksDescendants"

      

I created my own project and tested it and it works.

+1


source







All Articles