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 bet the match). My goal is to save those scores to the list in the Activity
, when user clicked on save Button
.
I implemented this method in Activity
, which actually get particular item from LinearLayoutManager
and then get data from EditText
, but the findViewByPosition()
method (or getChildAt() there are different way to do this) sometimes returns null.
I saw another answers on similar question, but they didn't help me. Maybe my strategy is wrong, because I made whole my logic in Activity (better to do this in Adapter?) and I get though all of my items in RecyclerView
even if only in one user entered the score.
private List<Bet> getUserBets() {
View betView;
Bet betItem;
EditText userBet1;
EditText userBet2;
int numberOfMatches = rvBetsAdapter.getItemCount();
List<Bet> bets = new ArrayList<>();
for (int i = 0; i < numberOfMatches; i++)
{
betItem = rvBetsAdapter.getItem(i);
betView = linearLayoutManager.findViewByPosition(i);
userScore1 = (EditText) betView.findViewById(R.id.result1); // <- NPE appears
userScore2 = (EditText) betView.findViewById(R.id.result2);
//here i checked whether the editText is empty and other stuff
bets.add(betItem);
}
return bets;
}
How do I fix it? I suppose I should do something in onBindViewHolder method in my adapter, but what exactly? I would really appreciate any help.
You should add to your Bet
model variable which holds EditText
value for example editTextValue
.
And access it using list in adapter list like this end use EditText
value from there. rvBetsAdapter.getItem(i).editTextValue
editTextValue
can be set using TextWatcher.afterTextChanged()
callback