Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pick a Number and Name From Contacts List in android app

i want to pick a contact with it's number from my contacts list. i read a lot of solutions and research for couple weeks but all of articles didn't work properly. some codes like following:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT); 

// and in activityresult:

if (resultCode == Activity.RESULT_OK) {             Uri contactData = data.getData();             Cursor c =  managedQuery(contactData, null, null, null, null);             if (c.moveToFirst()) {               String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));               tv1.setText(name);             }           } 

or this code for getting all contacts but i cant have the number of contacts:

String[] contacts = new String[] {People.NAME, People.NUMBER};        Uri contentUri = People.CONTENT_URI;         Cursor cursor = managedQuery(contentUri, contacts, null, null, null);                  String textContacts = "";                  if (cursor.moveToFirst()) {              String myname = null;              String mynumber = null;              do {                   textContacts = textContacts + cursor.getString(cursor.getColumnIndex(People.NAME)) + " : " + cursor.getString(cursor.getColumnIndex(People.NUMBER)) + "\n";              } while (cursor.moveToNext());  tv1.setText(textContacts); } 

can anyone help me plz? my android is 2.3.3

like image 872
aTa Avatar asked Feb 29 '12 09:02

aTa


People also ask

How do I select a contact list?

On your Android phone or tablet, open the Contacts app . Tap a Contact in the list. Select an Option.

How do I show contact names and numbers on Android?

Display Only Android Contacts with Phone Numbers. Open your Contacts app and tap the Options button (three dots), and select Contacts Manager. On the next screen, tap on Contacts to display from the menu.


1 Answers

Try following code it will help you:

  // You need below permission to read contacts   <uses-permission android:name="android.permission.READ_CONTACTS"/>    // Declare   static final int PICK_CONTACT=1;    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);   startActivityForResult(intent, PICK_CONTACT);    //code    @Override  public void onActivityResult(int reqCode, int resultCode, Intent data) {  super.onActivityResult(reqCode, resultCode, data);   switch (reqCode) {  case (PICK_CONTACT) :    if (resultCode == Activity.RESULT_OK) {       Uri contactData = data.getData();      Cursor c =  managedQuery(contactData, null, null, null, null);      if (c.moveToFirst()) {            String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));           String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));             if (hasPhone.equalsIgnoreCase("1")) {           Cursor phones = getContentResolver().query(                         ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,                         null, null);              phones.moveToFirst();               cNumber = phones.getString(phones.getColumnIndex("data1"));              System.out.println("number is:"+cNumber);            }          String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));        }    }    break;  }  } 
like image 133
Dharmendra Barad Avatar answered Sep 23 '22 19:09

Dharmendra Barad