Я новичок в 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);
}
}