0

i have RecyclerView and its working good , but i need to implement filter to search into RecyclerViewi search in the internet but i cannot success , so i want anyone to help me to implement filter in my RecyclerView this is my code , please try to help me

this is RecyclerView Activity

    public class TodoList extends AppCompatActivity implements DialogFragUpdateListener {
    RecyclerView todoRecyclerView;
    private RecyclerView.Adapter todoAdapter;
    private RecyclerView.LayoutManager todoLayoutManager;
    public List<ToDo> results;
    public List<String> list = new ArrayList<>();
    TodoRecyclerAdapter recyclerAdapter;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todo_list);

        todoRecyclerView = (RecyclerView)findViewById(R.id.todoRecyclerView);
        todoRecyclerView.setHasFixedSize(true);
        results= new ArrayList<ToDo>();
        todoLayoutManager = new LinearLayoutManager(this);
        todoRecyclerView.setLayoutManager(todoLayoutManager);
 getRetrofitObject();
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TodoList.this,AddToDo.class);
                startActivity(intent);
            }
        });}

    public void getRetrofitObject() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        APIService service = retrofit.create(APIService.class);
        Call<Result> call = service.getresults();
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                results = response.body().getResults();
                todoRecyclerView.setAdapter(new TodoRecyclerAdapter(this,results));
            }
            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                Log.d("onFailure", t.toString());
            }});}
 public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {

            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {

                return true;
            }

            @Override
            public boolean onQueryTextChange(String searchQuery) {

                return true;

            }
        });
        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                return true;
            }
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;
            }
        });
        return true;
    }

and this is Adapter

public class TodoRecyclerAdapter extends RecyclerView.Adapter<TodoRecyclerAdapter.ViewHolder> {

static List<ToDo> todoResults;
static Context context;
List<String> results;
private List<ToDo> orig;
private List<ToDo> items;
private ArrayList<ToDo> arrayList = null, stringArrayList;


public TodoRecyclerAdapter(Callback<Result> callback, List<ToDo> results) {
    this.todoResults = results;
    arrayList = (ArrayList<ToDo>) results;

}



@Override
public TodoRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_items, null);
    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

@Override
public void onBindViewHolder(final TodoRecyclerAdapter.ViewHolder holder, final int position) {
    holder.todoTitle.setText(todoResults.get(position).getTODO_TITLE().toString());
    String priority = todoResults.get(position).getPriority().toString();
    if (priority.equals("Low")) {
        holder.todoImage.setImageResource(R.drawable.low);
    } else if (priority.equals("Normal")) {
        holder.todoImage.setImageResource(R.drawable.normal);
    } else if (priority.equals("High")) {
        holder.todoImage.setImageResource(R.drawable.high);
    }
}

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

public static class ViewHolder extends RecyclerView.ViewHolder {
    public TextView todoTitle;
    public ImageView todoImage;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        todoTitle = (TextView) itemLayoutView.findViewById(R.id.todo_title);
        todoImage = (ImageView) itemLayoutView.findViewById(R.id.imageView2);
    }
}

Please Try to Help me because this method is from the main functionally in my app

Mr.moayed
  • 631
  • 3
  • 9
  • 15
  • Welcome to Stack Overflow! Please review our [SO Question Checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) to help you to ask a good question, and thus get a good answer. – Joe C Nov 05 '16 at 18:58
  • thank you i will review it :) and if you have any idea about my problem please tell me :) – Mr.moayed Nov 05 '16 at 19:00

2 Answers2

0

You can use your_adapter.getFilter().filter(searchQuery) in onQueryTextChange.

0

try recyclerAdapter.getFilter().filter(searchQuery) in your TodoList Activity class

But if you are trying to do something like this Action bar search

Community
  • 1
  • 1
Sajidh Zahir
  • 526
  • 4
  • 13