-2

Why is my question getting negative marks? i do not understand. Please help.

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);
    }
Honey
  • 19
  • 10
  • First of all, there are separated methods, for notifying only a part of dataset. Check the official docs, also check the again the behaviour of RecyclerView adapter. On the other hand, I know that it's not my business and the question is not asking advice regarding coding style, but why don't you use parameters instead of TextArray1 TextArray2 etc. in this way, the adapter just seems a but unnecessary, and it is too messy. Anyway, welcome to stackoverflow :D – barotia May 31 '19 at 11:33
  • Thank you. I have used parameters in the adapter constructor. And only one list is appeared at a time. That's how the notifyDataSetChanged for whole list. @barotia – Honey May 31 '19 at 11:41
  • You seem to have single adapter for many lists. Why not make it one adapter and one list, or many adapters and many lists? – M. Prokhorov May 31 '19 at 12:19
  • because the second list is suppose to swipe in when any item in the first list is clicked. so the adapter is called again but this time the count is 2. it's kind of a continuous questionnaire. @M.Prokhorov – Honey May 31 '19 at 13:12
  • I don't get it. List contains answers then? Then why not just fill the single form and animate it? You would still have one adapter. – M. Prokhorov May 31 '19 at 13:22
  • That's not how the app module works. answers has a different arraylist. @M.Prokhorov – Honey Jun 04 '19 at 06:07
  • Could you help me with this @Quicklearner – Honey Jun 04 '19 at 10:10

2 Answers2

0

Try like this

                TextArray5.remove(0);
                TextArray5.remove(1);
                adapter.RemoveTwoButtonClicked();
                adapter.notifyDataSetChanged();
  • Same thing happens, works for the first time and second time the arraylist items 0 and 1 are deleted but does not update the adapter. – Honey Jun 04 '19 at 06:28
0

You should put notifyDataSetChanged after deleting the items :

  TextArray5.remove(0);
  TextArray5.remove(1);
  adapter.RemoveTwoButtonClicked();
  adapter.notifyDataSetChanged();
Osama Abdelaziz
  • 132
  • 2
  • 8
  • Same thing happens, works for the first time and second time the arraylist items 0 and 1 are deleted but does not update the adapter. – Honey Jun 04 '19 at 06:29