带有联系人的自动完成 TextView
Posted
技术标签:
【中文标题】带有联系人的自动完成 TextView【英文标题】:AutoComplete TextView with Contacts 【发布时间】:2012-08-09 16:20:51 【问题描述】:我需要显示一个自动完成文本框,它基本上会加载联系人电子邮件 ID。 我已经尝试使用自定义适配器,但文本框中没有填充任何内容。完全没有建议。任何解决方案都会非常有用。
【问题讨论】:
请发布您的一些代码,以便我们更好地帮助您 阅读这篇文章***.com/questions/11879078/…这将对你有所帮助 【参考方案1】:尝试以下方法:
ArrayList<String> emailAddressCollection = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
while (emailCur.moveToNext())
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailAddressCollection.add(email);
emailCur.close();
String[] emailAddresses = new String[emailAddressCollection.size()];
emailAddressCollection.toArray(emailAddresses);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, emailAddresses);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.YOUR_TEXT_VIEW);
textView.setAdapter(adapter);
注意:不要忘记将READ_CONTACTS
权限添加到您的Manifest.xml:
<uses-permission android:name="android.permission.READ_CONTACTS" />
【讨论】:
我在这里做类似的事情!! ***.com/questions/12854336/autocompletetextview-backed-by-cursorloader【参考方案2】:@Korhan 肯定比我想象的要优雅得多。我的代码有效,但 @Korhan 的要简单得多。谢谢。我创建了这个自定义适配器类来读取联系人
class ContactListAdapter extends CursorAdapter implements Filterable
private ContentResolver mCR;
public ContactListAdapter(Context context, Cursor c,boolean a)
super(context, c, true);
mCR = context.getContentResolver();
@Override
public void bindView(View view, Context context, Cursor cursor)
((TextView) view).setText(cursor.getString(1));
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view = (TextView) inflater.inflate( android.R.layout.simple_dropdown_item_1line, parent, false);
view.setText(cursor.getString(1));
return view;
@Override
public String convertToString(Cursor cursor)
return cursor.getString(1);
public Cursor runQueryOnBackgroundThread(CharSequence constraint)
if (getFilterQueryProvider() != null)
return getFilterQueryProvider().runQuery(constraint);
StringBuilder buffer = null;
String[] args = null;
if (constraint != null)
buffer = new StringBuilder();
buffer.append("UPPER(");
buffer.append(ContactsContract.CommonDataKinds.Email.ADDRESS);
buffer.append(") GLOB ?");
args = new String[] constraint.toString().toUpperCase() + "*" ;
return mCR.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,CreateEventActivity.PEOPLE_PROJECTION ,buffer == null ? null : buffer.toString(), args,
null);
还有主要的Activity:
MultiAutoCompleteTextView act = (MultiAutoCompleteTextView)findViewById(R.id.attende_list);
ContentResolver content = getContentResolver();
Cursor cursor = content.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,PEOPLE_PROJECTION, null, null, null);
ContactListAdapter adapter = new ContactListAdapter(this, cursor, true);
act.setThreshold(0);
act.setAdapter(adapter);
act.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
【讨论】:
以上是关于带有联系人的自动完成 TextView的主要内容,如果未能解决你的问题,请参考以下文章
Android 在一个结果中自动完成多个 textview 行