我一個定製Listview
(無重寫getView()
法)與每個項目在ListView具有以下佈局力列表視圖不重用次(複選框)
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>
我我通過以下方式使用SimpleCursorAdapter
填充Listview
...
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);
點擊一個按鈕我正在閱讀所有複選框的狀態。問題是,如果我檢查一個CheckBox
下線的其他人自動檢查。我知道這是重複使用視圖。我如何避免它?在這種情況下,我甚至不會重寫getView()
方法,所以我想知道是否還有辦法實現我想要的?
回答
最後我實現什麼建議...... @sastraxi
@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;
}
我們可以看到連接到'ListView'任何單擊處理這兩種方法? – sastraxi 2012-03-25 06:09:50
@sastraxi:沒有添加任何Listener到'Listview',我只是讀取所有複選框的狀態,當用戶點擊一個按鈕。 – 2012-03-25 06:11:54
非常感謝。它也解決了切換按鈕的問題。 – zawhtut 2012-08-10 12:48:00