AutoCompleteTextView未完成括号内的单词

时间:2015-10-03 18:33:02

标签: android android-arrayadapter autocompletetextview

我已按照以下方式实施AutoCompleteTextView

MainActivity.java

...
    public static String[] myData=new String[]{"Africa (AF)","America (AFM)","Apple (AMP)"};
    text=(AutoCompleteTextView)v.findViewById(R.id.first_state);
    ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.autocompletetextview_row,R.id.textViewItem,myData);

    text.setAdapter(adapter);
    text.setThreshold(1);
    text.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {

            selected_station = (String)parent.getItemAtPosition(position);
            //TODO Do something with the selected text
        }
    });

AutoCompleteTextView的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:padding="10dp" >
    <TextView
        android:id="@+id/textViewItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Item name here..."
        android:textColor="#000000"
        android:textSize="20sp" />
 </RelativeLayout>

当我输入“af ..”时,它会显示非洲(AF)来选择但不是美国(AFM)

数据只是一个示例数据。并不是我只使用AutoCompleteTextView只有3个项目。

编辑:当我删除括号时,它正常工作。但我需要保留括号以供进一步使用。

6 个答案:

答案 0 :(得分:13)

@BNK提供的答案是正确的。但是,我想提供一个类似的解决方案,它不需要整个ArrayAdapter类文件。相反,我们将只扩展该类并覆盖其仅有的两种方法:getView()getFilter()。因此,请定义您的自动完成课程:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class AutoSuggestAdapter extends ArrayAdapter
{
    private Context      context;
    private int          resource;
    private List<String> items;
    private List<String> tempItems;
    private List<String> suggestions;

    public AutoSuggestAdapter(Context context, int resource, List<String> items)
    {
        super(context, resource, 0, items);

        this.context = context;
        this.resource = resource;
        this.items = items;
        tempItems = new ArrayList<String>(items);
        suggestions = new ArrayList<String>();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(resource, parent, false);
        }

        String item = items.get(position);

        if (item != null && view instanceof TextView)
        {
            ((TextView) view).setText(item);
        }

        return view;
    }

    @Override
    public Filter getFilter()
    {
        return nameFilter;
    }

    Filter nameFilter = new Filter()
    {
        @Override
        public CharSequence convertResultToString(Object resultValue)
        {
            String str = (String) resultValue;
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {
            if (constraint != null)
            {
                suggestions.clear();
                for (String names : tempItems)
                {
                    if (names.toLowerCase().contains(constraint.toString().toLowerCase()))
                    {
                        suggestions.add(names);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            }
            else
            {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results)
        {
            List<String> filterList = (ArrayList<String>) results.values;
            if (results != null && results.count > 0)
            {
                clear();
                for (String item : filterList)
                {
                    add(item);
                    notifyDataSetChanged();
                }
            }
        }
    };
}

以XML格式定义自动完成视图,例如:

       <AutoCompleteTextView
        android:id="@+id/autoComplete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter some text ..."/>

并使用它:

    AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);

    List <String> stringList = new ArrayList<String>();
    stringList.add("Black");
    stringList.add("White");
    stringList.add("Yellow");
    stringList.add("Green");
    stringList.add("Blue");
    stringList.add("Brown");
    stringList.add("Orange");
    stringList.add("Pink");
    stringList.add("Violet");
    stringList.add("Cyan");
    stringList.add("LightBlue");

    AutoSuggestAdapter adapter = new AutoSuggestAdapter(this, android.R.layout.simple_list_item_1, stringList);

    autoComplete.setAdapter(adapter);

    // specify the minimum type of characters before drop-down list is shown
    autoComplete.setThreshold(1);

enter image description here

答案 1 :(得分:12)

ArrayAdapter过滤器默认实现正在搜索单词的开头(以空格分隔),我的意思是它使用startsWith 。您需要实施ArrayFiltercontainsstartsWith一起使用。

您的问题将通过以下步骤解决:

  • here
  • 下载ArrayAdapter.java文件
  • 将该文件添加到项目中(例如,您可以通过将文件重命名为CustomArrayAdapter.java来重构)。
  • 在该文件中,您将找到一个私有类ArrayFilter。然后,添加valueText.contains(prefixString)words[k].contains(prefixString),如下所示:

                if (valueText.startsWith(prefixString) || valueText.contains(prefixString)) {
                    newValues.add(value);
                } else {
                    final String[] words = valueText.split(" ");
                    final int wordCount = words.length;
                    // Start at index 0, in case valueText starts with space(s)
                    for (int k = 0; k < wordCount; k++) {
                        if (words[k].startsWith(prefixString) || words[k].contains(prefixString)) {
                            newValues.add(value);
                            break;
                        }
                    }
                }
    
  • 为您的ArrayAdapter

  • 使用该自定义AutoCompleteTextView

以下是结果截图:

AutoCompleteTextView

希望这有帮助!

答案 2 :(得分:1)

只需通过autoCompleteTextView.showDropDown()手动显示下拉菜单即可。

例如:

autoCompleteTextView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        acSrcCity.showDropDown();

        return false;
    }
});

答案 3 :(得分:1)

要添加@Ayaz Alifov的答案并补救@Aballano提出的问题, 这是带有kotlin代码的更完整的AutoSuggestAdapter:


import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Filter
import android.widget.TextView

class AutoSuggestAdapter<T : Any>(context: Context, private val resource: Int, items : List<T> = ArrayList()) :
    ArrayAdapter<T>(context, resource, items) {

    private val items : ArrayList<T> = ArrayList()
    private val suggestions : ArrayList<T> = ArrayList()
    var customLayoutHandler : ((View, T) -> Unit)? = null

    companion object {
        private const val TAG = "AutoSuggestAdapter"
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        lateinit var v : View
        v = if(convertView == null){
            val inflater : LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            inflater.inflate(resource, parent, false)
        } else convertView

        val item : T = suggestions[position]
        if(v is TextView) v.text = item.toString()
        else customLayoutHandler?.invoke(v,item)
        return v
    }

    override fun getFilter() : Filter = itemFilter

    override fun add(item: T?) {
        if(item != null) super.add(item)
        if(item != null && !items.contains(item)) items.add(item)
    }

    override fun addAll(vararg items: T) {
        super.addAll(*items)
        this.items.addAll(items)
    }

    override fun addAll(collection: MutableCollection<out T>) {
        super.addAll(collection)
        this.items.addAll(collection)
    }

    fun clearItems(){
        items.clear()
    }

    inner class ItemFilter() : Filter() {
        override fun performFiltering(constraint: CharSequence?): FilterResults {
            if(constraint != null){
                suggestions.clear()
                items.forEach { item ->
                    if(item.toString().toLowerCase().contains(constraint.toString().toLowerCase())) suggestions.add(item)
                }

                val fResults : FilterResults = FilterResults()
                fResults.values = suggestions
                fResults.count = suggestions.size
                return fResults
            }
            return FilterResults()
        }

        override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
            if(results != null && results.count > 0){
                var filterList : ArrayList<T> = results?.values as ArrayList<T>
                clear()
                filterList.forEach { item -> add(item) }
                notifyDataSetChanged()
            }
        }

        override fun convertResultToString(resultValue: Any?): CharSequence {
            return resultValue?.toString() ?: ""
        }
    }
    private val itemFilter : ItemFilter = ItemFilter()
}

对于字符串或任何其他原始类型(或具有有效toString表示形式的对象/类),它应该开箱即用。它还允许您指定一个自定义函数来处理要使用非标准布局(不仅仅是TextView)的自定义对象。它会传递顶级视图容器以及您要在下拉菜单中表示的项目。

答案 4 :(得分:0)

在您的情况下,您可以使用String.contains代替String.startsWith

看到这个问题: custom-autocompletetextview-behavior

答案 5 :(得分:0)

如果您想在其中添加数据

String[] arr=new String[100];

然后错了。 您可以执行与ArrayList形式相同的工作,但请记住,您永远不会在这里放置Getter / Setter类。只需声明一下即可。看到这个例子。

在主分区中声明:

   ArrayList<String>arr=new ArrayList<>();

,然后以这种方式将其初始化:

for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
                String imei = jsonObject1.getString("imei");
                String name = jsonObject1.getString("name");
                String my_pic = jsonObject1.getString("my_pic");
                String email = jsonObject1.getString("email");

                arr.add(name);
            }


            adapter= new ArrayAdapter<>
                    (this, android.R.layout.select_dialog_item, arr);
            autoCompleteText.setThreshold(1);//will start working from first character
            autoCompleteText.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
            autoCompleteText.setTextColor(Color.RED);


        }

我希望这对您有用。