android 联系人查询以数字形式返回联系人姓名

发布于 2024-11-25 05:48:59 字数 2036 浏览 2 评论 0原文

我正在尝试查询数据库并显示联系人及其所有电话号码,每个电话号码一行:

活动文件:

 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);
}

我获得了所需的所有信息,但它还显示每个联系人姓名两次(就好像它是电话号码一样)在其中一行中:

snapshot

有谁知道做错了什么?任何帮助将不胜感激!

[这里是 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"
          />                            

Am trying to query the database and display contacts and all their phone numbers, a row for each phone number:

Activity file:

 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);
}

I get all the info I need, but it also shows every contacts name twice (as if it were the phone number) in one of the rows:

snapshot

Does anyone know what am doing wrong? any help will be appreciated!

[Here are the XML files (although don't think they are the source of the problem)]:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2024-12-02 05:48:59

尝试使用此代码代替您的方法

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, Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE+"'", null, sortOrder);

}

Try this code instead your method

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, Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE+"'", null, sortOrder);

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文