-2

I have may own class "Torrent" which contains some data about my uTorrent downloads on dedicated PC. ArrayList<Torrent> torrents is updating in background every second. So, I have a RecyclerView.Adapter like known pattern. Please, explain me, WHY this code is working perfectly:

@Override
public void onBindViewHolder(final MyViewHolder holder,int position){
    ...
    // Update ETA textView
    holder.tvEta.setText(String.format(Locale.getDefault(),"%d h %d min %d s",torrents.get(position).getEta()/3600,(torrents.get(position).getEta()%3600)/60,torrents.get(position).getEta()%60));
    ...
}

But in this case NOT WORKING? It just update first value on start and not changing more:

@Override
public void onBindViewHolder(final MyViewHolder holder,int position){
    ...
        // Update ETA textView
        int secs = torrents.get(position).getEta();
        int hours = secs / 3600;
        int minutes = (secs % 3600) / 60;
        int seconds = secs % 60;
        holder.tvEta.setText(Locale.getDefault(), String.format("%d h %d min %d s", hours, minutes, seconds));
    ...
}
rbaloo
  • 47
  • 6

1 Answers1

0

inside adapter write one method as shown below

 public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements View.OnClickListener {

 ....
 ArrayList<Torrent> torrentList;

 public void addItems(ArrayList<Torrent> torrents)
 {
 torrentList = torrents;
 notifyDataSetChanged();
 }

@Override
public int getItemCount() {
return torrentList.size();
  }
.....
.....
}

in your main activity

public class MainActivity extends Activity
{
   ...
   ...
mAdapter.addItems(torrentList);
   ...
}
Mrugesh
  • 4,381
  • 8
  • 42
  • 84