Chr*_*ian 6 android list contact
任何人都可以了解如何从Android获取联系人列表?
我只想获得与拨号器应用程序相同的列表.但我得到了许多不在拨号列表上的联系人,代码如下.
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Contacts.People.CONTENT_URI, null, null, null, Contacts.ContactMethods.DEFAULT_SORT_ORDER);
startManagingCursor(cursor);
Run Code Online (Sandbox Code Playgroud)
提前致谢.
试试这个片段:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
你所拥有的看起来不错。您能否详细说明“获取大量不在拨号列表中的联系人”?难道是Android在造人吗?或者您是否看到有电子邮件地址但没有电话号码的人(因此他们可能不会出现在拨号器中)?
请注意,Contacts.People适用于 Android 1.6 及以下版本。从 Android 2.0 开始,该提供程序已被弃用,并由一ContactsContract组提供程序取代。
| 归档时间: |
|
| 查看次数: |
12006 次 |
| 最近记录: |