我是新來的Android,我試圖做一個簡單的應用程序,我必須在應用程序的ListView中顯示電話簿中可用的聯繫人列表,我嘗試添加複選框t每個項目。當用戶選中複選框時,我想要檢索該項目。到目前爲止,我能夠顯示列表,但不清楚如何檢查複選框檢查事件。Android:如何點擊ListView中的複選框時獲取事件?
我的代碼如下:
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@android:id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight = "1"
android:choiceMode="multipleChoice" android:clickable="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id ="@+id/savebutton"
android:text = "save"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@android:id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight = "1"
android:choiceMode="multipleChoice" android:clickable="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id ="@+id/savebutton"
android:text = "save"
/>
</LinearLayout>
源代碼:
public class testcontacts extends ListActivity
{
private ListAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//CheckBox box = (CheckBox)findViewById(R.id.checkbox);
//box.setFocusable(false);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
adapter = new SimpleCursorAdapter
(this, R.layout.contact_entry, cur,new String[] {ContactsContract.Contacts.DISPLAY_NAME}, new int[] {R.id.contactEntryText});
setListAdapter(adapter);
ListView list = getListView();
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
你好Soorya,謝謝你的回覆,但我該如何捕捉事件?如何知道何時選中/取消選中複選框? – Madz 2011-04-24 13:11:48