title: How to remove the last item in recyclerview adapter by a button?
body: <p>I have a recyclerview and I want to delete the last item on it when I click a button that is not in the recyclerview. I want to delete the item and notify the adapter that the item is deleted so I can enter another item. By the way, the items I enter are by edittext and a button.</p>
  <p><strong>activity_main.xml</strong>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;com.example.brecyclerview.MainActivity&quot;
      android:orientation=&quot;vertical&quot;&gt;
  
      &lt;EditText
          android:id=&quot;@+id/edit_ten&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:ems=&quot;10&quot;
          android:hint=&quot;Score&quot;
          android:inputType=&quot;numberSigned|number&quot; /&gt;
  
      &lt;LinearLayout
          android:orientation=&quot;horizontal&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;&gt;
  
          &lt;Button
              android:id=&quot;@+id/btn_add&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;
              android:layout_weight=&quot;1&quot;
              android:onClick=&quot;btn_add&quot;
              android:text=&quot;Add&quot;
              tools:ignore=&quot;OnClick&quot; /&gt;
  
          &lt;Button
              android:id=&quot;@+id/btn_undo&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;
              android:layout_weight=&quot;1&quot;
              android:onClick=&quot;btn_undo&quot;
              android:text=&quot;Undo&quot;
              tools:ignore=&quot;OnClick&quot; /&gt;
  
          &lt;Button
              android:id=&quot;@+id/btn_new&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;
              android:layout_weight=&quot;1&quot;
              android:onClick=&quot;btn_new&quot;
              android:text=&quot;New Game&quot; /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:id=&quot;@+id/recycleViewContainer&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:layout_alignParentStart=&quot;true&quot;
          android:layout_alignParentLeft=&quot;true&quot;
          android:layout_alignParentTop=&quot;true&quot;
          tools:itemCount=&quot;8&quot; /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p><strong>list_item.xml</strong>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;&gt;
  
      &lt;RelativeLayout
          android:id=&quot;@+id/singleRow&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:padding=&quot;8dp&quot;&gt;
  
          &lt;ImageView
              android:id=&quot;@+id/userImg&quot;
              android:src=&quot;@mipmap/ic_launcher&quot;
              android:layout_width=&quot;60dp&quot;
              android:layout_height=&quot;60dp&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/pNametxt&quot;
              android:text=&quot;User Name&quot;
              android:textSize=&quot;20sp&quot;
              android:layout_marginTop=&quot;6dp&quot;
              android:maxLines=&quot;1&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_alignParentTop=&quot;true&quot;
              android:layout_toRightOf=&quot;@+id/userImg&quot;
              android:layout_toEndOf=&quot;@+id/userImg&quot;
              android:layout_marginLeft=&quot;8dp&quot;
              android:layout_marginStart=&quot;8dp&quot; /&gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;View
          android:layout_below=&quot;@+id/singleRow&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;1dp&quot;
          android:background=&quot;#f2f2f2&quot; /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p><strong>MainActivity.java</strong>:</p>
  <pre><code>import androidx.annotation.NonNull;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.recyclerview.widget.LinearLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class MainActivity extends AppCompatActivity {
      int total = 0;
      Button button;
      Button button1;
      Button button2;
      EditText editText;
      TextView personName;
      RecyclerView recyclerView;
      RecyclerView.Adapter mAdapter;
      RecyclerView.LayoutManager layoutManager;
  
      List&lt;PersonUtils&gt; personUtilsList;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          button = (Button)findViewById(R.id.btn_add);
          button1 = (Button)findViewById(R.id.btn_undo);
          button2 = (Button)findViewById(R.id.btn_new);
          editText = (EditText)findViewById(R.id.edit_ten);
          personName = (TextView)findViewById(R.id.pNametxt);
  
          recyclerView = (RecyclerView) findViewById(R.id.recycleViewContainer);
          recyclerView.setHasFixedSize(true);
          layoutManager = new LinearLayoutManager(getApplicationContext());
          recyclerView.setLayoutManager(layoutManager);
          personUtilsList = new ArrayList&lt;&gt;();
          mAdapter = new CustomRecyclerAdapter(this, personUtilsList);
  
          button2.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (v == button2) {
                      MainActivity.this.finish();
  
                      startActivity(new Intent(MainActivity.this, MainActivity.class));
                  }}});
  
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  for (int i = 0; i&lt;personUtilsList.size(); i++)
                  {
                      total = personUtilsList.get(i).getPersonName();
                  }
                  total += Integer.parseInt(editText.getText().toString());
  
                  personUtilsList.add(new PersonUtils(total));
                  recyclerView.setAdapter(mAdapter);
                  mAdapter.notifyDataSetChanged();
                  editText.setText(&quot;&quot;);
              }});
          button1.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  for (int i = 0; i&lt;personUtilsList.size(); i++)
                  {
  
                      personUtilsList.remove(i).getPersonName();
                  personUtilsList.remove(new PersonUtils(total));
                  recyclerView.setAdapter(mAdapter);
                  mAdapter.notifyDataSetChanged();
              }}
          });
      }}
  </code></pre>
  <p><strong>CustomRecyclerAdapter.java</strong>:</p>
  <pre><code>import android.content.Context;
  import android.preference.PreferenceScreen;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import androidx.recyclerview.widget.RecyclerView;
  
  import java.util.List;
  
  public class CustomRecyclerAdapter extends RecyclerView.Adapter&lt;CustomRecyclerAdapter.ViewHolder&gt; {
      private Context context;
      private List&lt;PersonUtils&gt; personUtils;
  
      public CustomRecyclerAdapter(Context context, List personUtils) {
          this.context = context;
          this.personUtils = personUtils;
      }
  
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
          ViewHolder viewHolder = new ViewHolder(v);
          return viewHolder;
      }
  
      @Override
      public void onBindViewHolder(ViewHolder holder, int position) {
          holder.itemView.setTag(personUtils.get(position));
  
          PersonUtils pu = personUtils.get(position);
  
         holder.pName.setText(String.valueOf(pu.getPersonName()));
      }
  
      @Override
      public int getItemCount() {
          return (personUtils.size()&gt;8)?8:personUtils.size();
      }
  
      public static class ViewHolder extends RecyclerView.ViewHolder{
          public TextView pName;
          final Button deleteButton;
  
          public ViewHolder(final View itemView) {
              super(itemView);
  
              pName = (TextView) itemView.findViewById(R.id.pNametxt);
              deleteButton = (Button) itemView.findViewById(R.id.btn_undo);
          }
  }}
  </code></pre>
  <p><strong>PersonUtils.java</strong>:</p>
  <pre><code>public class PersonUtils {
      private Integer personName;
  
      public static void remove(int index) {
      }
  
      public Integer getPersonName() {
          return personName;
      }
  
      public void setPersonName(Integer personName) {
          this.personName = personName;
      }
  
      public PersonUtils(Integer personName) {
          this.personName = personName;
      }
  }
  </code></pre>
  <p>I expect to delete the last item (integer) in the recyclerview and notify the adapter so I can edit the value when needed.</p>