Android从联系人列表中获取电话号码 [英] Android get phone number from contact list

查看:115
本文介绍了Android从联系人列表中获取电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些代码,它们基本上使用ListView在联系人列表中显示姓名,当我单击每个姓名时,我想获取其电话号码:

I have these codes which basically use a ListView to display the names in the contact list and I want to get their phone number when click each single name:

final ContentResolver cr = getContentResolver();

final Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
myCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {ContactsContract.Contacts.DISPLAY_NAME}, new int[]{R.id.TVRow}, 0);
myPhoneList.setAdapter(myCursorAdapter);

myPhoneList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        c.moveToPosition(position);
        Toast.makeText(getApplicationContext(), c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)), Toast.LENGTH_SHORT).show();
    }
});

onItemClick方法中

    GetColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)

返回-1,因此我无法通过此方法获取电话号码.

returns -1 and therefore I cannot get the phone number with this method.

我还尝试打印出光标c的所有列,它返回34列,但似乎唯一与电话号码相关的列是HasPhoneNumber.

I've also tried to print out all the columns of the cursor c and it returns 34 columns but the only column which seems to be related to the phone number is HasPhoneNumber.

那么问题出在哪里,我该如何解决?谢谢!

So where's the problem and how can I fix it? Thanks!

更新的版本,其中更改了传递给构造myCursorAdapterString数组:

The updated version, where the String array passed to construct myCursorAdapter is changed:

final ContentResolver cr = getContentResolver();

    final Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    myCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER}, new int[]{R.id.TVRow}, 0);
    myPhoneList.setAdapter(myCursorAdapter);

    myPhoneList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            c.moveToPosition(position);
            Toast.makeText(getApplicationContext(), c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)), Toast.LENGTH_SHORT).show();
        }
    });

我想更新后的代码将在ListView中显示电话号码,但我收到一条错误消息,说列'data1'不存在".

I suppose the updated code will show the phone numbers in the ListView but I got an error says "column 'data1' does not exist".

推荐答案

ContactsContract Android API将有关联系人的数据(例如电话号码)存储在Data表中,而不是在Contacts表中.

The ContactsContract Android API stores data about contacts like phone number in the Data table, not the Contacts table.

请仔细阅读以下内容: https://developer.android.com/reference/android/provider/ContactsContract.html .

Read this carefully: https://developer.android.com/reference/android/provider/ContactsContract.html.

更新-这是您代码的固定版本(未经测试):

Update - here's a fixed version of your code (untested):

final ContentResolver cr = getContentResolver();
String[] projection = new String[] {Contacts.DISPLAY_NAME, Phone.NUMBER};
final Cursor c = cr.query(Data.CONTENT_URI, projection, null, null, null);
myCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {Phone.NUMBER}, new int[]{R.id.TVRow}, 0);
myPhoneList.setAdapter(myCursorAdapter);

myPhoneList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        c.moveToPosition(position);
        Toast.makeText(getApplicationContext(), c.getString(1), Toast.LENGTH_SHORT).show();
    }
});

这篇关于Android从联系人列表中获取电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆