NotifyDataSetChanged isn't updating arrayList when called the second time

时间:2019-05-31 11:23:17

标签: java android arrays android-recyclerview notifydatasetchanged

I have 5 lists displaying in the adapter one after another. I have to delete 2 rows when user clicks on a button in the activity. (Remove two options from the list every time the button is clicked) Each list gets the button click only once. On click of button, NotifyDataSetChanged method does not update the list in the adapter.

For now it is happening for list 1, when 2nd list comes in, the button is clicked, 2 items from arraylist are deleted but notifyDataSetChanged does not update the list in the adapter.

In Activity : row_number = count that i get from implementing the interface mentioned in adapter

  private void removeTwoFunctionality() {

    button_removeTwo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (row_number == 1) {
                TextArray1.remove(0);
                TextArray1.remove(1);
                adapter.notifyDataSetChanged();
                adapter.RemoveTwoButtonClicked();
            }
            if (row_number == 2) {
                TextArray2.remove(0);
                TextArray2.remove(1);
                adapter.notifyDataSetChanged();
                adapter.RemoveTwoButtonClicked();
            }
            if (row_number == 3) {
                TextArray3.remove(0);
                TextArray3.remove(1);
                adapter.notifyDataSetChanged();
                adapter.RemoveTwoButtonClicked();
            }
            if (row_number == 4) {
                TextArray4.remove(0);
                TextArray4.remove(1);
                adapter.notifyDataSetChanged();
                adapter.RemoveTwoButtonClicked();
            }
            if (row_number == 5) {
                TextArray5.remove(0);
                TextArray5.remove(1);
                adapter.notifyDataSetChanged();
                adapter.RemoveTwoButtonClicked();
            }
        }
    });
}

In ADAPTER onBindViewHolder: All the conditions are satisfied when next list arrives

// list 1
    if (!TextArray1.isEmpty()) {
        count = 1;
        holder.name.setText(TextArray1.get(position).getTitle());
    }
// list 2
    if (TextArray1.isEmpty() && TextArray2.size() > 0) {
        count = 2;
        holder.name.setText(TextArray2.get(position).getTitle());
    }
// list 3
    if (TextArray2.isEmpty() && TextArray3.size() > 0) {
        count = 3;
        holder.name.setText(TextArray3.get(position).getTitle());
    }
// list 4
    if (TextArray3.isEmpty() && TextArray4.size() > 0) {
        count = 4;
        holder.name.setText(TextArray4.get(position).getTitle());
    }
// list 5
    if (TextArray4.isEmpty() && TextArray5.size() > 0) {
        count = 5;
        holder.name.setText(TextArray5.get(position).getTitle());
    }
// interface to know the count in the activity
    if (mOnAnswerListener != null) {
        mOnAnswerListener.getRowNumber(count);
    }

2 个答案:

答案 0 :(得分:0)

尝试这样

                TextArray5.remove(0);
                TextArray5.remove(1);
                adapter.RemoveTwoButtonClicked();
                adapter.notifyDataSetChanged();

答案 1 :(得分:0)

您应在删除项目后放置notifyDataSetChanged:

  TextArray5.remove(0);
  TextArray5.remove(1);
  adapter.RemoveTwoButtonClicked();
  adapter.notifyDataSetChanged();