Recycleview项目根据搜索textinput字段进行过滤

时间:2015-06-02 05:39:23

标签: android android-layout

我有一个搜索文本输入字段和一个加载了适配器项目的循环视图,当我在搜索字段中输入数据时,循环视图适配器必须根据搜索输入进行过滤,然后显示它。但是,在我将数据输入搜索字段后,我无法过滤其显示旧时的内容。请帮我解决错误并告诉我如何解决它

SearchInput代码:

txtDynamicSearch.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) {
            //DynamicSearchActivity.this.arrayAdapter.getFilter().filter(s);

DynamicSearchActivity.this.customListAdapter.getFilter().filter(s);

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

过滤代码:

public class CustomListAdapter extends 
RecyclerView.Adapter<CustomListAdapter.MyViewHolder>
    implements Filterable{

private ArrayList<samples> allsamplesList;
private ArrayList<samples> filteredsamplesList;
private Activity context;
private LayoutInflater inflater;
private sampleFilter filter;
Integer imageId = R.drawable.icon;
RecyclerView recyclerView;

public static class MyViewHolder extends RecyclerView.ViewHolder {

    TextView textViewName;
    TextView textViewLanguage;
    //ImageView imageViewIcon;*/

    public MyViewHolder(View itemView) {
        super(itemView);
        this.textViewName = (TextView) 
itemView.findViewById(R.id.textViewName);
        this.textViewLanguage = (TextView) 
itemView.findViewById(R.id.textViewLanguage);
        /*this.imageViewIcon = (ImageView) 
itemView.findViewById(R.id.imageView);
    */}
}


public CustomListAdapter(Activity context, ArrayList<samples> list) {
    //super(context, R.layout.item, list);
    this.context = context;
    this.allsamplesList = new ArrayList<samples>();
    allsamplesList.addAll(list);
    this.filteredsamplesList = new ArrayList<samples>();
    filteredsamplesList.addAll(allsamplesList);
    inflater = context.getLayoutInflater();


    getFilter();
}



@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.cards_layout, viewGroup, false);

    //view.setOnClickListener(DynamicSearchActivity);

    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;

}

@Override
public void onBindViewHolder(MyViewHolder myViewHolder, int i) {

    TextView textViewName = myViewHolder.textViewName;
    TextView textViewLanguage = myViewHolder.textViewLanguage;


    textViewName.setText(allsamplesList.get(i).getsample_name());
    textViewLanguage.setText(allsamplesList.get(i).getsample_language());


}

@Override
public int getItemCount() {
    return allsamplesList.size();
}

@Override
public Filter getFilter() {

    if (filter == null) {
        filter = new sampleFilter(this);
    }
    return filter;
}


private class sampleFilter extends Filter {

    private CustomListAdapter adapter;

    private sampleFilter(CustomListAdapter adapter) {
        super();

        this.adapter = adapter;

    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        constraint = constraint.toString().toUpperCase();
        FilterResults results = new FilterResults();
        if (constraint != null && constraint.toString().length() > 0) {
            ArrayList<samples> filteredItems = new ArrayList<samples>();

            for (int i=0; i < allsamplesList.size(); i++ ) {
                samples samples = allsamplesList.get(i);
                if (samples.getsample_name().contains(constraint)) {
                    filteredItems.add(samples);
                    notifyItemRangeChanged(i,allsamplesList.size());

                }
            }
            results.count = filteredItems.size();
            results.values = filteredItems;
        }
        else {
            synchronized (this) {
                results.count = allsamplesList.size();
                results.values = allsamplesList;
            }
        }

        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults 
results) {

        filteredsamplesList = (ArrayList<samples>) results.values;

       Collections.sort(filteredsamplesList, new Comparator<samples>() {
            @Override
            public int compare(samples lhs, samples rhs) {
                return lhs.getsample_name().compareTo(rhs.getsample_name());
            }
        });

        notifyDataSetChanged();
        adapter.notifyDataSetChanged();

        adapter.filteredsamplesList.clear();

        adapter.filteredsamplesList.addAll((ArrayList<samples>) 
results.values);


        adapter.filteredsamplesList = (filteredsamplesList);
          int n = results.count;

        for (int i=0; i < filteredsamplesList.size(); i++) {
            adapter.filteredsamplesList.add(filteredsamplesList.get(i));
            adapter.notifyItemInserted(i);
            adapter.notifyDataSetChanged();
            //notifyItemRangeChanged(0,n);
        }

        adapter.notifyDataSetChanged();
        notifyDataSetInvalidated();

        adapter.notifyDataSetChanged();



        notifyDataSetChanged();


        Log.e("values", constraint.toString());
        //Log.e("values", 
String.valueOf((filteredsamplesList.get(0)).sample_name));
    }


}


}

0 个答案:

没有答案