简体   繁体   中英

get android contact phone number list

I am new to android.When i try to get contact names its working fine but i want to get numbers only, but i am not able to do so. My code is:-

package com.example.sqllitecontactlist;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class PhoneBookActivity extends Activity {

//Android listview object
ListView listViewPhoneBook;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  setContentView(R.layout.phone_book);

   //get the ListView Reference from xml file
  listViewPhoneBook=(ListView)findViewById(R.id.listPhoneBook);
  String[] arrayColumns = new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER};

    // arrayViewID is the id of the view it will map to here textViewPhone only 
  int[] arrayViewID = new int[]{R.id.textViewNumber};

 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,        arrayColumns, null, null, null);

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.each_contact,    cursor, arrayColumns, arrayViewID);
 listViewPhoneBook.setAdapter(adapter); }}

When i execute this it says "java.lang.IllegalArgumentException: Invalid column data1". i have googled it a lot and applied some solution but failed. Please help me out

    getNumber(this.getContentResolver()); 


public void getNumber(ContentResolver cr)
{
    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            // use the cursor to access the contacts    
    while (phones.moveToNext())
    {
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
             // get display name
      phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
              // get phone number
      System.out.println(".................."+phoneNumber); 
    }

}

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity" >

<ListView
   android:layout_width="match_parent"
   android:layout_height="fill_parent"
   android:id="@+id/lv"/>

</RelativeLayout>

MainActivity.java

 public class MainActivity extends Activity {

     String phoneNumber;
     ListView lv;
     ArrayList <String> aa= new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
         lv= (ListView) findViewById(R.id.lv);

        getNumber(this.getContentResolver()); 
    }

    public void getNumber(ContentResolver cr)
    {
        Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
          String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          System.out.println(".................."+phoneNumber); 
          aa.add(phoneNumber);
        }
                 phones.close()// close cursor
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,aa);
          lv.setAdapter(adapter);
                  //display contact numbers in the list
    }
      }

snap shot

在此输入图像描述

Make sure you have this in manifest

       <uses-permission android:name="android.permission.READ_CONTACTS"/>

My solution to recover all contacts:

    Cursor cursor = null;
    try {
        cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
        int contactIdIdx = cursor.getColumnIndex(Phone._ID);
        int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
        int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
        int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
        cursor.moveToFirst();
        do {
            String idContact = cursor.getString(contactIdIdx);
            String name = cursor.getString(nameIdx);
            String phoneNumber = cursor.getString(phoneNumberIdx);
            //...
        } while (cursor.moveToNext());  
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

You need this permission in your manifest :

<uses-permission android:name="android.permission.READ_CONTACTS" />

I hope I have helped you!

.@Arpit:

add the permission in your manifest.

BUT MOST IMPORTANTLY... Didn't you noticed that just below the line where you formatted the phoneNumber you did not add the names? You should add the name to the number just the way you added the phoneNumber (I enclosed in the code below):

..System.out.println(".................."+phoneNumber); 
aa.add(name);
aa.add(phoneNumber);

the output of the above code will be separate names and numbers on your listview(like this):


name

number

name

in order to display that in one line, you can do this:

aa.add(name+"\\n"+phoneNumber);

and the output will be like this:


name number


name number


name number


good luck!

Find the solution below, It will work for getting contact no from contactlist.

You need permission like:

 android:name="android.permission.READ_CONTACTS"/>

Then, Calling the Contact Picker:

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

Then,

 @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 name = c.getString(c.getColumnIndexOrThrow(People.NAME));
      // TODO Whatever you want to do with the selected contact name.
    }
  }
  break;
  }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM