如何检索联系人的电话号码(Android)?
问题描述:
我有一个绝对的噩梦让我的联系人活动工作 - 我知道这是一个常见问题,但我似乎无法将现有解决方案应用于我的代码 - 我无法理解这一点。任何帮助将非常感激,我在这里撕掉我的头发。如何检索联系人的电话号码(Android)?
public class nominateContactsActivity extends ListActivity {
public String strName;
public String strLoginCode;
public String strTelNo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.nominatecontactsactivitytest);
this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
startManagingCursor(cursor);
String[] contacts = new String[] {ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME};
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_multiple_choice, cursor, columns, to);
this.setListAdapter(adapter);
Button finishButton = (Button) this.findViewById(R.id.finishButton);
finishButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SimpleCursorAdapter adapter = (SimpleCursorAdapter) nominateContactsActivity.this.getListAdapter();
Cursor cursor = adapter.getCursor();
ListView lv = nominateContactsActivity.this.getListView();
SparseBooleanArray selectedItems = lv.getCheckedItemPositions();
for (int i = 0; i < selectedItems.size(); i++) {
int selectedPosition = selectedItems.keyAt(i);
cursor.moveToPosition(selectedPosition);
Log.d("", cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME))+" is checked");
Log.d("", "row id: "+adapter.getItemId(selectedPosition));
}
}
});
我使用上面的代码返回联系人列表 - 这工作得很好,显示联系人的姓名在ListView为我所愿。然而,没有电话号码检索,我只是不知道如何得到它们。
谢谢
答
下面是读取您可以尝试的所有联系号码和名称的代码。
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); while (phones.moveToNext()) { String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close();
谢谢你,谢谢你,谢谢你! 这让我疯狂了两周。 – user1593869 2013-03-16 04:11:21
欢迎...... :)很高兴它帮助你。 :) – GrIsHu 2013-03-16 04:23:20
再次,非常感谢您的帮助之前 - 我现在有一个新的问题... 我只有一个联系在分贝后呢? (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null); (phones.moveToNext()) { \t strName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); (电话号码)。 uploadContacts(); phones.close();}' – user1593869 2013-03-16 14:53:53