Home » Android » android – How can I insert element in Recycler View?-Exceptionshub

android – How can I insert element in Recycler View?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment


I have a problem with RecyclerView.
Every time I try to manage a Rec View for insert or delete or update some element I have to fight with Recycler View and the evil notifyDataSetChange.

I tried with runOnUiThread and everytime this solution is useless.
I tried to call notifyDataSetChange in the Adapter or in the Activity that instance the Rec View.

I really need to know how to make this work everytime to be a little better developer.
I really hope to solve this problem definitely.

This is my new try

public class MainActivity extends AppCompatActivity {

    private Button nextTurn;
    private Button submit;
    private EditText name;
    private EditText number;

    private CounterElementAdapter mAdapter;
    private RecyclerView recV;
    private List<CounterElement> list;

    protected void onCreate(Bundle savedInstanceState) {

        nextTurn = (Button) findViewById(R.id.button2);
        submit = (Button) findViewById(R.id.button);
        name = (EditText) findViewById(R.id.editText);
        number = (EditText) findViewById(R.id.editText2);
        recV = (RecyclerView) findViewById(R.id.recV);

        list = new ArrayList<CounterElement>();

        mAdapter = new CounterElementAdapter(list);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recV.setItemAnimator(new DefaultItemAnimator());

        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(name.getText().toString().length() > 0 && number.getText().toString().length() > 0) {
                    mAdapter.addElement(new CounterElement(name.getText().toString(), Integer.parseInt(number.getText().toString())));

        nextTurn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

public class CounterElementAdapter extends RecyclerView.Adapter<CounterElementAdapter.MyViewHolder> {

    private List<CounterElement> elementList;

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView name, counter;

        public MyViewHolder(View view) {
            name = view.findViewById(R.id.name);
            counter = view.findViewById(R.id.counter);

    public CounterElementAdapter(List<CounterElement> elementList) {
        this.elementList = elementList;

    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.element, parent, false);

        return new MyViewHolder(itemView);

    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        CounterElement ce = elementList.get(position);

    public int getItemCount() {
        return elementList.size();

    public void addElement(CounterElement ce){

    public void nextTurn(){
        for(CounterElement ce : elementList){
            if(ce.getStatus() >= 1)
                ce.setStatus(ce.getStatus() - 1);
            else {

Thanks in advance

How to&Answers:

Your code looks fine. I would recommend you to take a look at this article
Also try to debug your code and check if your elementList updates correctly.


Looked at your code for a while, didn’t seem to find anything wrong, Are you using an android emulator for testing or a physical device? Sometimes emulators tend to be buggy.
Try initializing your array list with a few dummy values at first and then try to add or remove and see if that helps