Recyclerview过滤器无法正常工作。不搜索元素

时间:2017-03-28 06:58:38

标签: filter android-recyclerview recycler-adapter

when i filter recyclerview it shows Not found .My Searchview not working.when i run the code its result in Not Found i think there is problem in onQueryTextChange
myfilter function also did not work 

    @Override
    public boolean onQueryTextSubmit(String query) {

    Toast.makeText(SecondActivity1.this, "Name is : " + query, Toast.LENGTH_SHORT).show();
    return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

    final List<DatabaseModel> filteredModelList = filter(dbList, newText);
    if (filteredModelList.size() > 0) {
      //  Toast.makeText(SecondActivity1.this, "Found", Toast.LENGTH_SHORT).show();
        recyclerAdapter.setFilter(filteredModelList);
        return true;
    } else {
        Toast.makeText(SecondActivity1.this, "Not Found", Toast.LENGTH_SHORT).show();
        return false;
    }

私人列表过滤器(列表模型,字符串查询){

    query = query.toLowerCase();
    recyclerAdapter.notifyDataSetChanged();
    final List<DatabaseModel> filteredModelList = new ArrayList<>();
  //  mRecyclerView.setLayoutManager(new LinearLayoutManager(SecondActivity1.this));
    //  mRecyclerView.setAdapter(RecyclerAdapter);

    for (DatabaseModel model : models) {
        final String text = model.getName().toLowerCase();
        if (text.contains(query)) {
            filteredModelList.add(model);

        }
    }

    return filteredModelList;

  //
}

这里是收集参数(dblist,newtext)过滤器方法的过滤方法,当我使用toast它的节目需要newText但是没有过滤这个时我收到了这些方法。我检查了很多站点但是这在许多站点中是相同的。当我输入名称toast显示我输入但未过滤的名称

RecyclerAdapter.java
package com.example.prabhu.databasedemo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;

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

/**
 * Created by user_adnig on 11/14/15.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>  {
    List<DatabaseModel> dbList;
    static  Context context;
    RecyclerAdapter(Context context, List<DatabaseModel> dbList ){
        this.dbList = new ArrayList<>();
        this.context = context;
        this.dbList = (ArrayList<DatabaseModel>) dbList;

    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.item_row, null);

        // create ViewHolder

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int position) {

        holder.name.setText(dbList.get(position).getName());
        holder.email.setText(dbList.get(position).getEmail());

    }

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



    public void setFilter(List<DatabaseModel> countryModels) {
      //  Toast.makeText(RecyclerAdapter.this,"Method is called", Toast.LENGTH_SHORT).show();
        dbList = new ArrayList<>();
        dbList.addAll(countryModels);
        notifyDataSetChanged();
    }



    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView name,email;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            name = (TextView) itemLayoutView
                    .findViewById(R.id.rvname);
            email = (TextView)itemLayoutView.findViewById(R.id.rvemail);
            itemLayoutView.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context,DetailsActivity.class);

            Bundle extras = new Bundle();
            extras.putInt("position",getAdapterPosition());
            intent.putExtras(extras);

            /*
            int i=getAdapterPosition();
            intent.putExtra("position", getAdapterPosition());*/
            context.startActivity(intent);
            Toast.makeText(RecyclerAdapter.context, "you have clicked Row " + getAdapterPosition(), Toast.LENGTH_LONG).show();
        }
    }

}

这是我的recyclerAdapterCode.i也使用了Recycleradapter.setFilter(filterModeList)方法,但它对我没用。我想我的set filter方法错误,我还没解决。 。但是当我清除搜索小部件时,我没有得到完整列表,而是获得了空的RecyclerView。

0 个答案:

没有答案