0

我想保留一次选择多个联系人号码的能力,使用checkbox. 我使用了一个带有两个textview和一个的自定义布局checkbox来为列表创建一行。我已经成功地用联系人填写了列表,但是当用户单击完成按钮时,我无法配置如何检索已选中(勾选)联系人的电话号码。请指导我。

截屏

custcontactview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5.0px"
android:paddingLeft="5.0px"
android:paddingTop="5.0px" >

<TextView
    android:id="@+id/txtContactName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15.0dip"
    android:layout_toLeftOf="@+id/checkBox1"
    android:layout_alignParentLeft="true"
    android:text="Medium Text"
    android:textAppearance="?android:textAppearanceMedium" />

<TextView
    android:id="@+id/txtContactNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtContactName"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="15.0dip"
    android:layout_toLeftOf="@+id/checkBox1"
    android:text="Small Text"
    android:textAppearance="?android:textAppearanceSmall" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false" />

</RelativeLayout>

activity_contacts_picker.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/btnShow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Selected" />

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:layout_below="@+id/btnShow" >
</ListView>

</RelativeLayout>

ContactsPicker.java

public class ContactsPicker extends ListActivity implements OnItemClickListener {

protected Object mActionMode;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts_picker);
    ArrayList<Map<String, String>> list = buildData();
    String[] from = { "Name", "Phone" };
    int[] to = { R.id.txtContactName, R.id.txtContactNumber };
    SimpleAdapter adapter = new SimpleAdapter(this, list,
            R.layout.custcontactview, from, to);
    setListAdapter(adapter);
}

private ArrayList<Map<String, String>> buildData() {
    ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
    list.clear();
    Cursor people = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null,
            "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
    while (people.moveToNext()) {
        String contactName = people.getString(people
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String contactId = people.getString(people
                .getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = people
                .getString(people
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
        if ((Integer.parseInt(hasPhone) > 0)) {
            Cursor phones = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = " + contactId,
                    null,
                    "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
                            + ") ASC");
            while (phones.moveToNext()) {
                String phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Map<String, String> NamePhoneType = new HashMap<String, String>();
                NamePhoneType.put("Name", contactName);
                NamePhoneType.put("Phone", phoneNumber);
                list.add(NamePhoneType);
            }
            phones.close();
        }
    }
    people.close();
    return list;
}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { }
}
4

1 回答 1

0

您应该为每个复选框设置一个标签以及一个 onCheckChanged 侦听器或类似的东西。当状态变为选中时,只需获取与该复选框关联的标签(这样您就会知道列表中的哪些元素被选中)。然后,您必须重新查询以获取该列索引处的电话号码。您已经在此处存储了您的电话号码

String phoneNumber = phones
                    .getString(phones
                                  .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
于 2012-10-13T18:06:46.310 回答