I have a custom arrayadapter and I want to add an onclicklistener for a button in each one of its rows, when I click on the button I want the image resource to change, everything works fine except that when I click on a button the image changes but the image of another button in an other row also changes. Thanks for your help ! Here is my code:
public class Coursadapter extends ArrayAdapter<String>{
Context context;
int layoutResourceId;
ArrayList<String> data = null;
WeatherHolder holder;
public Coursadapter(Context context, int layoutResourceId, ArrayList<String> data) {
// super(context, layoutResourceId, data, coeff);
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.name = (TextView)row.findViewById(R.id.item_cours_name);
holder.b=(ImageButton) row.findViewById(R.id.button);
holder.b.setTag(holder);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
holder.b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
WeatherHolder w = (WeatherHolder) v.getTag();
w.b.setImageResource(R.drawable.butgreen);
}
});
String name1 = data.get(position);
holder.name.setText(name1);
return row;
}
static class WeatherHolder
{
TextView name;
ImageButton b;
}
}