AutocompleteTextView 建议总是选择建议中的第一项

Posted

技术标签:

【中文标题】AutocompleteTextView 建议总是选择建议中的第一项【英文标题】:AutocompleteTextView suggestions always selecting the first item in suggestions 【发布时间】:2019-05-27 07:53:55 【问题描述】:

我遇到了一个我无法解决的问题。我创建了一个 AutoCompleteTextView,它显示来自远程 JSON 数据的过滤建议。建议框弹出并正确过滤。我有两个我无法理解的问题。

    选择建议项时, AutoCompleteTextView 它总是设置为原始的第一项 建议数组,数据是否被过滤。 在 AutocompleteTextview 中删除字符时,如果变成 empty 抛出空指针异常

我的自定义适配器的代码:

package tz.co.fsm.fas;

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.Filterable;
import android.widget.TextView;

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

public class office_locations_adapter extends ArrayAdapter<office_location_data> implements Filterable 
    private Context context;
    private List<office_location_data> items, tempItems, suggestions;
    int row_layout;

    public office_locations_adapter(Context context, int row_layout, List<office_location_data> items) 
        super(context, row_layout, items);
        this.context = context;
        this.row_layout = row_layout;
        this.items = items;
        tempItems = new ArrayList<>(items); 
        suggestions = new ArrayList<>();
    

    @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(row_layout, parent, false);
        

        office_location_data office = items.get(position);

        if (office != null) 
            TextView lbloffice = view.findViewById(R.id.rowtxtOfficeLocation);
            if (lbloffice != null) 
                lbloffice.setText(office.getOffice_location());
            
        

        return view;
    

    // This is important as it returns the count of the new arraylist when we filter it.
    @Override
    public int getCount() 
        return items.size();
    

    // Method to return the filter
    @Override
    public Filter getFilter() 
        return performFiletring;
    

    // Create a new filter. Here we perform the filtering results and use this in the getFilter() method
    Filter performFiletring = new Filter() 
        @Override
        public CharSequence convertResultToString(Object resultValue) 
            String str = ((office_location_data) resultValue).getOffice_location();
            return str;
        

        @Override
        protected FilterResults performFiltering(CharSequence constraint) 
            // We have some text to search
            if (constraint != null) 
                // CLear the suggestions array
                suggestions.clear();
                // Find the rows which match and add them to suggestions
                for (office_location_data offices : tempItems) 
                    if (offices.getOffice_location().toLowerCase().contains(constraint.toString().toLowerCase())) 
                        suggestions.add(offices);
                    
                
                // Pass the filter results to the next step publish results
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
//                System.out.println(filterResults.count);
                return filterResults;
             else 
                return new FilterResults();
            
        

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) 
            items = (List) results.values;
            if (results.count > 0) 
                //suggestions = (office_location_data) results.values;
                notifyDataSetChanged();
             else 
                notifyDataSetInvalidated();
            
        
    ;

【问题讨论】:

【参考方案1】:

发现问题。 publishResults() 方法错误:

@Override
        protected void publishResults(CharSequence constraint, FilterResults results) 
            List<office_location_data> items = (ArrayList<office_location_data>) results.values;
            if (results != null && results.count > 0) 
                clear();
                for (office_location_data offices : suggestions) 
                    add(offices);
                    notifyDataSetChanged();
                
             else 
                notifyDataSetInvalidated();
            
        

我就是这样做的。

【讨论】:

以上是关于AutocompleteTextView 建议总是选择建议中的第一项的主要内容,如果未能解决你的问题,请参考以下文章

动态更新 AutoCompleteTextView 适配器

自定义 AutoCompleteTextView 行为

Google 在 Appcompactivity - Android 中对 AutoCompleteTextView 进行 api 调用

AutoCompleteTextView 的自定义过滤器在单击时返回错误的字符串

适配器中的AutoCompleteTextView无法正常工作

与 Uber 一样,在 ListView 中显示带有 Google Places 的 AutoCompleteTextView