2009-12-13 11 views
6

Kann jemand ein Licht auf, wie man Kontaktliste von Android bekommen ?.Android-Kontaktliste

Ich möchte nur die gleiche Liste wie in der Dialer-App erhalten. Aber ich bekomme viele Kontakte, die nicht auf der Dialer-Liste stehen, mit dem unten stehenden Code.

ContentResolver cr = getContentResolver(); 
Cursor cursor = cr.query(Contacts.People.CONTENT_URI, null, null, null, Contacts.ContactMethods.DEFAULT_SORT_ORDER); 
startManagingCursor(cursor); 

Vielen Dank im Voraus.

Antwort

2

Was Sie haben scheint gut. Könnten Sie näher ausführen, "viele Kontakte zu bekommen, die nicht auf der Dialer-Liste stehen"? Ist es, dass Android Leute ausmacht? Oder sehen Sie Leute mit E-Mail-Adressen, aber keine Telefonnummern (die daher möglicherweise nicht im Wähler angezeigt werden)?

Beachten Sie, dass Contacts.People für Android 1.6 und niedriger ist. Dieser Anbieter wird ab Android 2.0 nicht mehr unterstützt und durch den Anbieter ContactsContract ersetzt.

+0

ich viele Entwickler vorstellen, gehen mit nicht ContactsContract verwenden, weil sie 1,6 Kompatibilität wollen ... – Eno

0

Nun, danke für die Antwort zuerst. Nur um ein Licht darauf zu werfen.

Ich wollte nur E-Mails nur für die Kontakte auf meinem Handy erhalten. Die Gruppe "MyContacts". Ich habe gesehen, dass dies die Gruppe ContactList Activity ist, die verwendet wird.

ich fertig somethig wie dies zu tun:

c = cr.query(myGroupUri, mEmailsProjection, null, null, null); 
.... 

c.close(); 

c = cr.query(
    Contacts.ContactMethods.CONTENT_URI, 
     mContactsProjection, contactIds, null, null 
); 
.... 
c.close(); 

einfach die Gruppe abgefragt und dann die E-Mails Tabelle.

6

dieses Snippet Versuchen:

import android.app.ListActivity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.provider.ContactsContract.CommonDataKinds.Phone; 
import android.widget.SimpleCursorAdapter; 

public class ContactList extends ListActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, null); 

     startManagingCursor(cursor); 

     String[] from = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER}; 

     int[] to = new int[] { R.id.name_entry, R.id.number_entry}; 

     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_entry, cursor, from, to); 
     this.setListAdapter(adapter); 
    } 
} 

XML-Datei ist:

list_entry.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="6dip"> 
     <TextView 
      android:id="@+id/name_entry" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:gravity="center_vertical" 
     android:textSize="18dip"/> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:id="@+id/number_entry" 
      android:singleLine="true" 
      android:ellipsize="marquee" 
     android:textSize="18dip"/> 
    </LinearLayout> 
+2

Benötigt sonst ist es nützlich. –

+1

startManagingCursor ist veraltet ... – drulabs

+0

@KKD: Was sollte es ersetzen? –

1

This grundlegende Implementierung von Android-Kontaktliste Aktivität ist.

0

versuchen Absicht zur Kontaktliste

  startActivityForResult(new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI),1);}