AutoCompleteTextView 没有完成括号内的单词 [英] AutoCompleteTextView not completing words inside parentheses

查看:16
本文介绍了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 适配器 = new ArrayAdapter(getActivity(),R.layout.autocompletetextview_row,R.id.textViewItem,myData);text.setAdapter(适配器);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 对选中的文本做一些事情}});

AutoCompleteTextView的布局中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"机器人:背景=#FFFFFF"android:padding="10dp" ><文本视图android:id="@+id/textViewItem"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:text="这里的项目名称..."android:textColor="#000000"android:textSize="20sp"/></RelativeLayout>

当我输入af.."时,它显示我Africa (AF)选择但不是America (AFM).

数据只是示例数据.并不是我将 AutoCompleteTextView 仅用于 3 个项目.

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

解决方案

ArrayAdapterfilter默认实现正在搜索开头单词(用空格分隔),我的意思是它使用 startsWith.您需要实现一个 ArrayFilter,它使用 containsstartsWith.

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

  • 希望这有帮助!

    I have implemented AutoCompleteTextView as follows:

    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
            }
        });
    

    In layout of 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>
    

    When I type like "af..", it shows me Africa (AF) to select but not America (AFM).

    The data is just an example data. it's not that I am using AutoCompleteTextView for just 3 items.

    EDIT: when I remove the parentheses, it works properly. But I need to keep the parentheses for further use.

    解决方案

    The default implementation of the filter for ArrayAdapter is searching the beginning of words (separated by space), I mean it uses startsWith. You will need to implement an ArrayFilter which uses contains together with startsWith.

    Your issue will be solved by the following steps:

    • Download the ArrayAdapter.java file from here
    • Add that file into the project (you can refactor by renaming file to CustomArrayAdapter.java, for example).
    • In the file, you will find a private class ArrayFilter. Then, add valueText.contains(prefixString) and words[k].contains(prefixString) as the following:

                  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;
                          }
                      }
                  }
      

    • Use that customized ArrayAdapter for your AutoCompleteTextView

    And here is the result screenshot:

    Hope this helps!

    这篇关于AutoCompleteTextView 没有完成括号内的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆