Neil M.
Neil M.

Reputation: 689

Getting text from inside editText that is contained in a Recyclerview

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.

Upvotes: 2

Views: 4803

Answers (2)

Eldhose M Babu
Eldhose M Babu

Reputation: 14520

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.

Upvotes: 7

yshahak
yshahak

Reputation: 5096

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());
}

Upvotes: 5

Related Questions