简体   繁体   English

获取android联系电话号码列表

[英]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. 我是android的新手。当我试图让联系人名称工作正常但我想只获得数字,但我无法这样做。 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". 当我执行它时,它说“java.lang.IllegalArgumentException:无效的列data1”。 i have googled it a lot and applied some solution but failed. 我已经google了很多并应用了一些解决方案但失败了。 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 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 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: @ 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? 但最重要的是......你没有注意到在格式化phoneNumber的行下方你没有添加名字吗? You should add the name to the number just the way you added the phoneNumber (I enclosed in the code below): 您应该按照添加phoneNumber的方式将名称添加到数字中(我在下面的代码中附上):

..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): 上面代码的输出将是listview上的单独名称和数字(如下所示):


name 名称

number

name 名称

in order to display that in one line, you can do this: 为了在一行中显示,您可以这样做:

aa.add(name+"\\n"+phoneNumber); aa.add(名+ “\\ 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;
  }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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