在listview中遇到联系困难

时间:2012-10-11 07:11:14

标签: android android-emulator contacts contactscontract

我试图通过listview checkboxes获取手机的所有联系人,以便一次选择多个联系人。我编写了一些代码,这些代码创建的行数与emulator中的联系人数相同。但问题是联系人的姓名和号码没有显示,如附图所示。此外,当我点击显示联系人按钮以显示Toast中所选择的联系人时,也没有发生。

enter image description here

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" >

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

<TextView
    android:id="@+id/cConName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/checkBox1"
    android:layout_marginLeft="15.0dip"
    android:layout_toRightOf="@+id/checkBox1"
    android:text="Small Text"
    android:textAppearance="?android:textAppearanceSmall" />

<TextView
    android:id="@+id/cConNum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/cConName"
    android:layout_marginLeft="15.0dip"
    android:layout_toRightOf="@id/checkBox1"
    android:text="Medium Text"
    android:textAppearance="?android:textAppearanceMedium" />

</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="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/btnShow" >
</ListView>

</RelativeLayout>

ContactsPicker.java

public class ContactsPicker extends ListActivity {

protected Object mActionMode;
public int selectedItem = -1;
private Button btnShowContacts;
private ListView myListView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts_picker);
            myListView = getListView();
    btnShowContacts = (Button) findViewById(R.id.btnShow);
    btnShowContacts.setOnClickListener(new View.OnClickListener() {
        public void onClick (View v){
            Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
            String name = null;
            String number = null;
            long [] ids = myListView.getCheckedItemIds();
            for(long id : ids) {
                Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, null);
                while(contact.moveToNext()){
                    name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    number = contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                Toast.makeText(getApplicationContext(), "Name: " +name + "\n" + "Number: " + number , Toast.LENGTH_LONG).show();
            }
        }
    });
    ArrayList<Map<String, String>> list = buildData();
    String[] from = { "name", "purpose" };
    int[] to = { R.id.cConName, R.id.cConNum };
    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,null);
    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, null);
            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();
    startManagingCursor(people);        
    return list;
  }
}

2 个答案:

答案 0 :(得分:0)

  

但问题是联系人的姓名和号码不是   正如您在附图中看到的那样出现

首先,我猜您检查过ArrayList方法list buildData中的实际值。其次,在将值添加到"Name"时,使用键名"Phone"HashMap,但在from中使用的SimpleAdapter字符串数组中您使用值"name""purpose"。在from字符串数组中,需要具有与HashMap中相同的密钥。

  

此外,当我点击显示联系人按钮以显示所选内容时   Toast中的联系人也没有发生。

首先,当您拥有自定义行视图时,无法对已检查项目使用ListView的默认机制。如果您想获得已检查的项目,您需要自己管理行中CheckBox的状态。 getCheckedItemIds()方法不会返回有效的内容,并且查询将无法返回任何有效结果。相反,您应该使用HashMap方法实现自己的自定义适配器并返回行SimpleAdapter.getItem(包含名称和电话)。这样您就可以避免再次查询联系人。

答案 1 :(得分:0)

call notifyDataSetChanged();您需要刷新列表..