2016-04-13 79 views
1

我有一個RecyclerView包含交互式內容的項目。 每個項目都包含一個每5秒更新一次的文本視圖,但是當RecyclerView中有5個以上的項目時,數據會混淆在一起。RecyclerView項目內容混合起來

例如:

正確: | 1 - 100 |

| 2 - 283 |

| 3 - 382 |

當它變混時:

| 2 - 283 |

| 1 - 100 |

| 3 - 382 |

它有點像,它只是混合起來。

當我調用de adapter.notifyDataSetChanged();

我的適配器:

公共類FavoritesAdapter擴展RecyclerView.Adapter {

private List<Favorites> channels; 
private Context context; 

public FavoritesAdapter(Context context, List<Favorites> channels) { 
    this.context = context; 
    this.channels = channels; 
} 

@Override 
public FavoritesViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.favoriteitem_layout, viewGroup, false); 
    FavoritesViewHolder pvh = new FavoritesViewHolder(v); 
    return pvh; 
} 

@Override 
public void onBindViewHolder(final FavoritesViewHolder holder, final int position) { 
    final DBHelper dbHelper = new DBHelper(context); 
    if (Data.isConnected(context)) { 
     Scheduler.getInstance().doAsync(new Scheduler.Task<Object, Channel>() { 
      @Override 
      public Channel run(Object... params) { 
       return Methods.getData(channels.get(position).getChannelId(), channels.get(position).getChannelName(), true, context); 
      } 
     }, new Scheduler.Executable<Channel>() { 
      @Override 
      public void execute(Channel result) { 

       if(result.getId() != "") { 
        if(!result.channelSubs.equalsIgnoreCase("-500_RTSS_ERROR")) { 
         holder.channelSubsView.setText(result.getSubs()); 
         holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName()) + "'s Suscribers"); 
         channels.get(position).setChannelSubs(result.getSubs()); 
         dbHelper.updateFavorites(channels.get(position)); 
        } 
        else { 
         holder.channelSubsView.setText(channels.get(position).getChannelSubs()); 
         holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName()) + "'s Suscribers (Cached)"); 
        } 
       } 
       else { 
        holder.channelSubsView.setText(channels.get(position).getChannelSubs()); 
        holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName()) + "'s Suscribers (Cached)"); 
       } 
      } 
     }); 
    } 
    else { 
     holder.channelSubsView.setText(channels.get(position).getChannelSubs()); 
     holder.channelTitleSubsView.setText(String.valueOf(channels.get(position).getChannelName()) + "'s Suscribers (Cached)"); 
    } 

    Picasso.with(context).load(channels.get(position).getChannelAvatar()) 
      .error(R.drawable.youtube_default_avatar) 
      .placeholder(R.drawable.youtube_default_avatar) 
      .into(holder.channelAvatarView); 

    holder.channelCardView.setOnClickListener(clickListener); 
    holder.channelCardView.setTag(holder); 
} 

View.OnClickListener clickListener = new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     FavoritesViewHolder holder = (FavoritesViewHolder) view.getTag(); 
     int position = holder.getPosition(); 

     Favorites channel = channels.get(position); 
     if(Data.isConnected(context)) { 
      Methods.getYouTubeData(channel.getChannelId(), channel.getChannelName(), channel.getChannelAvatar(), context); 
     } 
     else { 
      Toast.makeText(context, R.string.error_connection_no_internet, Toast.LENGTH_LONG).show(); 
     } 
    } 
}; 

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

@Override 
public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
    super.onAttachedToRecyclerView(recyclerView); 
} 

public static class FavoritesViewHolder extends RecyclerView.ViewHolder { 
    CardView channelCardView; 
    TextView channelSubsView; 
    TextView channelTitleSubsView; 
    ImageView channelAvatarView; 

    FavoritesViewHolder(View itemView) { 
     super(itemView); 
     channelCardView = (CardView) itemView.findViewById(R.id.channelCardView); 
     channelSubsView = (TextView) itemView.findViewById(R.id.subsAmount); 
     channelTitleSubsView = (TextView) itemView.findViewById(R.id.subsText); 
     channelAvatarView = (ImageView) itemView.findViewById(R.id.channelAvatarView); 
    } 
} 

}

+0

張貼一些代碼.. –

+0

張貼您的適配器類請 – Jeffalee

+0

@RohitArya完成! –

回答

0

您可以檢查您是否正在編輯的順序/傳遞給構造初始列表進行排序的適配器。

List<Favorites> channels = getChannels(); 
adapter = new FavoritesAdapter(context, channels); 
recyclerView.setAdapter(adapter) 
//  edit or sort *channels* 
adapter.notifyDataSetChanged(); 
0

您在onBindViewHolder製造asynchronous通話所以當異步任務的響應到來時,它讀取public void onBindViewHolder(final FavoritesViewHolder holder, final int position)錯誤位置,因爲由onBindViewHolder返回的時間位置已經改變

您可以做的是將位置作爲參數傳遞給async task,並在async task的結果中傳遞相同的位置。通過這種方式撥打電話p(說)將適用於p

除此之外,還嘗試在RecyclerViewHolder上使用getAdapterPosition而不是getPosition(不建議使用並造成混淆)。

+0

不工作,我會錄製一段視頻,向你展示它的外觀。 –

+0

更新你的代碼。 –

+0

這裏是視頻,看看發生了什麼:https://youtu.be/9KU2iKtMPCE我會盡快更新代碼。 –