10

I have created a RecyclerView that contains dynamic EditText fields. When the values in the EditText fields are edited I need to save them on a click of button. I am using recyclerView.getChildCount() to get the child count which is giving a different number everytime.

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
Shivani
  • 121
  • 2
  • 2
  • 5

6 Answers6

28

RecyclerView does what is indicated by its name. It recycles views. Say you have a list of 1000 entries but at any given time 3-4 of them are shown on the screen. RecyclerView's adapter always holds all of those children so calling recyclerView.getAdapter().getItemCount(); will return a 1000.

However RecyclerView only holds some of the items which are shown and are "close" to being shown so recyclerView.getChildCount() will return a smaller and not-constant value. For most applications you should then use the method provided by the adapter.

sp0rk
  • 483
  • 4
  • 14
  • @Shivani . If I helped you, don't forget to accept the answer. – sp0rk Jul 02 '16 at 16:19
  • Thanks @spork for responding to my question. I have tried using recyclerView.getAdapter().getItemCount(); but from this I can not get the values edited in recyclerview. i'll only get number of items. I am using recyclerView.getChildAt(index) for getting the child view. – Shivani Jul 04 '16 at 07:25
  • If you don't need RV's recycling functionality it may be easier to switch to ListView. If you need to get certain item I suggest you use Adapter's `findViewByPosition(int position);` It returns the view at given position or null if it's not present. Beware that it follows `ignoreView` specs as well. You can read on it here: https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#findViewByPosition(int) – sp0rk Jul 04 '16 at 10:19
  • you mean layout manager's findViewByPosition(int position); ? – Shivani Jul 04 '16 at 11:05
  • its throwing an NullPointerException. – Shivani Jul 04 '16 at 13:47
  • It isn't that helpful you know? What is throwing NPE? What did you change, what does the logcat show? Be more specific to receive a proper answer. :) – sp0rk Jul 07 '16 at 12:34
  • My layout of recyclerView (for each item) consists of one relative layout which further contains a linear layout from which I need to get the values. When I try to get the count of recyclerView using layoutManager.getItemCount(), it is giving me proper number of child. When I write RelativeLayout rl=(RelativeLayout)layoutmanager.findViewByPosition(i); in the loop (i=0 to layoutManager.getItemCount()) and try to get the linear layout using LinearLayout ll=(LinearLayout)rl.findViewById(R.id.linear_layout); I receive a NPE in this line. – Shivani Jul 08 '16 at 07:40
7

By the children of RecyclerView you mean number of rows?

Then I would suggest calling adapter's method. Call:

recyclerView.getAdapter().getItemCount();
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
2

I suppose you are using a LinearLayoutManager to show the list. It has a nice method called findViewByPosition that

saneesh
  • 137
  • 11
1

Rather late to problem posted. Don't think the answers addressed the problem correctly. A few years since the problem of "getChildCount under reporting visible items displayed" was reported, yet my research found no one had fingered out what was causing this problem surfacing in some circumstances.

I had the same problem recently which led me to investigate further. Here's my discovery.

Say if 9 items on RecyclerView are visible. Call getChildCount(). It should return 9, maybe 1 or 2 less/more depending on partially visible items at the top and bottom. This will be the result most of the time ... until the soft keyboard shows for some TextEdit input. If you call getChildCount() about 500 msecs after the method call that led to showing the keyboard, the result will be less than 9. It will be the count of items unobstructed by the keyboard view. Weird, even though user didn't change the displayed contents of the RecyclerView. What's weird too is that even after the keyboard is dismissed, and all 9 items are again visible, calling getChildCount() will still return the incorrect under count! This happened with Android 11, and presumably with pre-11s (didn't test with post-11s).

A clue to solving this is the method RecyclerView.postInvalidateDelayed. If you really need to have the correct getChildCount number to work with, do something like this (after the keyboard is dismissed):

   myRecyclerView.invalidate();
   myRecyclerView.postDelayed(myRunnable, 200); 

Subtle problem!

Android folks at Google .. please note and address this bug.

Ice Mann
  • 11
  • 3
0

There is a method called getItemCount() in LayoutManager, which returns exactly what you need. Basically it does what has been suggested in other answers.

buellas
  • 321
  • 4
  • 21
-1

Hey if I understood your question correctly, You are trying to retrieve the value in the EditText on button click. if you want to retrieve from all visible view holders, do this

View v;
int itemCount = recyclerview.getChildCount();
for(int i = 0; i < itemCount; i++){
    v = recyclerview.getChildAt(i); /* assuming you have your EditText in a view group like LinearLayout*/
    EditText et = v.findViewById(R.id.my_text);
    String text = et.getText().toString()
    // todo process text
}
CleverChuk
  • 141
  • 1
  • 6