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

Exception or error:

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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        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.setLayoutManager(mLayoutManager);
        recV.setItemAnimator(new DefaultItemAnimator());
        recV.setAdapter(mAdapter);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            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() {
            @Override
            public void onClick(View v) {
                mAdapter.nextTurn();
            }
        });
    }
} 

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) {
            super(view);
            name = view.findViewById(R.id.name);
            counter = view.findViewById(R.id.counter);
        }
    }


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

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

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        CounterElement ce = elementList.get(position);
        holder.name.setText(ce.getName());
        holder.counter.setText(ce.getDuration());
    }

    @Override
    public int getItemCount() {
        return elementList.size();
    }

    public void addElement(CounterElement ce){
        elementList.add(ce);
        notifyDataSetChanged();
    }

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

Thanks in advance

How to solve:

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

Leave a Reply

Your email address will not be published. Required fields are marked *