2
我試圖查詢數據庫並顯示聯繫人和他們所有的電話號碼,每個電話號碼一行返回聯繫人姓名:Android通訊錄查詢多項
活動文件:
private Cursor getContacts() {
Uri uri = Data.CONTENT_URI;
String[] fields = new String[] {
Data._ID,
Phone.NUMBER,
Data.DISPLAY_NAME,
Phone.LABEL,
Phone.TYPE,
};
String sortOrder = Data.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, fields, null, null, sortOrder);
}
private void populateContactList() {
Cursor cursor = getContacts();
String[] fields = new String[] {
Data.DISPLAY_NAME,
Phone.NUMBER,
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_details, cursor,
fields, new int[] {R.id.contactNameText, R.id.contactNumberText });
mContactList.setAdapter(adapter);
}
我得到我需要的所有信息,但同時也顯示每個聯繫人的名字兩次行的一個(好像是電話號碼):
有人知道我在做什麼錯嗎?任何幫助將不勝感激!
[以下是XML文件(但不認爲他們是問題的根源):
//contact_list.xml <ListView android:layout_width="fill_parent"
android:id="@+id/contactList"
android:layout_height="wrap_content"
android:layout_weight="1"/>
//contact_details.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="@+id/contactNumber"
android:id="@+id/contactNumberText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dip"
/>
<TextView android:text="@+id/contactName"
android:id="@+id/contactNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/contactNumberText"
/>
順便說一句你應該閱讀手冊的這一部分http://developer.android.com/reference/android/provider/ContactsContrac t.Data.html –
感謝您的回答。我確實讀過那部分內容,但是我沒有關注這個特定的行,我認爲只需要查詢單個用戶的配置文件(因爲這是給出的示例...) – Eric