在Array Adapter中实现Filterable不会过滤任何结果 - 与过滤

时间:2018-05-03 14:16:04

标签: android android-edittext android-arrayadapter android-filterable

这是我第一次在我的代码中实现Filterable。我按照所有步骤进行了操作,但过滤仍未过滤任何结果。

  • 在我的数组适配器中实现了可过滤
  • 根据Array Adapter中的名称制作了我的过滤器
  • 覆盖了getFilter以使用我的新过滤器阵列适配器
  • 将edittext链接到数组适配器并请求过滤

我仍然看到与过滤前相同的列表。我错过了什么?请参阅下面的代码。提前谢谢!

public class OnlineShopsAdapter extends ArrayAdapter<OnlineShop> implements Filterable {
    private Context context;
    private List<OnlineShop> mOnlineShops;
    Filter mOnlineShopsFilter;

    public OnlineShopsAdapter (Context context, int resource, List<OnlineShop> objects){
        super(context, resource, objects);
        this.context = context;
        this.mOnlineShops = objects;
    }



    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        final OnlineShop onlineShop = getItem(position);

        if (convertView == null) {
            convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.one_online_shop_display, parent, false);
        }

        mOnlineShopsFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                Toast.makeText(getContext(),constraint,Toast.LENGTH_SHORT).show();
                FilterResults filterResults = new FilterResults();
                ArrayList<OnlineShop> tempList=new ArrayList<OnlineShop>();
                //constraint is the result from text you want to filter against.
                //objects is your data set you will filter from
                if(constraint != null && mOnlineShops!=null) {
                    int length=mOnlineShops.size();
                    int i=0;
                    while(i<length){
                        OnlineShop item=mOnlineShops.get(i);
                        //do whatever you wanna do here
                        //adding result set output array
                        if(item.getShopName().toUpperCase().contains(constraint.toString().toUpperCase() )) {
                            Toast.makeText(getContext(),"Im in",Toast.LENGTH_SHORT).show();
                            tempList.add(item);
                        }
                        i++;
                    }
                    //following two lines is very important
                    //as publish result can only take FilterResults objects
                    filterResults.values = tempList;
                    filterResults.count = tempList.size();
                }
                return filterResults;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence contraint, FilterResults results) {
                mOnlineShops = (ArrayList<OnlineShop>) results.values;
                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };

        return convertView;
    }

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

这是我的edittext所在的地方:

private GridView mOnlineShopsListView;
private ArrayList searchResultsKeysForShops;

@Nullable
@Override
public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.get_points,container,false);
    mOnlineShopsListView = (GridView) v.findViewById(R.id.online_shops_grid_view);

    //making the adapter work so it can show the Online Shops from the database

    mOnlineShopsListView.setAdapter(MainActivity.mOnlineShopsAdapter);

    editSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mOnlineShopsAdapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    return v;
}

和我的班级:

public class OnlineShop {
    private String shopName;
    private int preferenceScore;



    public OnlineShop (){
    }

    public String setShopName(String shopName) {
        this.shopName = shopName;
        keywords.add(shopName);
        return shopName;
    }
    public int setPreferenceScore(int preferenceScore) {
        this.preferenceScore = preferenceScore;
        return preferenceScore;
    }

    public String getShopName() { return shopName; }

    public int getPreferenceScore() { return preferenceScore; }

    public static Comparator<OnlineShop> OnlineShopPreferenceScoreComparator
            = new Comparator<OnlineShop>() {

        public int compare(OnlineShop onlineShop1, OnlineShop onlineShop2) {

            int onlineShop1PreferenceScore1 = onlineShop1.getPreferenceScore();
            int onlineShop1PreferenceScore2 = onlineShop2.getPreferenceScore();

            //ascending order
            //fruitName1.compareTo(fruitName2);
            //descending order
            //return fruitName2.compareTo(fruitName1);

            return onlineShop1PreferenceScore1 - onlineShop1PreferenceScore2;
        }

    };


}

1 个答案:

答案 0 :(得分:0)

我还必须覆盖以下方法:

public int getCount() {
    return mOnlineShops.size();
}

public OnlineShop getItem(int position) {
    return mOnlineShops.get(position);
}

public long getItemId(int position) {
    return position;
}