I have a Recyclerview that contains an edittext, when a button is pressed outside the RecyclerView, I would like to retrieve the text from each view inside the RecyclerView. However I can't seem to find the best way to do that. I have tried to use a callback, and also have tried a external class to store the data but both ways seem like they should work but have not had much luck with either. What is the best practice for geting the text out of each edittext indivdual so that it can be a added to an array or database.
2 Answers
In recycle view, list view etc, there is a view recycling mechanism to reduce the memory consumption.
So simply iterating through recycle view childs won't give you the whole data.
For example if there are 10 items in recycle view and if 5 items are shown on the screen, then if you go with iterating through the recycle view childs, you will get only data associated with few children, say 6 or 7. Because for others the view will be reused.
So in your case what you need to do is to save the data from edittext to model or bean class at the time of view reusing in Recycle View adapter. When the button is clicked, then you can iterate through the childs and get the data and put that data again to model or bean class. Then from the bean list you can get the whole edited data.

- 14,382
- 8
- 39
- 44
You could retrieve the item with:
ArrayList<String> list = new ArrayList<>(); //this list will hold the Strings from the adapter Views
for (int i = 0 ; i < yourRecycle.getChildCount(); i++){
list.add(((EditText)yourRecycle.getChildAt(i)).getText().toString());
}

- 4,996
- 1
- 31
- 37
-
1Thanks this is what I was looking for. – Neil M. Jul 01 '15 at 20:07
-
1Pay attention to @Eldhose M Babu answer, it a really important point if you had several item in your recycleView – yshahak Jul 01 '15 at 20:16
-
1It's a bad solution in a case if not all children are visible. getChildCount() returns only number of visible children. So even if keyboard will cover a part of your recyclerView, you won't get that covered children. – Евгений Смирнов Aug 10 '16 at 21:54