0

I have a RecyclerView which displays two pieces of data in each row.

Both are from a List Players.

What I need is to update the second piece of data (which is a counter, an int) each time an element is clicked.

Basically I have this players List modified, but don't know how to put it back in to the RecyclerView edited.

My Adapter Code

TextView name;
    TextView counter;

    CoursesViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        name = (TextView) itemView.findViewById(R.id.textName);
        counter = (TextView) itemView.findViewById(R.id.textCounter);
    }

[...]

@Override
public void onBindViewHolder(CoursesViewHolder holder, int position) {
    Player player = mArrayCourses.get(position);
    holder.name.setText(player.getName());
    holder.counter.setText(String.valueOf(player.getCount()));
}

What I've tried

adapter = new CoursesAdapter(players);
            myList.setAdapter(adapter);
            myList.invalidate();

Edit: // Piece of code of the activity where my RecyclerView is

 myList = (RecyclerView) findViewById(R.id.playersVote);
    myList.setLayoutManager(new LinearLayoutManager(this));
    adapter = new CoursesAdapter(players);
    myList.setAdapter(adapter);


    // RecyclerView with a click listener

    CoursesAdapter clickAdapter = new CoursesAdapter(players);

    clickAdapter.setOnEntryClickListener(new CoursesAdapter.OnEntryClickListener() {
        @Override
        public void onEntryClick(View view, int position) {
            // Let each player vote (ghosts too)
            Player player = players.get(position);
            player.incrementCount();
            //Toast.makeText(ListPlayersVote.this, String.valueOf(player.getCount()), Toast.LENGTH_SHORT).show();
            votes++;
FET
  • 942
  • 4
  • 13
  • 35
  • You see this question i have just answered [link](http://stackoverflow.com/questions/38717343/how-to-update-a-edittext-value-when-another-edittext-value-changed-inside-listvi/38717625#38717625) its more or less similar to your scenario in case if you find difficulty i will add an answer for your specific use case. – Manikanta Aug 02 '16 at 10:53

2 Answers2

0

Every time that your data change you should notify those changes in the adapter, using notifyDataSetChanged()

notifyDataSetChanged(): Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

like this:

adapter = new CoursesAdapter(players);
myList.setAdapter(adapter);
....
....
adapter.notifyDataSetChanged();
Miguel Benitez
  • 2,322
  • 10
  • 22
0

Use the below code to change the particular Item in a recyclerview

 public void updatedelivact(String value,int index) {

            ChatMessage chatMessage = getItem(index);    
                Collection<Integer> readIds = chatMessage.getDeliveredIds();
                readIds.add(userid);
                chatMessage.setReadIds(readIds);  

    //To change the perticular item   
            notifyItemChanged(index);

        }
MathankumarK
  • 2,717
  • 1
  • 17
  • 34