优化 Android 联系人的 AutoCompleteTextView
我实现了自动完成功能,允许用户开始输入联系人,并使用 autoCompleteTextView 使该联系人显示在下拉列表中,就像为消息或电子邮件选择联系人时的工作方式一样。
由于我不希望有一个变量同时保存所有联系人,因为这可能会很大,因此当用户在联系人字段中输入字母时,我填充了 ArrayList。
我这样设置:- peopleList = new ArrayList>();
adapter = new SimpleAdapter(this, peopleList, R.layout.customcontcell ,new String[] { "Name", "Phone" , "Type" }, new int[] { R.id.ccontName, R.id.ccontNo, R.id.ccontType });
txtPhoneNo.setAdapter(adapter);
然后,当用户开始输入名称时,它会从联系人中获取与该名称匹配的所有行,这是在我的函数“QueryContacts”中完成的,如下所示: -
selectionWhere = ContactsContract.Contacts.DISPLAY_NAME+" LIKE '" + name + "%'";
//Cursor to retrive contact details.
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, selectionWhere, null, null);
然后,该游标用于填充适配器正在使用的 peopleList。
在这种状态下它可以工作,但是如果没有一些检查,上面的代码最终会检索最初的所有联系人(因为没有输入名称,所以它不会过滤任何内容),并且每次输入新字母时都会运行。这是紧张而缓慢的。
因此,我想进行一些简单的检查,将其限制为仅在输入 2 个字母时检索联系人,并且不再检索,除非它低于 2 个字母,然后再次返回到 2 个字母。
因此,围绕我的 QueryContacts 函数,我添加: -
if(name.length() < 2)
mGotContacts = false;
//If the length is two letters long and we haven't queried already, query for the name.
if(name.length() == 2 && mGotContacts == false)
{
// Cursor code
// Populate list with cursor data.
}
问题现在是 autocompleteTextView 不再下拉,我已经检查过,并且填充我的 SimpleAdapter 的变量 peopleLists 正在正确更新。
那么,我这样做是不是很愚蠢?我是否应该一次性获取所有数据并让 AutoCompleteTextView 对其进行过滤?
他们是执行此操作的更好方法吗?为什么它不再适用于我的 QueryContacts 函数中的这些检查?
I have implemented an autocomplete feature to allow the user to start typing a contact and for that contact to appear in a dropdown list using an autoCompleteTextView, like the same way it works when picking contacts for messages or e-mails.
As I don’t want a variable holding all of the contacts at once since this could be very big, I populated my ArrayList as the user enters letters into the contact field.
I am setting it up like this:-
peopleList = new ArrayList>();
adapter = new SimpleAdapter(this, peopleList, R.layout.customcontcell ,new String[] { "Name", "Phone" , "Type" }, new int[] { R.id.ccontName, R.id.ccontNo, R.id.ccontType });
txtPhoneNo.setAdapter(adapter);
Then when the user starts typing a name it grabs all the rows from the Contacts that match that, this is done in my function “QueryContacts” like so:-
selectionWhere = ContactsContract.Contacts.DISPLAY_NAME+" LIKE '" + name + "%'";
//Cursor to retrive contact details.
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, selectionWhere, null, null);
This cursor is then used to populate my peopleList which is being used by the Adapter.
In this state it works, however without some checks the above code ends up retrieving ALL of the contacts initially (as no name is entered so it filters nothing) and it runs every time a new letter is typed. This is jittery and slow.
So I wanted to put some simple checks to limited it to only retrieving the contacts when 2 letters have been entered, and not retrieving anymore unless it goes below 2 letters then back to 2 again.
So around my QueryContacts functions I add:-
if(name.length() < 2)
mGotContacts = false;
//If the length is two letters long and we haven't queried already, query for the name.
if(name.length() == 2 && mGotContacts == false)
{
// Cursor code
// Populate list with cursor data.
}
Problem is now the autocompleteTextView no longer drops down, I have checked and the variable peopleLists which populated my SimpleAdapter is being correctly updated.
So, am I doing this a daft way? Should I just grab all of the data in one go and let the AutoCompleteTextView filter it?
Is their a better way of doing this and why does it no longer work with those checks in my QueryContacts function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 AutoCompleteTextView 的阈值属性。阈值定义在显示自动完成下拉列表之前必须输入的字符数。我不确定这会对性能产生什么影响,但由于它是 Android 原生的,我想它会尽可能快。
setThreshold 方法文档
Have a look at the threshold property of AutoCompleteTextView. The threshold defines the number of characters that must be entered before the autocomplete drop down is shown. I am not sure what effect this would have on performance, but since its native to android I imagine its as fast as reasonably possible.
setThreshold method documentation