I have list item like this
and when i click on the lock image for this list item the image will be converted into this
it's working and changes the image of the current list item as expected but the problem is it is also change the image of other rows this is due to recycling the list view.. https://stackoverflow.com/a/14108676/5989049 so is there any thing i can do so that the item click on the image only affect the row that was clicked.
this my code for the adapter
public class WordAdapter extends ArrayAdapter<Word> {
public WordAdapter(Activity context, ArrayList<Word> words) {
super(context, 0, words);
}
@Override
public View getView(final int position, @Nullable final View convertView, @NonNull final ViewGroup parent) {
View listItem = convertView;
if (listItem == null) {
listItem = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
final Word word = getItem(position);
String englishWord = null;
if (word != null) {
englishWord = word.getEnglishWord();
}
String arabicWord = null;
if (word != null) {
arabicWord = word.getArabicWord();
}
TextView englishText = listItem.findViewById(R.id.english_text_view);
TextView arabicText = listItem.findViewById(R.id.arabic_text_view);
ImageView mUnLocked = listItem.findViewById(R.id.unlock);
mUnLocked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!word.isLocked()) {
word.setLocked(true);
ImageView icon = (ImageView) view;
icon.setImageResource(R.drawable.ic_lock_outline_black_24dp);
icon.setVisibility(View.VISIBLE);
} else {
ImageView icon = (ImageView) view;
icon.setImageResource(R.drawable.ic_lock_open_black_24dp);
icon.setVisibility(View.VISIBLE);
}
}
});
Typeface enTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontfamily.ttf");
Typeface arTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/ShahdRegular.ttf");
englishText.setTypeface(enTypeface);
arabicText.setTypeface(arTypeface);
englishText.setText(englishWord);
arabicText.setText(arabicWord);
return listItem;
}
}