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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.