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;

    protected void onCreate(Bundle savedInstanceState) {

        nextTurn = (Button) findViewById(;
        submit = (Button) findViewById(;
        name = (EditText) findViewById(;
        number = (EditText) findViewById(;
        recV = (RecyclerView) findViewById(;

        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(;
            counter = view.findViewById(;

    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 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 *