我有RecyclerView从对象数组中获取数据,我试图像这样在Recycler View适配器中生成过滤器,
public void filter(String filterString) {
filterString = filterString.toLowerCase(Locale.getDefault());
results.clear();
if(filterString.length() == 0) {
}
else {
for( ToDo item : stringArrayList){
if(item.getTODO_TITLE().toLowerCase(Locale.getDefault()).contains(filterString)) {
results.add(item.getTODO_TITLE() );
}
}
}
notifyDataSetChanged();
}
}
当我在这样的菜单中调用它时
@Override
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) {
if (searchQuery.length() != 0) {
if (null != recyclerAdapter) {
recyclerAdapter.filter(searchQuery.toString());
todoRecyclerView.invalidate();
todoAdapter.notifyDataSetChanged();
}
}
else{
}
return false;
}
});
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}}
它无法正常工作,并且没有显示任何错误!我怎么能解决这个问题????
这是我的代码进入回收商
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());
}});}
Recycler查看 Adpater
public class TodoRecyclerAdapter extends RecyclerView.Adapter<TodoRecyclerAdapter.ViewHolder> {
static List<ToDo> todoResults;
static Context context;
List<String> results;
private List<String> mDefaultCheeses;
private List<ToDo> mData;
private List<ToDo> originalData;
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);
}
}
public void filter(String filterString) {
filterString = filterString.toLowerCase(Locale.getDefault());
results.clear();
if(filterString.length() == 0) {
}
else {
for( ToDo item : stringArrayList){
if(item.getTODO_TITLE().toLowerCase(Locale.getDefault()).contains(filterString)) {
results.add(item.getTODO_TITLE() );
}
}
}
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
我认为错误不在日志中,因为您没有向结果添加元素。您必须记录您的代码,看看那里发生了什么。
也许这会对你有所帮助:
在filter
方法中试试这个:
ArrayList<Note> result;
List<ToDO>itemsCopy = new ArrayList<>();
private void filter(String text){
itemsCopy= items in results arraylist
result = new ArrayList<>();
text = text.toLowerCase();
for(ToDO item: itemsCopy){
if(item.getTitle().toLowerCase().contains(text)){
result.add(item);
}
}
results.clear();
results.addAll(result);
notifyDataSetChanged();
//Log your code! Always helps
Log.i(TAG, "---RESULT--" +result.toString());
}
然后在OnCreateOptionsMenu
:
@Override
public boolean onQueryTextSubmit(String query) {
filter(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
filter(newText);
return true;
}
});
希望它有所帮助!