Android联系人列表 [英] Android Contact List

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

问题描述

任何人都可以阐明如何从android获取联系人列表吗?

Can anyone shed a light on how to get contact list from android?.

我只想获取与Dialer应用程序中相同的列表.但是我得到了很多不在拨号列表中的联系人,下面的代码.

I just want to get the same list as in the dialer app. But im getting a lots of contacts that are not on the dialer list with the code below.

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Contacts.People.CONTENT_URI, null, null, null, Contacts.ContactMethods.DEFAULT_SORT_ORDER);
startManagingCursor(cursor);

提前谢谢.

推荐答案

尝试以下代码段:

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.SimpleCursorAdapter;

public class ContactList extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, null);

        startManagingCursor(cursor);

        String[] from = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER};

        int[] to = new int[] { R.id.name_entry, R.id.number_entry};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_entry, cursor, from, to);
        this.setListAdapter(adapter);
    }
}

XML文件为:

list_entry.xml

list_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dip">
        <TextView
            android:id="@+id/name_entry"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
        android:textSize="18dip"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:id="@+id/number_entry"
            android:singleLine="true"
            android:ellipsize="marquee"
        android:textSize="18dip"/>
    </LinearLayout>

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

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