2016-05-16 33 views
-2

我可能擁有自己的類「Torrent」,其中包含一些關於我在專用PC上的uTorrent下載的數據。每秒鐘在後臺更新ArrayList<Torrent> torrents。 所以,我有一個像已知模式的RecyclerView.Adapter。 請給我解釋一下,爲什麼代碼是可以正常使用:ViewHolder的onBindViewHoleder()方法的邏輯?

@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)); 
    ... 
} 

但在這種情況下不工作?它只是更新的開始和不改變多個第一值:

@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)); 
    ... 
} 

回答

0

適配器寫一個方法內爲您的主要活動

public class MainActivity extends Activity 
{ 
    ... 
    ... 
mAdapter.addItems(torrentList); 
    ... 
} 
+0

不工作如下圖所示

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(); } ..... ..... } 

..仍然沒有改變... – rbaloo