Eric
Eric

Reputation: 371

android contacts querying return the contact name as a number

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

Upvotes: 2

Views: 988

Answers (1)

Henry Pootle
Henry Pootle

Reputation: 1216

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

}

Upvotes: 1

Related Questions