I have an Activity
, which has a RecyclerView
and save Button
. Each item (football match) of the RecyclerView
contains some text (name of teams) and EditText
, where user enters data (actually enters a score of the match). My goal is to save those scores to the list in the Activity
, when user clicked on save button.
I read the similar question (How can I return data from a RecyclerView adapter?), the difference is that I need to get data from editText, not from checkBox(which is easy to check by clickListeners in onBindViewHolder
method in Adapter
)
So I made it on my way and it's works. I implemented this method, which actually get particular item from LinearLayoutManager
and then get data from EditText
.
private List<Bet> getNewUserBets() {
View betView;
Bet betItem;
EditText userBet1;
EditText userBet2;
int numberOfMatches = rvMatchesAdapter.getItemCount();
List<Bet> bets = new ArrayList<>();
for (int i = 0; i < numberOfMatches; i++)
{
betItem = rvMatchesAdapter.getItem(i);
betView = linearLayoutManager.findViewByPosition(i);
userScore1 = (EditText) betView.findViewById(R.id.result1);
userScore2 = (EditText) betView.findViewById(R.id.result2);
//here i checked whether the editText is empty and other stuff
bets.add(betItem);
}
return bets;
}
But I was wondering if I made this on a good way. In my opinion there are two approach:
Separate logic. Implement a method in
Adapter
(probably inonBindViewHolder
), which get data fromEditText
(entered by user). So logic about views should be inAdapter
. And inActivity
just call this method (likegetAdatper().
), when user presses a save button. SoActivity
doesn't know anything about views (what's is going on inAdapter
).Like I did. Make whole logic in
Activity
, which in my opinion is worse than approach described at number 1.
So in which way should I solve this task? And if 1 method is better, how should I get data from EditText
directly from Adapter
. Or maybe there is another approach(solution) to make this done. I would really appreciate any help.