My app crashes and deletes some other child on removing data

I am making an android app based on firebase Realtime database. I am reading the data in a recycler view. There are three items in my recycler view.

  1. Text view = Name
  2. Text view = Number
  3. Button = Delete

When I delete some child from my recycler view three things happen:

  1. Sometimes it deletes the targeted child normally

2.Sometimes it deletes some other child ( often which is below the targeted child.)

3.Sometimes my app crashes.

This is the code for my adapter:



public class holder extends RecyclerView.ViewHolder {
    public Button btnDelete;
    public TextView tvName;
    public TextView tvRoll;

    public holder(@NonNull View itemView) {
        super(itemView);
        btnDelete=(Button) itemView.findViewById(R.id.idDelete);
        tvName=(TextView) itemView.findViewById(R.id.idName);
        tvRoll=(TextView) itemView.findViewById(R.id.idRoll);

    }
}

This is the code to show items in recycler view and code for delete button:

 options = new FirebaseRecyclerOptions.Builder<basic>()
                .setQuery(myRef, basic.class)
                .build();
        adapter = new FirebaseRecyclerAdapter<basic, holder>(options) {
            @SuppressLint("SetTextI18n")
            @Override
            protected void onBindViewHolder(@NonNull holder holder, final int i, @NonNull final basic basic) {
                holder.tvName.setText(basic.getFb01name());
                holder.tvRoll.setText(basic.getFb04roll());
                holder.btnDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            myRef.child(getRef(i).getKey()).removeValue();
                    }
                });
            }

            @NonNull
            @Override
            public holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
                return new holder(v);
            }
        };
        //------------------------------
        adapter.startListening();
        Userlist.setAdapter(adapter);
        //-----------------------------------

I cannot guess where is the problem. Will anybody kindly investigate my codes? And Please also provide a practical solution.



Read more here: https://stackoverflow.com/questions/65078470/my-app-crashes-and-deletes-some-other-child-on-removing-data

Content Attribution

This content was originally published by user14253444 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: