1

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:

  1. Separate logic. Implement a method in Adapter (probably in onBindViewHolder), which get data from EditText(entered by user). So logic about views should be in Adapter. And in Activity just call this method (like getAdatper().), when user presses a save button. So Activity doesn't know anything about views (what's is going on in Adapter).

  2. 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.

nazkord
  • 53
  • 7

1 Answers1

0

I found it easier to use TextWatcher in ViewHolder method in Adapter, which get data from EditText, at the moment when those EditText's are changed. Add this data to ArrayList in Adapter of Bets and then use this ArrayList (which is public) in Activity

nazkord
  • 53
  • 7