1
在我的情況下,我有兩個文本視圖和一個複選框在列表視圖。我正在使用ArrayAdapter類來設置textviews中的數據。Android的一覽表與複選框
當我選擇任何複選框並在列表視圖中滾動時,某些複選框會自動選中。我不明白爲什麼下面發生這種情況是我的代碼,請查看代碼並給出解決方案以解決此問題。
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<String> {
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;
}
}
}
我想打印你的listview.please問題檢查你的listview它可能有數據重複,這就是爲什麼你感覺自動複選框得到選擇.check並讓我知道, – OnkarDhane 2012-02-28 07:04:33
這是發生在我單擊任何複選框並滾動列表時,下面的複選框也被選中。 – user03 2012-02-28 08:59:18
檢查下面的鏈接它可以幫助你http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php http://developmentality.wordpress.com/2010/11/05/of-rubber -stamps-and-checkboxes-why-your-listview-is-broken/ – 2012-04-17 13:27:14