选择一个号码和名称从Android应用程序的联系人列表 [英] Pick a Number and Name From Contacts List in android app
本文介绍了选择一个号码和名称从Android应用程序的联系人列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想选择一个与它接触是从我的联系人列表号。 我读了很多的解决方案,并研究几个星期,但所有的文章都不能正常工作。 一些codeS类似如下:
i want to pick a contact with it's number from my contacts list. i read a lot of solutions and research for couple weeks but all of articles didn't work properly. some codes like following:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
//和activityresult:
// and in activityresult:
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(ContactsContract.Contacts.DISPLAY_NAME));
tv1.setText(name);
}
}
或此code获取的所有联系人,但我不能有触点数量:
or this code for getting all contacts but i cant have the number of contacts:
String[] contacts = new String[] {People.NAME, People.NUMBER};
Uri contentUri = People.CONTENT_URI;
Cursor cursor = managedQuery(contentUri, contacts, null, null, null);
String textContacts = "";
if (cursor.moveToFirst()) {
String myname = null;
String mynumber = null;
do {
textContacts = textContacts + cursor.getString(cursor.getColumnIndex(People.NAME)) + " : " + cursor.getString(cursor.getColumnIndex(People.NUMBER)) + "\n";
} while (cursor.moveToNext());
tv1.setText(textContacts);
}
谁能帮助我PLZ?我的Android 2.3.3是
can anyone help me plz? my android is 2.3.3
推荐答案
试试下面的code它会帮助你。
Try following code it will help you
// Declare
static final int PICK_CONTACT=1;
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
//code
@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 id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex("data1"));
System.out.println("number is:"+cNumber);
}
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
}
break;
}
}
这篇关于选择一个号码和名称从Android应用程序的联系人列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文