• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to get phone number from address book

GIR

Member
Hello everyone,

I've been all over google/stackoverflow/other sites, but I just cant seem to get this solved.

Im aiming for 2.1 API, and simply want to open the address book, and when a contact is pressed by the user, have that number returned as a string.

It seems I'm far from alone in having problems with, as the interwebs seem full of questions and very few answers, and further complication is added by depreciation of the frequently seen examples that use pre-2.0 API examples.

Can anyone help me?
 
OK, it seems I answer many of my own questions - is suppose the main this the question gets answered... :confused:

I have a working code sample, but it poses a further question:

After the contact is clicked on, the toast message appears with the correct number - excellent :)-however, the edittext does not update with the number the until the button is used again.

How can i make the edit text update when the contacts book close?

I know i'm missing something here, but i'm so tired after spending 3 days sussing this out, so I'd be very grateful for any help

XML=
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" />
    
<TextView android:text="TextView" 
android:id="@+id/textView1" 
android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 
<Button android:text="Button" 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />
</LinearLayout>
main Java code =

Code:
package com.nds.ContactsLookupLearn;

import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Contacts.People;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.RawContacts.Entity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class ContactsLookupLearn extends Activity {
    /** Called when the activity is first created. */
    public String contact;
    public String xnumber;
    public String NAME;
    static final int PICK_CONTACT_REQUEST = 0;
 
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        //setContentView(R.layout.main);
        setContentView(R.layout.contact_picker);

        Button button1 = (Button)findViewById(R.id.button1);
        final EditText nds = (EditText)findViewById(R.id.editText1);
        nds.setText(xnumber);
        
        
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            // Do stuff here
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                startActivityForResult(intent, 1);     
                nds.setText(xnumber);
            }}
            );
    }
    public void showSelectedNumber(int type, String number) {
        Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();   
        xnumber = number.toString();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            Uri uri = data.getData();
            if (uri != null) {
                Cursor c = null;
                try {
                    c = getContentResolver().query(uri, new String[]{ 
                                ContactsContract.CommonDataKinds.Phone.NUMBER,  
                                ContactsContract.CommonDataKinds.Phone.TYPE },
                            null, null, null);
                    if (c != null && c.moveToFirst()) {
                        String number = c.getString(0);
                        int type = c.getInt(1);
                        showSelectedNumber(type, number);
                    }
                } finally {
                    if (c != null) {
                        c.close();
                    }
                }
            }
        }
    }
}
Thank you for taking the time to read this, and for any suggestions,
GIR
 
Well, you don't set the number to the edittext after it returns from selecting a contact. You should put in: nds.setText(xnumber); in your onActivityResult method.
 
Yes, I did try that.

when i move it there I get the following error:
Code:
 nds cannot be resolved

which is VERY frustrating - been on this a long time now, and tried lots of different ways of doing it, but the edittext does not update until the button is used the 2nd time..
 
SOLVED!

It seems that i had to re-declare (if thats the right term), a variable and its link to the edittext.

When this code is placed inside the showSelectedNumber code block (What is the name for these blocks of code? classes, sub classes... please let me know) it works as I intend, that is after the contact is clicked the contacts list closes and the edittext shows the number of the contact I just pressed :)

Code:
EditText ed=(EditText)findViewById(R.id.editText1);
ed.setText(xnumber);
 
Back
Top Bottom