我有一个绝对的噩梦让我的联系人活动工作 - 我知道这是一个常见的问题,但我似乎无法将现有的解决方案应用于我的代码 - 我只是无法理解这一点。任何帮助都会非常感激,我在这里撕裂我的头发。
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));
}
}
});
我使用上面的代码返回联系人列表 - 这很好用,按照我的意愿在列表视图中显示联系人的姓名。但是,没有检索到电话号码,我只是无法弄清楚如何获取它们。
谢谢
答案 0 :(得分:2)
以下是您可以尝试阅读所有联系电话和姓名的代码。
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();