tôi đã thực hiện một phong tục Listview
(Nếu không có trọng getView()
phương pháp) với mỗi mục trong một listview có một Layout sauForce listview không để tái sử dụng views (Checkbox)
contactlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1">
<CheckBox android:id="@+id/checkBox1" android:text="CheckBox" android:layout_width="134dp" android:layout_height="108dp" android:focusable="false"></CheckBox>
<LinearLayout android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical" android:layout_width="87dp" android:layout_weight="0.84" android:weightSum="1" >
<TextView android:text="TextView" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/name" android:layout_weight="0.03"></TextView>
<TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/phone"></TextView>
<TextView android:text="TextView" android:layout_height="wrap_content" android:layout_weight="0.03" android:layout_width="match_parent" android:id="@+id/contactid" android:visibility="invisible"></TextView>
</LinearLayout>
</LinearLayout>
tôi đang điền vào số Listview
bằng cách sử dụng SimpleCursorAdapter
theo cách sau ...
Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String from[] = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
int to[] = new int[]{R.id.name,R.id.phone,R.id.contactid};
SimpleCursorAdapter s = new SimpleCursorAdapter(this,R.layout.contactlayout,c,from,to);
lv.setAdapter(s);
Khi bấm vào nút Tôi đang đọc các trạng thái của tất cả các hộp kiểm. Vấn đề là, nếu tôi kiểm tra một CheckBox
một số người khác xuống dòng sẽ tự động được kiểm tra. Tôi biết điều này đang tái sử dụng Chế độ xem. Làm thế nào để tránh nó? Tôi thậm chí không ghi đè phương pháp getView()
trong trường hợp này, vì vậy tôi tự hỏi liệu có cách nào để đạt được những gì tôi muốn không?
trả lời
Cuối cùng tôi thực hiện những gì @sastraxi gợi ý ...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
final TextView name = (TextView) view.findViewById(R.id.name);
final TextView contactId = (TextView) view.findViewById(R.id.contactid);
final int pos = position;
checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(checkBox.isChecked())
{
checkList.add(String.valueOf(pos));
nameList.add(name.getText().toString());
contactList.add(contactId.getText().toString());
Log.i("Chk added",String.valueOf(pos));
}
else
{
checkList.remove(String.valueOf(pos));
nameList.remove(name.getText().toString());
contactList.remove(contactId.getText().toString());
Log.i("Un Chk removed",String.valueOf(pos));
}
}
});
if(checkList.contains(String.valueOf(pos)))
{
checkBox.setChecked(true);
}
else
{
checkBox.setChecked(false);
}
return view;
}
Chúng ta có thể thấy bất kỳ bộ xử lý nhấp chuột gắn liền với 'ListView'? – sastraxi
@sastraxi: chưa thêm bất kỳ Trình nghe nào vào 'Listview', tôi chỉ đọc trạng thái của tất cả các hộp kiểm khi người dùng nhấp vào nút. –
Cảm ơn bạn rất nhiều. Nó cũng giải quyết được vấn đề với các nút Toggle. – zawhtut