Android文本视图自动完成和自动搜索
我有一些联系人自动完成和自动搜索算法用于我的android应用程序。 首先使用一些xml来定义输入的文本视图:Android文本视图自动完成和自动搜索,android,android-contacts,Android,Android Contacts,我有一些联系人自动完成和自动搜索算法用于我的android应用程序。 首先使用一些xml来定义输入的文本视图: <AutoCompleteTextView a:id="@+id/recipientBody" a:layout_width="0dip" a:layout_height="wrap_content" a:layout_weight="1.0" a:nextFocusRight="@+id/smsRe
<AutoCompleteTextView
a:id="@+id/recipientBody"
a:layout_width="0dip"
a:layout_height="wrap_content"
a:layout_weight="1.0"
a:nextFocusRight="@+id/smsRecipientButton"
a:hint="@string/sms_to_whom"
a:maxLines="10"
/>
private List<String> getAllContacts() {
List<String> contacts = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{contactId}, null);
while (pCursor.moveToNext()) {
String phoneNumber = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.add(phoneNumber + " ( " + displayName + " )");
}
pCursor.close();
}
}
}
return contacts;
}
现在我设置了文本视图
AutoCompleteTextView recip =
(AutoCompleteTextView) findViewById(R.id.recipientBody);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.list_item, getAllContacts());
recip.setAdapter(adapter);
AutoCompleteTextView recip=
(AutoCompleteTextView)findViewById(R.id.recipientBody);
阵列适配器=
新的ArrayAdapter(此,R.layout.list_项,getAllContacts());
往复式设置适配器(适配器);
现在是搜索与输入匹配的联系人的实际算法:
<AutoCompleteTextView
a:id="@+id/recipientBody"
a:layout_width="0dip"
a:layout_height="wrap_content"
a:layout_weight="1.0"
a:nextFocusRight="@+id/smsRecipientButton"
a:hint="@string/sms_to_whom"
a:maxLines="10"
/>
private List<String> getAllContacts() {
List<String> contacts = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{contactId}, null);
while (pCursor.moveToNext()) {
String phoneNumber = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.add(phoneNumber + " ( " + displayName + " )");
}
pCursor.close();
}
}
}
return contacts;
}
private List getAllContacts(){
列表联系人=新建ArrayList();
ContentResolver cr=getContentResolver();
Cursor Cursor=cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null);
if(cursor.getCount()>0){
while(cursor.moveToNext()){
String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.\u ID));
String displayName=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))大于0){
游标pCursor=cr.query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
无效的
ContactsContract.CommonDataTypes.Phone.CONTACT_ID+“=?”,
新字符串[]{contactId},null);
while(pCursor.moveToNext()){
String phoneNumber=pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
联系人。添加(电话号码+”(“+displayName+”);
}
pCursor.close();
}
}
}
返回联系人;
}
这对联系人号码和姓名输入都适用。但仍然存在一个问题。用户可以输入多个电话号码。但是,当一个联系人应用于文本视图时,它无法再次搜索,因为该算法会占用整个字符串
我如何解决这个问题?编辑:
嗯,我想了一会儿,发现我的解决方案有一个问题-没有地方可以将联系人从完成列表插入到TextView中
解决方案似乎是,这个东西是为解决你的问题而设计的
抱歉搞混了
对我来说,看起来你需要一个自定义适配器 您可以扩展
ArrayAdapter
并实现getFilter()
——当然,您还需要一个自定义筛选器(扩展filter
),从该方法返回该实例
筛选器的performFiltering
方法有一个参数-需要建议列表的字符串。您需要在最后一个逗号(或用作分隔符的任何字符)后面取一部分,然后返回该子字符串的建议列表
附言
为了获得更好的用户体验,您还可以考虑使用跨距设置AutoCompleteTextView内容的样式:编辑:
嗯,我想了一会儿,发现我的解决方案有一个问题-没有地方可以将联系人从完成列表插入到TextView中
解决方案似乎是,这个东西是为解决你的问题而设计的
抱歉搞混了
对我来说,看起来你需要一个自定义适配器 您可以扩展
ArrayAdapter
并实现getFilter()
——当然,您还需要一个自定义筛选器(扩展filter
),从该方法返回该实例
筛选器的performFiltering
方法有一个参数-需要建议列表的字符串。您需要在最后一个逗号(或用作分隔符的任何字符)后面取一部分,然后返回该子字符串的建议列表
附言
为了获得更好的用户体验,您还可以考虑使用跨距设置AutoCompleteTextView内容的样式: