Xamarin Android: Using a botton navigation view to delete a selected recyclerview item

TaniguchiSantosTaniguchiSantos Member ✭✭✭
edited October 4 in Xamarin.Android

I've implemented a RecyclerView and on my page I've inserted a bottom navigation which have a delete button that i would like to delete the recycler view items that are selected. I wrote code where I get the position the item but when I use:

mEmails.RemoveAt(position);

nothing is happening.

How can I use bottom navigation view to delete a recycler view selected items?

My RecyclerView Adapter:

   public class RecyclerAdapter : RecyclerView.Adapter, View.IOnClickListener
 {
   private List<Email> mEmails;
   private Activity mActivity;
   public static int listPosition;

   private Boolean isSelected = false;

  public Boolean IsSelected()
  {
    return isSelected;
 }

 public void setSelected(Boolean selected)
 {

    isSelected = selected;
  }

  public RecyclerAdapter(List<Email> emails, Activity activity)
 {
    mEmails = emails;
    mActivity = activity;
  }

 public override int ItemCount => mEmails.Count;

  public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
 {
    View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row, parent, false);
    RecyclerViewHolder view = new RecyclerViewHolder(row);
    return view;
  }
  public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
  {
    RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
    myHolder.mDate.Text = ((Email)mEmails[position]).date;
    myHolder.mSubject.Text = ((Email)mEmails[position]).Subject;
    myHolder.mMessage.Text = ((Email)mEmails[position]).Message;

    myHolder.ItemView.SetBackgroundColor(((Email)mEmails[position]).IsSelected() ? Color.LightBlue : Color.Transparent);
    myHolder.ItemView.Tag = position;
    myHolder.ItemView.SetOnClickListener(this);
  }

  void View.IOnClickListener.OnClick(View v)
  {
     int position = (int)v.Tag;
    listPosition = (int)v.Tag;

    ((Email)mEmails[position]).setSelected(!((Email)mEmails[position]).IsSelected());
    v.SetBackgroundColor(((Email)mEmails[position]).IsSelected() ? Color.LightBlue : Color.Transparent);

    Toast.MakeText(v.Context, "Click : " + ((Email)mEmails[position]).date + "---" + position, ToastLength.Short).Show();
 }


 public void DeleteItem(int position)
 {
     mEmails.RemoveAt(position);
    NotifyDataSetChanged();
    NotifyItemChanged(listPosition);

   }


  }

where i calling my bottom navigation view:

 public class MainActivity : AppCompatActivity
 {
public static RecyclerView mRecyclerView;
public static RecyclerView.LayoutManager mLayoutManager;
public RecyclerView.ViewHolder holder;
public static BottomNavigationView bottomnavigationview1;
public static RecyclerView.Adapter mAdapter;
public static List<Email> mEmails;

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    Xamarin.Essentials.Platform.Init(this, bundle);
    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.activity_main);

    mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerViwer);
    mRecyclerView.AddItemDecoration(new DividerItemDecoration(mRecyclerView.Context, DividerItemDecoration.Vertical));
    mRecyclerView.HasFixedSize = true;
    SetupList();

    //Create our layout Manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.SetLayoutManager(mLayoutManager);
    RecyclerAdapter mAdapter = new RecyclerAdapter(mEmails, this);
    mRecyclerView.SetAdapter(mAdapter);

    bottomnavigationview1 = FindViewById<BottomNavigationView>
          (Resource.Id.bottom_navigation);
    bottomnavigationview1.NavigationItemSelected += BottomNavigation_NavigationItemSelected;

}

private void SetupList()
{
    mEmails = new List<Email>();
    mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
    mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
    mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
    mEmails.Add(new Email() { date = "9/24/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
    mEmails.Add(new Email() { date = "9/24/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
}

private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
    LoadFragment(e.Item.ItemId);
}

public void LoadFragment(int id)
{
    Android.Support.V4.App.Fragment fragment = null;
    switch (id)
    {
        case Resource.Id.bottomDelete:

            RecyclerAdapter mAdapter = new RecyclerAdapter(mEmails, this);
            mAdapter.DeleteItem(RecyclerAdapter.listPosition);

        break;
    }

    if (fragment == null)
        return;

    SupportFragmentManager.BeginTransaction()
        .Replace(Resource.Id.content_frame, fragment)
        .Commit();
}

Best Answer

Answers

Sign In or Register to comment.