2017-05-22 8 views
0

So habe ich versucht, Kontakt zu einem Editiertext hinzuzufügen, ich habe Onclickevent verwendet, um Kontakte aufzurufen und dann, sobald ein Kontakt ausgewählt wurde, sollte es in Edittext geschrieben werden, aber ich bin nicht in der Lage, das zu tun, unter meinem onActivity Ergebnis ist,Kontakte werden nicht auf meinem Edittext angezeigt

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     switch (requestCode) { 
      case CONTACT_PICKER_RESULT: 
       Cursor cursor = null; 
       String name = ""; 
       try { 
        Uri result = data.getData(); 
        //writeToFile("uri" +result); 
        String id = result.getLastPathSegment(); 

        // query for name 
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
          null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] { id }, 
          null); 

        if (cursor != null && cursor.moveToFirst()) 
        { 
         int phoneIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA); 
         int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
         writeToFile("ifcursor" +phoneIdx+nameIdx); 
         name = cursor.getString(nameIdx); 
        } 



       } catch (Exception e) { 
        //Log.e(DEBUG_TAG, "Failed to get name", e); 
       } finally { 
        if (cursor != null) { 
         cursor.close(); 
        } 
        // phNo = (EditText) findViewById(R.id.phone_number); 
        phNo.setText(name); 
        if (name.length() == 0) { 
         Toast.makeText(getApplicationContext(),"Name not found for contact.",Toast.LENGTH_LONG).show(); 
        } 

       } 

       break; 
     } 

    } else { 
     //Log.w(DEBUG_TAG, "Warning: activity result not ok"); 
    } 
} 

jede mögliche Hilfe würde sehr geschätzt werden, es Stuck auf

Antwort

0

„Namen nicht gefunden“ Versuchen sie diesen

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // Check which request it is that we're responding to 
     if (requestCode == PICK_CONTACT_REQUEST) { 
      // Make sure the request was successful 
      if (resultCode == RESULT_OK) { 
       // Get the URI that points to the selected contact 
       Uri contactUri = data.getData(); 
       // We only need the NUMBER column, because there will be only one row in the result 
       String[] projection = {Phone.NUMBER}; 

       // Perform the query on the contact to get the NUMBER column 
       // We don't need a selection or sort order (there's only one result for the given URI) 
       // CAUTION: The query() method should be called from a separate thread to avoid blocking 
       // your app's UI thread. (For simplicity of the sample, this code doesn't do that.) 
       // Consider using CursorLoader to perform the query. 
       Cursor cursor = getContentResolver() 
         .query(contactUri, projection, null, null, null); 
       cursor.moveToFirst(); 

       // Retrieve the phone number from the NUMBER column 
       int column = cursor.getColumnIndex(Phone.DISPLAY_NAME); 
       String number = cursor.getString(column); 
        phNo.setText(number); 


       // Do something with the phone number... 
      } 
     } 
    } 
+0

es sagt Fehlerergebnis, ungültige Spalte data1 liefert –

+0

versuchen jetzt/.................. –

+0

@jaikhambhayta Sie können nicht nach einer Telefonnummer aus der Contacts-Tabelle fragen, die Abfrage muss auf dem 'CommonDataKinds.Phone.CONTENT_URI' stehen Tabelle – marmor

0

Wenn Sie den Benutzer benötigen, um p ick einen Kontakt mit einer Telefonnummer, sollten Sie die folgende Absicht nennen:

Intent i = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI); 
startActivityForResult(i, CONTACT_PICKER_RESULT); 

Dann können Sie die Antwort in onActivityResult wie so handhaben:

Uri result = data.getData(); 
// because we asked for a Phone.CONTENT_URI picker, we'll get a uri for a Phone._ID entry 
String id = result.getLastPathSegment(); 
String[] projection = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER }; 
String selection = Phone._ID + "=" + id; 
Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, projection, selection,null, null); 

if (cursor != null && cursor.moveToFirst()) { 
    String name = cursor.getString(0); 
    String number = cursor.getString(1); 
    Log.d("MyActivity", "got name=" + name + ", phone=" + number); 
} 
if (cursor != null) { 
    cursor.close(); 
} 
+0

Was ist der Unterschied zwischen contacts.contracts.phone und nur Telefon, weil Telefon scheint –

+0

entlastet werden stellen Sie sicher, dass ich mport 'Telefon' aus' ContactsContract.CommonDataKinds.Phone', ich vermute, dass es automatisch in falsche Klasse importiert wurde – marmor

+0

Das bekommt den Kontakt, aber es ist nicht der, den ich ausgewählt habe, ich habe ph.edittext nach dem if() verwendet {cursor.close()}, irgendetwas falsch mache ich ?? –

Verwandte Themen