Skip to content Skip to sidebar Skip to footer

Item Position In Recyclerview And Sharedpreferences

I have a RecyclerView that when the user clicks any item it will open an activity which the user must finish some tasks and if the user completes the tasks, it will be recorded in

Solution 1:

check out this snippet, add to the bottom of onBindViewHolder

booleanletter1= prefs1.getBoolean("letter1", false);
booleanletter2= prefs2.getBoolean("letter2", false);

if ((letter1 || letter2) && position==1) {
    holder.itemTrophy.setVisibility(View.VISIBLE);
}
else holder.itemTrophy.setVisibility(View.GONE);

check out how RecyclerView works and what is recycling in terms of Android. in short: Views/list items are reused when some item scrolls outside screen then same View enters from another side (scrolled-out to top will be shown at the bottom as next list item). you aren't setting View.GONE so recycled list item still have View.VISIBLE for your trophy icon

edit:

simple way

if (position==1){
    holder.itemTrophy.setVisibility(letter1 ? View.VISIBLE : View.GONE);
}

more sophisticated

SharedPreferencesprefs= context.getSharedPreferences("prefs" + (position+1), MODE_PRIVATE);
booleanletterSet= prefs.getBoolean("letter" + (position+1), false);
holder.itemTrophy.setVisibility(letterSet ? View.VISIBLE : View.GONE);

Post a Comment for "Item Position In Recyclerview And Sharedpreferences"