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

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

  1. 选择建议项时, AutoCompleteTextView 它总是设置为原始的第一项 建议数组,数据是否被过滤。
  2. 在 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();
            }
        }
    };
}

【问题讨论】:

    标签: android android-adapter android-filterable


    【解决方案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();
                }
            }
    

    我就是这样做的。

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      相关资源
      最近更新 更多