AutoCompleteTextView过滤问题
我有一个 AutoCompleteTextView
和一个 自定义 AutoCompleteTextView 适配器。
我的列表有 20 个项目,所有项目都包含“vodafone”字符串。当我在 AutoCompleteTextView 中输入一个字符时,它会显示结果,而第二个字符时,它不会显示列表。 例如;
<br/>->V
<br/>-->AutoComplete DropDown menu appears
<br/>->Vo
<br/>-->AutoComplete DropDown disappears
<br/>->Vod
<br/>-->AutoComplete DropDown menu appears
<br/>->Voda
<br/>-->AutoComplete DropDown disappears
<br/>...etc
为什么会发生这种情况?有什么建议吗?
这是我的适配器:
package listadapters;
import java.util.ArrayList;
import java.util.Locale;
import objects.Item;
import utils.Logging;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
public class AutoCompleteTextAdapter2 extends BaseAdapter implements Filterable {
Context _context;
ArrayList<Item> _items = new ArrayList<Item>();
public AutoCompleteTextAdapter2(Context context, ArrayList<Item> items) {
_context = context;
for (int i = 0; i < items.size(); i++) {
_items.add(new Item(items.get(i)));
}
for (int i = 0; i < _items.size(); i++) {
orig.add(new Item(_items.get(i)));
}
filter = new ItemFilter();
}
@Override
public int getCount() {
if (_items != null)
return _items.size();
else
return 0;
}
@Override
public Object getItem(int arg0) {
return _items.get(arg0);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View gv;
if (arg1 == null){
gv = new View(_context);
}else {
gv = (View) arg1;
}
return gv;
}
@Override
public Filter getFilter() {
return filter;
}
private ItemFilter filter;
ArrayList<Item> orig = new ArrayList<Item>();
private class ItemFilter extends Filter {
public ItemFilter() {
}
ArrayList<Item> results = new ArrayList<Item>();
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if(constraint != null)
Logging.TraceMessage(constraint.toString(), "**", "1");
FilterResults oReturn = new FilterResults();
if (orig == null){
for (int i = 0; i < _items.size(); i++) {
orig.add(new Item(_items.get(i)));
}
}
if (constraint != null){
results.clear();
if (orig != null && orig.size() > 0) {
for (Item i : orig) {
if (i._itemName.toLowerCase(Locale.US).contains(constraint.toString().toLowerCase(Locale.US))){
results.add(new Item(i));
}
}
}
Logging.TraceMessage(String.valueOf(results.size()), "**", "2");
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
_items.clear();
for(int i = 0; i < ((ArrayList<Item>)results.values).size(); i++){
_items.add(new Item(((ArrayList<Item>)results.values).get(i)));
}
notifyDataSetChanged();
}
}
}
并设置适配器代码:
AutoCompleteTextAdapter2 adapter = new AutoCompleteTextAdapter2(myAct, GlobalVariables.currCategoryItems);
yeniMarkaModel.setAdapter(adapter);
I have aAutoCompleteTextView
and a custom AutoCompleteTextView adapter.
my list has 20 items and All items contains "vodafone" string. When i type one character in AutoCompleteTextView
, it shows the results and on second, it does not shows the list. For example;
<br/>->V
<br/>-->AutoComplete DropDown menu appears
<br/>->Vo
<br/>-->AutoComplete DropDown disappears
<br/>->Vod
<br/>-->AutoComplete DropDown menu appears
<br/>->Voda
<br/>-->AutoComplete DropDown disappears
<br/>...etc
Why it's happening? Any suggestions?
here is my adapter:
package listadapters;
import java.util.ArrayList;
import java.util.Locale;
import objects.Item;
import utils.Logging;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
public class AutoCompleteTextAdapter2 extends BaseAdapter implements Filterable {
Context _context;
ArrayList<Item> _items = new ArrayList<Item>();
public AutoCompleteTextAdapter2(Context context, ArrayList<Item> items) {
_context = context;
for (int i = 0; i < items.size(); i++) {
_items.add(new Item(items.get(i)));
}
for (int i = 0; i < _items.size(); i++) {
orig.add(new Item(_items.get(i)));
}
filter = new ItemFilter();
}
@Override
public int getCount() {
if (_items != null)
return _items.size();
else
return 0;
}
@Override
public Object getItem(int arg0) {
return _items.get(arg0);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View gv;
if (arg1 == null){
gv = new View(_context);
}else {
gv = (View) arg1;
}
return gv;
}
@Override
public Filter getFilter() {
return filter;
}
private ItemFilter filter;
ArrayList<Item> orig = new ArrayList<Item>();
private class ItemFilter extends Filter {
public ItemFilter() {
}
ArrayList<Item> results = new ArrayList<Item>();
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if(constraint != null)
Logging.TraceMessage(constraint.toString(), "**", "1");
FilterResults oReturn = new FilterResults();
if (orig == null){
for (int i = 0; i < _items.size(); i++) {
orig.add(new Item(_items.get(i)));
}
}
if (constraint != null){
results.clear();
if (orig != null && orig.size() > 0) {
for (Item i : orig) {
if (i._itemName.toLowerCase(Locale.US).contains(constraint.toString().toLowerCase(Locale.US))){
results.add(new Item(i));
}
}
}
Logging.TraceMessage(String.valueOf(results.size()), "**", "2");
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
_items.clear();
for(int i = 0; i < ((ArrayList<Item>)results.values).size(); i++){
_items.add(new Item(((ArrayList<Item>)results.values).get(i)));
}
notifyDataSetChanged();
}
}
}
and set adapter code:
AutoCompleteTextAdapter2 adapter = new AutoCompleteTextAdapter2(myAct, GlobalVariables.currCategoryItems);
yeniMarkaModel.setAdapter(adapter);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在返回结果之前,您必须将过滤的项目数分配给
FilterResults.count
。文本控件为下拉列表分配这么多行。原因是
AutoCompleteTextView
从不直接使用values
字段,而是要求您的Adapter
获取具有特定索引的项目。例如,这使得始终可以将整个项目集合传递给values
,但仅从Adapter.getItem()
返回其中的一些项目。You MUST assign the number of items you filtered to the
FilterResults.count
before you return your results. The text control allocates this many lines for the drop-down.The reason for this is that the
AutoCompleteTextView
never uses thevalues
field directly but rather asks yourAdapter
to fetch an item with a specific index. This makes it possible, for instance, to always pass the whole collection of items tovalues
but only return some of them fromAdapter.getItem()
.