2014-04-01 7 views
7

Ich führe eine Abfrage gegen CommonDataKinds.Phone.CONTENT_URI und ich bekomme alle Ergebnisse, die eine NOT NULL Telefon-ID haben.Android Contacts._ID! = Data.CONTACT_ID

so ziemlich der Code:

String[] projection2 = new String[] { 
      ContactsContract.CommonDataKinds.Phone.NUMBER, 
      ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
      ContactsContract.CommonDataKinds.Phone.CONTACT_ID,   
      ContactsContract.Contacts._ID,        
      ContactsContract.Contacts.DISPLAY_NAME,    
     }; 
    String where2 = ContactsContract.CommonDataKinds.Phone._ID + " != ''" ; 


    Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
      projection2, where2, null, null); 

und dann iterieren ich durch jeden Cursor Ergebnis und bekommen die Spalten ich will. Der Code ist:

  if (phoneCursor.getCount() > 0) { 
      while (phoneCursor.moveToNext()) { 
       String contacts_id = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.Contacts._ID)); 
       String phone_id = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); 
        Log.i("phonecursor", "contacts_id= " + contacts_id + " phone_id " + phone_id 
          + " contacts_name= " + contacts_name + " phone_name= " + phone_name 
          + " Phone= " + phone ); 

      } 
      phoneCursor.close(); 
     } 

Was ich nicht bekommen ist, warum Phone.CONTACT_ID aus dem entsprechenden Contacts._ID aus der gleichen Reihe ist anders ...

Sollten beide nicht gleich sein? Es gibt viele Beispiele, die genau diese Spalten verwenden, um Abfragen auszuführen. Zum Beispiel here und here, wenn Sie die Schlüsselzeiger überprüfen.

Antwort

5

ContactsContract.Contacts._ID gibt eindeutige ID für eine Zeile zurück.

Jetzt hängt die Ausgabe davon ab, welche cursor Sie ContactsContract.Contacts._ID abfragen.

Wenn Sie abfragen ContactsContract.Contacts._ID von ContactsContract.Contacts.CONTENT_URI, erhalten Sie ContactsContract.Contacts._ID und ContactsContract.CommonDataKinds.Phone.CONTACT_ID gleich.

Aber wenn Sie ContactsContract.Contacts._ID von ContactsContract.CommonDataKinds.Phone.CONTENT_URI Abfrage werden Sie ContactsContract.Contacts._ID und ContactsContract.CommonDataKinds.Phone.CONTACT_ID anders als an jedem Telefoneintrag ContactsContract.Contacts._ID erhöht bekommen.

Also, wenn Sie gleiche _ID und Phone._ID wollen, dann Abfrage von ContactsContract.Contacts.CONTENT_URI statt ContactsContract.CommonDataKinds.Phone.CONTENT_URI

18

ContactsContract.Contacts._ID kehrt eindeutige ID für eine Reihe

this is very good example

and this image

+0

Nach 24 Stunden der Exploration Ich habe endlich herausgefunden, wie Kontakte tatsächlich gespeichert werden und wie meine Abfragen funktionieren und wie manche Anfragen nicht funktionieren! Vielen Dank! – kirtan403

+3

Sehr gute und hilfreiche Antwort. Vielen Dank. – Hossein

Verwandte Themen