2017-05-22 1 views
0

J'ai donc essayé d'ajouter un contact à un edittext, j'ai utilisé Onclickevent pour appeler des contacts, puis une fois qu'un contact a été sélectionné, il doit être écrit sur edittext, mais Je ne suis pas en mesure de le faire, ci-dessous est le résultat de mon onActivity,Les contacts n'apparaissent pas sur mon edittext

@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"); 
    } 
} 

toute aide serait très apprécié, il stucks sur "nom not found"

Répondre

0

Essayez celui

@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

il est dit résultat livrer échec, colonne invalide data1 –

+0

essayer maintenant/.................. –

+0

@jaikhambhayta vous ne pouvez pas interroger un numéro de téléphone à partir de la table des contacts, la requête doit être sur 'CommonDataKinds.Phone.CONTENT_URI' table – marmor

0

Si vous avez besoin de l'utilisateur pour p beurk un contact avec un numéro de téléphone, vous devez appeler l'intention suivante:

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

Ensuite, vous pouvez gérer la réponse à onActivityResult comme ceci:

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

quelle est la différence entre contacts.contracts.phone et juste le téléphone, parce que le téléphone semble être déprimé –

+0

assurez-vous que vous mport 'Phone' de' ContactsContract.CommonDataKinds.Phone', je suppose qu'il est importé automatiquement dans la mauvaise classe – marmor

+0

Cela obtient le contact, mais ce n'est pas celui que j'ai choisi, j'ai utilisé ph.edittext après le if() {cursor.close()}, quelque chose de mal que je fais ?? –