Java AutoCompleteTextView未按预期工作

Java AutoCompleteTextView未按预期工作,java,android,autocomplete,Java,Android,Autocomplete,我正在开发一个使用AutoCompleteTextView的应用程序,遇到的问题很少。请查看以下问题的详细信息 数据中存在以下值: List<String> namesList = new ArrayList<String>(stops); namesList.add("Manish Logan Jain"); namesList.add("Logan"); namesList.add("M. J. (Logan Fern)");

我正在开发一个使用AutoCompleteTextView的应用程序,遇到的问题很少。请查看以下问题的详细信息

数据中存在以下值:

    List<String> namesList = new ArrayList<String>(stops);
    namesList.add("Manish Logan Jain");
    namesList.add("Logan");
    namesList.add("M. J. (Logan Fern)");

    ArrayAdapter<String> namesSuggestion = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, namesList);
    AutoCompleteTextView textView = (AutoCompleteTextView)                       findViewById(R.id.autoCompleteTextView1);
     textView.setAdapter(namesSuggestion);
     textView.setThreshold(1);
1) 曼尼什洛根耆那教

2) M.J.(洛根·弗恩)

3) 洛根

问题:

1) 当用户搜索Manish时,Manish Logan Jain显示为建议。但当用户输入Logan Jain时,不会返回任何结果

2) 当用户输入Logan时,我希望第二个值显示为建议,但目前建议列表没有显示任何内容

3) 当用户输入ogan时,我希望显示建议3。目前,它还没有显示

自动完成视图xml:

自动完成文本视图

    android:id="@+id/autoCompleteTextView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:hint="@string/enter_user_name" >

    <requestFocus />
</AutoCompleteTextView>
android:id=“@+id/autoCompleteTextView1”
android:layout\u width=“匹配父项”
android:layout\u height=“包装内容”
android:layout\u alignParentLeft=“true”
android:layout\u alignParentTop=“true”
android:ems=“10”
android:hint=“@string/enter_user_name”>
填充数据的Java代码:

    List<String> namesList = new ArrayList<String>(stops);
    namesList.add("Manish Logan Jain");
    namesList.add("Logan");
    namesList.add("M. J. (Logan Fern)");

    ArrayAdapter<String> namesSuggestion = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, namesList);
    AutoCompleteTextView textView = (AutoCompleteTextView)                       findViewById(R.id.autoCompleteTextView1);
     textView.setAdapter(namesSuggestion);
     textView.setThreshold(1);
List namesList=newarraylist(停止);
名称列表。添加(“Manish Logan Jain”);
名称列表。添加(“Logan”);
名称列表。添加(“M.J.(洛根蕨类植物)”;
ArrayAdapter namesSuggestion=新的ArrayAdapter(这是android.R.layout.simple\u下拉列表\u item\u 1line,名称列表);
AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
setAdapter(名称建议);
设置阈值(1);

有人遇到过类似的问题吗?如果是,那么可能的解决方案是什么?

使用实现可过滤的自定义适配器。在getFilter方法中,根据需要使用String.contains()


请检查是否使用实现可过滤功能的自定义适配器。在getFilter方法中,根据需要使用String.contains()


请检查您要查找的内容是无法直接完成的。但我建议您使用MultiCompleteTextView

请参阅此链接:

您要找的东西无法直接完成。但我建议您使用MultiCompleteTextView

请参阅此链接:
请尝试以下代码:

public class AutoCompleteAdapter extends ArrayAdapter<Address> implements Filterable {

    private LayoutInflater mInflater;
    private Geocoder mGeocoder;
    private StringBuilder mSb = new StringBuilder();

    public AutoCompleteAdapter(final Context context) {
        super(context, -1);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mGeocoder = new Geocoder(context);
    }

    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
        final TextView tv;
        if (convertView != null) {
            tv = (TextView) convertView;
        } else {
            tv = (TextView) mInflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
        }

        tv.setText((createFormattedAddressFromAddress(getItem(position))));
        return tv;
    }

    private String createFormattedAddressFromAddress(final Address address) {
        mSb.setLength(0);
        final int addressLineSize = address.getMaxAddressLineIndex();
        for (int i = 0; i < addressLineSize; i++) {
            mSb.append(address.getAddressLine(i));
            if (i != addressLineSize - 1) {
                mSb.append(", ");
            }
        }
        return mSb.toString();
    }

    @Override
    public Filter getFilter() {
        Filter myFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(final CharSequence constraint) {
                List<Address> addressList = null;
                if (constraint != null) {
                    try {
                        addressList = mGeocoder.getFromLocationName((String) constraint, 5,23.0,72.0,23.9,72.9);
                    } catch (IOException e) {
                    }
                }
                if (addressList == null) {
                    addressList = new ArrayList<Address>();
                }

                final FilterResults filterResults = new FilterResults();
                filterResults.values = addressList;
                filterResults.count = addressList.size();

                return filterResults;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(final CharSequence contraint, final FilterResults results) {
                clear();
                for (Address address : (List<Address>) results.values) {
                    add(address);
                }
                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }

            @Override
            public CharSequence convertResultToString(final Object resultValue) {
                return resultValue == null ? "" : ((createFormattedAddressFromAddress((Address) resultValue).split(", Ahmedabad")[0].length()<6)?createFormattedAddressFromAddress((Address) resultValue).split(", Gujarat")[0]:createFormattedAddressFromAddress((Address) resultValue).split(", Ahmedabad")[0]);
            }
        };
        return myFilter;
    }
}
公共类自动完成适配器扩展ArrayAdapter实现可过滤{
私人停车场;
私人地理编码器;
私有StringBuilder mSb=新StringBuilder();
公共自动完成适配器(最终上下文){
super(上下文-1);
mInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
mGeocoder=新地理编码器(上下文);
}
@凌驾
公共视图getView(最终整型位置、最终视图转换视图、最终视图组父视图){
最终文本视图电视;
if(convertView!=null){
电视=(文本视图)转换视图;
}否则{
tv=(TextView)mInflater.flate(android.R.layout.simple\u dropdown\u item\u 1line,parent,false);
}
setText((createFormattedAddressFromAddress(getItem(position)));
返回电视;
}
私有字符串createFormattedAddressFromAddress(最终地址){
mSb.setLength(0);
final int addressLineSize=address.getMaxAddressLineIndex();
对于(int i=0;i0){
notifyDataSetChanged();
}否则{
notifyDataSetionValidated();
}
}
@凌驾
public CharSequence ConvertResultString(最终对象结果值){

return resultValue==null?”:((createFormattedAddressFromAddress((Address)resultValue).split(“,Ahmedabad”)[0]。length()请尝试以下代码:

public class AutoCompleteAdapter extends ArrayAdapter<Address> implements Filterable {

    private LayoutInflater mInflater;
    private Geocoder mGeocoder;
    private StringBuilder mSb = new StringBuilder();

    public AutoCompleteAdapter(final Context context) {
        super(context, -1);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mGeocoder = new Geocoder(context);
    }

    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
        final TextView tv;
        if (convertView != null) {
            tv = (TextView) convertView;
        } else {
            tv = (TextView) mInflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
        }

        tv.setText((createFormattedAddressFromAddress(getItem(position))));
        return tv;
    }

    private String createFormattedAddressFromAddress(final Address address) {
        mSb.setLength(0);
        final int addressLineSize = address.getMaxAddressLineIndex();
        for (int i = 0; i < addressLineSize; i++) {
            mSb.append(address.getAddressLine(i));
            if (i != addressLineSize - 1) {
                mSb.append(", ");
            }
        }
        return mSb.toString();
    }

    @Override
    public Filter getFilter() {
        Filter myFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(final CharSequence constraint) {
                List<Address> addressList = null;
                if (constraint != null) {
                    try {
                        addressList = mGeocoder.getFromLocationName((String) constraint, 5,23.0,72.0,23.9,72.9);
                    } catch (IOException e) {
                    }
                }
                if (addressList == null) {
                    addressList = new ArrayList<Address>();
                }

                final FilterResults filterResults = new FilterResults();
                filterResults.values = addressList;
                filterResults.count = addressList.size();

                return filterResults;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(final CharSequence contraint, final FilterResults results) {
                clear();
                for (Address address : (List<Address>) results.values) {
                    add(address);
                }
                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }

            @Override
            public CharSequence convertResultToString(final Object resultValue) {
                return resultValue == null ? "" : ((createFormattedAddressFromAddress((Address) resultValue).split(", Ahmedabad")[0].length()<6)?createFormattedAddressFromAddress((Address) resultValue).split(", Gujarat")[0]:createFormattedAddressFromAddress((Address) resultValue).split(", Ahmedabad")[0]);
            }
        };
        return myFilter;
    }
}
公共类自动完成适配器扩展ArrayAdapter实现可过滤{
私人停车场;
私人地理编码器;
私有StringBuilder mSb=新StringBuilder();
公共自动完成适配器(最终上下文){
super(上下文-1);
mInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
mGeocoder=新地理编码器(上下文);
}
@凌驾
公共视图getView(最终整型位置、最终视图转换视图、最终视图组父视图){
最终文本视图电视;
if(convertView!=null){
电视=(文本视图)转换视图;
}否则{
tv=(TextView)mInflater.flate(android.R.layout.simple\u dropdown\u item\u 1line,parent,false);
}
tv.setText((createForm
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
AutoCompleteTextView actv = new AutoCompleteTextView(this);
String[] from = {"name"};
int[] to = {android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to);
FQP fqp = new FQP();
fqp.add("Manish Logan Jain");
fqp.add("Logan");
fqp.add("M. J. (Logan Fern)");
adapter.setFilterQueryProvider(fqp);
actv.setAdapter(adapter);
actv.setThreshold(1);
ll.addView(actv);
setContentView(ll);