I have a simple listview inflated with custom view which changes colour when clicked. Issue is when I scroll the list the position of item is changed randomly. Here is my code to the adapter. Is there anything obvious in my getview method that I am missing? thanks for the help in advance.
public class HADevicesAdapter extends ArrayAdapter<HADevicesViews>{
private static final String HADevicesViews = null;
private final String TAG = HADevicesAdapter.class.getSimpleName();
private Context context;
private ArrayList<HADevicesViews> arrayList;
OnClickListener listner;
LayoutInflater inflater;
public HADevicesAdapter(Context context, ArrayList<HADevicesViews> arrayList, OnClickListener listner) {
// TODO Auto-generated constructor stub
super(context, R.layout.layout_endpoints_list_row,arrayList);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.listner = listner;
this.arrayList = arrayList;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
@Override
public HADevicesViews getItem(int position) {
// TODO Auto-generated method stub
return arrayList.get(position);
}
@Override
public int getPosition(HADevicesViews item) {
// TODO Auto-generated method stub
return arrayList.indexOf(item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
convertView = inflater.inflate(R.layout.layout_endpoints_list_row, parent,false);
viewHolder = new ViewHolder();
viewHolder.haDeviceView = (HADevicesViews) convertView.findViewById(R.id.onOffOutput);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.haDeviceView.setOnClickListener(listner);
return convertView;
}
static class ViewHolder{
HADevicesViews haDeviceView;
}
}