2012-10-13 68 views
0

我想保留一次使用checkbox來選擇多個聯繫號碼的功能。我使用了兩個textviewcheckbox的自定義佈局來爲列表創建一行。我已經成功地填寫了聯繫人列表,但我無法配置當用戶單擊完成按鈕時如何檢索選中(勾選)的聯繫人的電話號碼。請指導我。如何使用複選框從列表視圖中提取詳細信息?

screenshot

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> 

ContacsPicker.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) { } 
} 
+0

你需要重寫getView()方法,這樣就可以(比方說在ArrayList中)存儲信息時,該項檢查 –

+0

@ShashankKadne u能PLS完成我的代碼。我必須在星期一提交我的項目。而且還有很多工作要做。 –

回答

0

您應該爲每個複選框以及onCheckChanged偵聽器或類似的東西設置一個標記。當狀態更改爲檢查時,只需獲取與該複選框關聯的標記(以便您知道列表中的哪些元素已被選中)。然後,您必須重新查詢該列索引處的電話號碼。你已經在這裏存放你的電話號碼

String phoneNumber = phones 
        .getString(phones 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
+0

可以提供示例代碼嗎? –

相關問題