AutoCompleteTextView 强制显示所有项目 [英] AutoCompleteTextView force to show all items
问题描述
在我的应用程序中有一个时刻,无论用户输入什么,我都需要强制显示建议列表中的所有项目.我该怎么做?
There is a moment in my app, that I need to force to show all items in the suggestion list, no matter what the user has typed. How can I do that?
我尝试用过滤做一些事情,但对我来说,作为初学者过滤太复杂了,我尝试搜索初学者教程进行过滤,但没有任何运气.也许,有更简单的方法可以强制显示所有建议项?
I tried to do something with filtering, but for me as a beginner filtering is just way too complicated, I tried searching beginners tutorial for filtering without any luck. Maybe, there is a simpler way to force to show all the suggestion items?
基本上我的想法是,当用户输入列表中没有的内容时,它会显示他可以拥有的所有可用选项.
Basically what was my idea, is that, when user types something whats not in the list, it shows all the available options he can have.
我找到了检查 ACTV 是否正常显示天气的最佳方法,但是 onTextChangeEvent 我将用户输入的文本与我的列表进行比较,然后如果没有找到任何元素,则显示完整列表.
I've found the best way of checking weather the ACTV is beign shown or not, but onTextChangeEvent I compare the user typed text with my list, and then if no elements have been found show full list.
public void onTextChanged(CharSequence s, int start, int before, int count)
{
final EditText editText = (EditText) findViewById(R.id.vardsUserInput);
String strValue = editText.getText().toString().toUpperCase();
String temp;
int Cc=0; //my count variable
for(int i=0; i<vardi.length; i++)
{
temp = vardi[i].toUpperCase();
if(temp.startsWith(strValue.toUpperCase()))
{
Log.d("testing",vardi[i]);
Cc++;
}
}
if(Cc == 0)
{
//Show all the available options
textView.showDropDown();
}
}
推荐答案
基本上,经过 5-6 个小时的实验以了解该死的过滤器的工作原理后,我编写了自己的适配器,它完全符合我的要求:
Basically, after 5-6 hours of experimentation to understand how the damn filter works, I wrote my own adapter which does exactly what I want:
public class burtuAdapteris extends ArrayAdapter<String> implements Filterable {
ArrayList<String> _items = new ArrayList<String>();
ArrayList<String> orig = new ArrayList<String>();
public burtuAdapteris(Context context, int resource, ArrayList<String> items) {
super(context, resource, items);
for (int i = 0; i < items.size(); i++) {
orig.add(items.get(i));
}
}
@Override
public int getCount() {
if (_items != null)
return _items.size();
else
return 0;
}
@Override
public String getItem(int arg0) {
return _items.get(arg0);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if(constraint != null)
Log.d("Constraints", constraint.toString());
FilterResults oReturn = new FilterResults();
/* if (orig == null){
for (int i = 0; i < items.size(); i++) {
orig.add(items.get(i));
}
}*/
String temp;
int counters = 0;
if (constraint != null){
_items.clear();
if (orig != null && orig.size() > 0) {
for(int i=0; i<orig.size(); i++)
{
temp = orig.get(i).toUpperCase();
if(temp.startsWith(constraint.toString().toUpperCase()))
{
_items.add(orig.get(i));
counters++;
}
}
}
Log.d("REsult size:" , String.valueOf(_items.size()));
if(!counters)
{
_items.clear();
_items = orig;
}
oReturn.values = _items;
oReturn.count = _items.size();
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
使用起来也很简单,只需用这个替换原来的适配器:
And it's simple to use, just replace original adapter with this:
final burtuAdapteris fAdapter = new burtuAdapteris(this, android.R.layout.simple_dropdown_item_1line, liste);
在我的例子中,liste 是:ArrayList
In my case liste is: ArrayList<String> liste = new ArrayList<String>();
这篇关于AutoCompleteTextView 强制显示所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!