如何在android中的autocompletetextview中设置联系人号码?

时间:2012-04-08 08:24:07

标签: java android autocomplete android-contentprovider

我有一个代码,我可以在自动完整文本视图中获取联系人姓名,这是我的代码

autoContacts=(AutoCompleteTextView)findViewById(R.id.actvContacts);
          Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            startManagingCursor(emailCursor);
            autoContacts.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, emailCursor, new String[] {Email.DATA1}, new int[] {android.R.id.text1}));
            autoContacts.setThreshold(0);

但是当我点击其中一个名称时,它会在该自动填充文本视图中设置一个文本,如下所示:

android.content.ContentResolver$CursorWrapperInner@44efc9c8

但在这里我想设置具体的电话号码,如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您需要使用setCursorToStringConverter

  

设置用于将过滤Cursor转换为String的转换器。   http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

在你的情况下,你可以这样做:

String[] from = new String[] {
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER
};
autoContacts=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(emailCursor);
SimpleCursorAdapter adapter =new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, emailCursor, from, new int[] {android.R.id.text1}); 
adapter.setCursorToStringConverter(new CursorToStringConverter() {              
        @Override
        public CharSequence convertToString(Cursor cursor) {
            final int columnIndex = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
            final String str = cursor.getString(columnIndex);
            return str;
        }
});
autoContacts.setAdapter(adapter);
autoContacts.setThreshold(0);