2016-04-24 4 views
0

besteht ich an einem Projekt zu arbeiten, ich versuche, wo ich alle Kontakte eines Telefon benötigen, die 123 in seiner Telefonnummer hat. Ich bin in der Lage, die Kontakte abzurufen, sondern „wo“ -Klausel in contentresolver nicht hier als Referenz mein Code arbeitenabrufen Kontakte von android nur dann, wenn die Telefonnummer 123 in dem

public void fetchContacts() { 

     String phoneNumber = null; 
     String email = null; 

     Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; 
     String _ID = ContactsContract.Contacts._ID; 
     String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; 
     String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; 

     Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; 
     String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; 



     StringBuffer output = new StringBuffer(); 

     ContentResolver contentResolver = getContentResolver(); 

     Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null); 

     // Loop for every contact in the phone 
     if (cursor.getCount() > 0) { 

      while (cursor.moveToNext()) { 

       String contact_id = cursor.getString(cursor.getColumnIndex(_ID)); 
       String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)); 

       int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER))); 

       if (hasPhoneNumber > 0) { 

        output.append("\n First Name:" + name); 

        // Query and loop for every phone number of the contact 
        Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, NUMBER + " = 123", null, null); 

        while (phoneCursor.moveToNext()) 

        { 

         phoneNumber = cursor.getString(phoneCursor.getColumnIndex(NUMBER)); 
// 
         output.append("\n Phone number:" + phoneNumber); 


        } 



        phoneCursor.close(); 



       } 

       output.append("\n"); 
      } 

      outputText.setText(output); 
     } 
    } 


} 

Logcat java.lang.RuntimeException: Kann Aktivität ComponentInfo starten {com.gamemyworldgame.contact/ com.gamemyworldgame.contact.MainActivity}: bei android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2325) bei android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2390) bei android.app.ActivityThread.access $ 800 (ActivityThread.java:151) bei android.app.ActivityThread $ H.handleMessage (ActivityThread.java: 1303) bei android.os.Handler.dispatchMessage (Handler.java:102) bei android.os.Looper.loop (Looper.java:135) bei android.app.ActivityThread.main (ActivityThread.java:5257) bei java.lang.reflect.Method.invoke (native Methode) bei java.lang.reflect.Method.invoke (Methode.java:372) bei com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit .java: 903) bei com.android.internal.os.ZygoteInit.main (ZygoteInit.java:698) Verursacht von: android.database.sqlite.SQLiteException: keine solche Spalte: phone.NUMBER (code 1): ,

Antwort

0

Sie benötigen eine Suche zu starten -

Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "ContactsContract.CommonDataKinds.Phone.NUMBER like '%123%'", null, null); 

Dies wird Ihnen alle jene Telefonnummer holen, die ‚123‘ überall in dieser Zahl haben.

Eine andere Art und Weise -

final String keyword = "%123%"; // contains an "123" 

Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?", new String[] { keyword }, null); 
+0

Dank für die Beantwortung, indem diese Linien App nicht starten und wirft Fehler in logcat –

+0

Können Sie die Fehlerprotokolle schreiben? –

+0

Sie sollten Phone.NUMBER anstelle von NUMBER verwenden. Die Antwort wurde aktualisiert. –

Verwandte Themen