In last week, i just try to resolve this problem. see every similar questions and i don't know why, solutions don't work for me. I have a list view and each item has an image view. when clicked on image view , i want to change image resource. well. all done. but when scrolling, image resources change. my full code is:
public class CustomBaseAdapter extends BaseAdapter {
Context context;
ArrayList<String> items;
public CustomBaseAdapter(Context context,ArrayList<String> items){
this.context = context;
this.items = items;
}
private class ViewHolder{
TextView titr;
ImageView image;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return items.hashCode();
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position));
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
}
});
return vi;
}
}
where i have mistake?
you have to use one boolean value in holder to check whether your background image is set or not for particular row.
holder
private class ViewHolder{
TextView titr;
ImageView image;
boolean isSet;
}
set image
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
holder.isSet = true
}
});
if(holder.isSet)
{
holder.image.setBackgroundResource(R.drawable.fav_ic);
}
else{
holder.image.setBackgroundResource(no image or other image);
}
hope this will help.
try this,
public class CustomBaseAdapter extends BaseAdapter {
Context context;
ArrayList<Items> items;
public CustomBaseAdapter(Context context,ArrayList<Items> items){
this.context = context;
this.items = items;
}
private class ViewHolder{
TextView titr;
ImageView image;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Items getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return items.hashCode();
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position).getStrTxt());
holder.image.setImageResource(items.get(position).getDrawableImage());
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
items.set(position,new Items(R.drawable.fav_ic));
notifyDataSetChanged();
}
});
return vi;
}
}
Add Model Class :
public class Items {
int drawableImage;
String strTxt;
public Items(int drawableImage) {
this.drawableImage = drawableImage;
}
public int getDrawableImage() {
return drawableImage;
}
public void setDrawableImage(int drawableImage) {
this.drawableImage = drawableImage;
}
public String getStrTxt() {
return strTxt;
}
public void setStrTxt(String strTxt) {
this.strTxt = strTxt;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With