我有一个“ArrayList”来枚举数据,但是当我尝试在我的onBindViewHolder中获取数据时,我的位置不正确,向上/向下滚动
@Override
public void onBindViewHolder(CAPusheenAdapter_Fragment_0_3.ViewHolder viewHolder, int position) {
CustomItem_0_ALL pusheen = pusheenArrayList.get(position);
for(int i=0;i<pusheen.getAllStrings().size();i++){
// correct data
/*viewHolder.allTextViews.get(0).setText("melón");
viewHolder.allTextViews.get(1).setText("melón");
viewHolder.allTextViews.get(2).setText("melón");
viewHolder.allTextViews.get(3).setText("melón");
viewHolder.allTextViews.get(4).setText("melón");*/
// random position
viewHolder.allTextViews.get(i).setText("melón");
}
** Note Edited 29/03/2016 11:42:public ArrayList&lt; TextView&gt; allTextViews;
我无法理解为什么,我尝试setHasStableIds(true);但没什么。
有什么想法吗?
如果不是,我会直接向谷歌发表评论。
我发现了问题:
03-29 06:03:08.683 11716-11716/com.xx.aplicacion D/-POSITIONS CURRENTS->: 0 _____> com.xx.aplicacion.Adapters.CustomItem_0_ALL@b0bcdf8
03-29 06:03:08.686 11716-11716/com.xx.aplicacion D/-POSITIONS CURRENTS->: 1 _____> com.xx.aplicacion.Adapters.CustomItem_0_ALL@c8ac136
03-29 06:03:08.692 11716-11716/com.xx.aplicacion D/-POSITIONS CURRENTS->: 2 _____> com.xx.aplicacion.Adapters.CustomItem_0_ALL@12bd1a4
03-29 06:03:10.549 11716-11716/com.xx.aplicacion D/-POSITIONS CURRENTS->: 3 _____> com.xx.aplicacion.Adapters.CustomItem_0_ALL@cb9bac2
03-29 06:03:10.582 11716-11716/com.xx.aplicacion D/-POSITIONS CURRENTS->: 4 _____> com.xx.aplicacion.Adapters.CustomItem_0_ALL@9266410
03-29 06:03:10.599 11716-11716/com.xx.aplicacion D/-POSITIONS CURRENTS->: 5 _____> com.xx.aplicacion.Adapters.CustomItem_0_ALL@eecc10e
03-29 06:03:10.628 11716-11716/com.xx.aplicacion D/-POSITIONS CURRENTS->: 6 _____> com.xx.aplicacion.Adapters.CustomItem_0_ALL@5f5b13c
03-29 06:03:13.105 11716-11716/com.xx.aplicacion D/-POSITIONS CURRENTS->: 2 _____> com.xx.aplicacion.Adapters.CustomItem_0_ALL@12bd1a4
03-29 06:03:13.145 11716-11716/com.xx.aplicacion D/-POSITIONS CURRENTS->: 1 _____> com.xx.aplicacion.Adapters.CustomItem_0_ALL@c8ac136
03-29 06:03:13.178 11716-11716/com.xx.aplicacion D/-POSITIONS CURRENTS->: 0 _____> com.xx.aplicacion.Adapters.CustomItem_0_ALL@b0bcdf8
尝试获取数据时会出现错误,因为列表与上下不一样
答案 0 :(得分:1)
在Recycler Adapter的构造函数中:
ArrayList<Item> itemList = new ArrayList<>();
itemList.add(new FruitItem("Melon"));
在项目中:
public class FruitItem extends Item {
private String fruit;
public FruitItem(String fruit) {
super();
this.fruit = fruit;
}
@Override
public void bindToViewHolder(RecyclerView.ViewHolder viewholder) {
FruitViewHolder holder = (FruitViewHolder) viewholder;
holder.title.setText(fruit);
}
}