I have a recyclerview and and a textview in my activity I want to show my list size in textview but I need to change it whenever my list size has changed question is how can I make my activity listen to recyclerview after recyclerview has changed
Asked
Active
Viewed 214 times
0
-
can you write your code? – Vir Rajpurohit Apr 04 '18 at 09:54
-
You can use AdapterDataObserver( https://developer.android.com/reference/android/support/v7/widget/RecyclerView.AdapterDataObserver.html), but it is not the best way in this case. I think that you should change the way you handling item changing, but i can't say more without your code. – tema_man Apr 04 '18 at 10:20
1 Answers
3
Whenever your recyclerView is notified for dataChange, write
textView.setText(String.valueOf(yourAdapter.getItemCount()));
If on buttonClick, you are adding data into list, you will have to notify the adapter that data has been changed
let's take an example as below,
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.add(question);
adapter.notifyDataSetChanged();
textView.setText(String.valueOf(yourAdapter.getItemCount()));
}
});
If you are removing data from list then you will have position from where you are removing the data
I think that operation is being done in adapter, so to get that data into relevant activity or fragment you can use interface.
Check for my answer at some similar type of question

Deep Patel
- 2,584
- 2
- 15
- 28
-
-
How you are adding your data to list? can you please put the full code? – Deep Patel Apr 04 '18 at 10:13
-
-