In my case i have two text view and one check box in listview. I am using ArrayAdapter class to set the data in textviews.
When i select any check box and scroll in list view some check boxes are automatically selected.I am not getting why is this happening below is my code please look the code and give the solution to solve this out.
public class Contacts_NumberActivity extends Activity {
/** Called when the activity is first created. */
String nameList[] = { "U", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "L", "M", "N", "O", "P", "Q" }, numberList[] = { "1",
"2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4",
"5", "6", "7" };
ListView contactList;
boolean[] checked;
ContactHolder holder = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contactList = (ListView) findViewById(R.id.contact_list);
contactList.setAdapter(new ContactAdapter(this, R.layout.list_item,
nameList, numberList));
}
public class ContactAdapter extends ArrayAdapter {
Context context;
int layoutResourceId;
String data[] = null;
String data1[] = null;
public ContactAdapter(Context context, int layoutResourceId,
String[] data, String[] data1) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
this.data1 = data1;
}
@Override
public View getView(final 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 ContactHolder();
holder.name = (TextView) row.findViewById(R.id.name);
holder.number = (TextView) row.findViewById(R.id.number);
holder.chkbox = (CheckBox) row.findViewById(R.id.checkBox1);
holder.chkbox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
holder.chkbox.setChecked(isChecked);
System.out.println("Checked possition= "
+ isChecked);
}
});
row.setTag(holder);
} else {
holder = (ContactHolder) row.getTag();
}
holder.name.setText(data[position]);
holder.number.setText(data1[position]);
return row;
}
public class ContactHolder {
TextView name, number;
CheckBox chkbox;
}
}
}