简体   繁体   English

如何获取联系人的电话号码(Android)?

[英]How to retrieve contact's phone number (Android)?

I am having an absolute nightmare getting my contacts activity working - I understand this is a common problem, but I just can't seem to apply existing solutions to my code - I just cannot get my head around this. 我真是一场噩梦,无法进行联系活动-我知道这是一个常见问题,但是我似乎无法将现有解决方案应用于我的代码-我只是无法解决这个问题。 Any help would be very much appreciated, I am tearing my hair out here. 任何帮助将不胜感激,我在这里扯头发。

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));
                                }
        }
    });

I am using the above code to return contacts list - this works fine, displays contact's names in the listview as I wished. 我正在使用上面的代码返回联系人列表-可以正常工作,并根据需要在列表视图中显示联系人的姓名。 However, no phone numbers are retrieved, and I just cannot figure out how to get them. 但是,没有检索到电话号码,我只是无法弄清楚如何获得它们。

Thank you 谢谢

Below is the code to read all the contact numbers and name you can try out. 下面的代码可读取您可以尝试的所有联系电话和姓名。

  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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM