Skip to main content

how to fetch contact number in android?



I am trying to fetch contact number in my application but it is showing only id and display name can anyone correct it. I dont kno how to fetch the contact numbers. I am only trying to fetch the contact numbers not to add the contact numbers.







package com.data;



import android.app.Activity;

import android.content.Intent;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.ContactsContract;

import android.util.Log;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.ListView;

import android.widget.SimpleCursorAdapter;



public final class ContactManager extends Activity

{



public static final String TAG = "ContactManager";



private Button mAddAccountButton;

private ListView mContactList;

private boolean mShowInvisible;

private CheckBox mShowInvisibleControl;



/**

* Called when the activity is first created. Responsible for initializing the UI.

*/

@Override

public void onCreate(Bundle savedInstanceState)

{

Log.v(TAG, "Activity State: onCreate()");

super.onCreate(savedInstanceState);

setContentView(R.layout.contact_manager);



// Obtain handles to UI objects

// mAddAccountButton = (Button) findViewById(R.id.addContactButton);

mContactList = (ListView) findViewById(R.id.contactList);

mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);



// Initialize class properties

mShowInvisible = false;

mShowInvisibleControl.setChecked(mShowInvisible);



// Register handler for UI elements



mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);

mShowInvisible = isChecked;

populateContactList();

}

});



// Populate the contact list

populateContactList();

}







private void populateContactList() {



Cursor cursor = getContacts();

String[] pno=new String[cursor.getCount()];

if(cursor.getCount()>0)

{

int i=0;

if(cursor.moveToFirst())

{

do

{

pno[i++]=(String)cursor.getString(0).toString() +" "+ (String)cursor.getString(1);



}while(cursor.moveToNext());

}



}



ArrayAdapter<String> adapter=new ArrayAdapter<String>(ContactManager.this,android.R.layout.simple_list_item_1,pno);

mContactList.setAdapter(adapter);





}





private Cursor getContacts()

{

// Run query

Uri uri = ContactsContract.Contacts.CONTENT_URI;

String[] projection = new String[] {

ContactsContract.Contacts._ID,

ContactsContract.Contacts.DISPLAY_NAME,

};

String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +

(mShowInvisible ? "0" : "1") + "'";

String[] selectionArgs = null;

String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";



return managedQuery(uri, projection, selection, selectionArgs, sortOrder);

}







}


Comments

  1. You need to use another content provider uri. I will point you to one answer of mine, so that I don't repeat myself in stackoverflow: http://stackoverflow.com/a/8646827/1108032. In this answer I fetch the name out of the phone number, but I really believe you can revert it easily.

    ReplyDelete
  2. You should use the Phone Uri or the new PhoneLookUp Uri to get display name and all phone numbers with contact id.

    http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html

    ReplyDelete
  3. Modify your query projection to include also the ContactsContract.Contacts.HAS_PHONE_NUMBER column.

    Then, inside the do-while loop, you can do:

    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    if (Integer.parseInt(hasPhone) > 0) {
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
    String phoneNumber;
    while (cursor.moveToNext()) { //iterate over all contact phone numbers
    phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }
    phones.close();
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...