Reputation: 4187
Using recyclerview and first initialising the listview it begins to go through each item 1,2,3,4,5 but then suddenly the position value reverts back to 0 and getting the text values from the data items already used., thus my data in the list is looping, to look like:
0,1,2,4,5,0,1,2,3,4,5
I'm using a ViewHolder, i'm using the position that comes from the ViewHolder, when debugging the items list size is constant and the values correct. Everything seems correctly implemented yet, i presume it's recycling values or something? Before i've even scrolled or tapped anything.
public class AnswerAdapter extends RecyclerView.Adapter<AnswerAdapter.NumberViewHolder> {
List<NumberItem> items;
private SparseBooleanArray selectedItems;
public AnswerAdapter() {
setHasStableIds(true);
SparseBooleanArray localSparseBooleanArray = new SparseBooleanArray();
this.selectedItems = localSparseBooleanArray;
ArrayList localArrayList = new ArrayList();
this.items = localArrayList;
this.items.addAll(AnswerListContent.ITEMS);
}
public void clearSelections() {
this.selectedItems.clear();
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return this.items.size();
}
public NumberItem getItem(int pos) {
return this.items.get(pos);
}
public int getSelectedItemCount() {
return this.selectedItems.size();
}
public List<Integer> getSelectedItems() {
ArrayList localArrayList = new ArrayList(this.selectedItems.size());
for (int i = 0; i < this.selectedItems.size(); i++) {
localArrayList.add(Integer.valueOf(this.selectedItems.keyAt(i)));
}
return localArrayList;
}
@Override
public void onBindViewHolder(NumberViewHolder paramNumberViewHolder, int paramInt) {
NumberItem localNumberItem = items.get(paramNumberViewHolder.getPosition());
paramNumberViewHolder.text.setText(localNumberItem.num.toString());
if (this.selectedItems.get(paramInt, false)) {
paramNumberViewHolder.text.setBackgroundResource(R.drawable.answer_bg_selected);
} else {
paramNumberViewHolder.text.setBackgroundResource(R.drawable.answer_bg);
}
}
public NumberViewHolder onCreateViewHolder(ViewGroup paramViewGroup, int paramInt) {
View localView = LayoutInflater.from(paramViewGroup.getContext()).inflate(R.layout.list_item_answer, paramViewGroup, false);
NumberViewHolder localNumberViewHolder = new NumberViewHolder(localView);
return localNumberViewHolder;
}
public void removeItem(int paramInt) {
this.items.remove(paramInt);
}
public void swapPositions(int paramInt1, int paramInt2) {
Collections.swap(this.items, paramInt1, paramInt2);
}
public void toggleSelection(int pos) {
if (selectedItems.get(pos, false)) {
selectedItems.delete(pos);
} else {
selectedItems.put(pos, true);
}
notifyItemChanged(pos);
}
public final static class NumberViewHolder extends RecyclerView.ViewHolder {
TextView text;
public NumberViewHolder(View localView) {
super(localView);
this.text = ((TextView) localView.findViewById(R.id.title));
}
}
}
Upvotes: 2
Views: 7655
Reputation: 4187
Solved! For the help of anyone who comes across this issue also, the problem was:
setHasStableIds(true);
Which I set in the constructor.
It turns out I originally had overridden 'getItemID' ( Which would have allowed the list to work ) however I later removed it in favour of a different approach and FORGOT to remove the aforementioned hasStableId's!
Upvotes: 6
Reputation: 38243
The second argument to onBind is the position that you should bind.
paramInt = position
so you should be calling
items.get(position);
not
items.get(paramNumberViewHolder.getPosition());
You are probably receiving a stale position. ViewHolder#getPosition is valid for ViewHolders which are already bound.
Upvotes: 0