2016-11-05 120 views
0

我有RecyclerView和它的工作良好,但我需要實現過濾器來搜索RecyclerView我在互聯網搜索,但我不能成功,所以我想任何人都可以幫助我實現在我的RecyclerView過濾器這是我的代碼,請嘗試幫我如何在RecyclerView Android中實現搜索過濾器?

這是RecyclerView活動

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; 
    } 

,這是適配器

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); 
    } 
} 

請盡力幫助我,因爲這個方法是從主功能在我的應用程序

+0

歡迎堆棧溢出!請查看我們的[SO問題清單](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)來幫助你提出一個好問題,從而得到一個很好的答案。 –

+0

謝謝你,我會檢討它:)如果你對我的問題有任何想法,請告訴我:) –

回答

0

您可以使用your_adapter.getFilter()。過濾器(SEARCHQUERY)在onQueryTextChange。

+0

getFilter()?如何聲明這個方法? –

+0

不需要定義。它已經在那裏。 –

+0

它是陣列適配器類的一部分。你只需要使用它。 –

0

嘗試 recyclerAdapter.getFilter()。過濾器(SEARCHQUERY)在TodoList的Activity類

但是,如果你正在嘗試做這樣的事情 Action bar search

+0

當我但這,'todoAdapter.getFilter()。filter(searchQuery);''不能reslove getFilter() –

+0

我看到的例子,但我不能申請我的情況下,你能適用於我? –

相關問題