我正在尝试在工具栏中实现SearchView。当我尝试搜索我的列表时,在尝试搜索时,总是返回原始列表而不是新列表。
当我调试时,它开始显示包含正确数量的项目的新列表,但是当它完成后,它只返回原始列表大小。
调试时,它会进入 publicResults,但随后不会更新适配器。不知道为什么它会获得新列表但不替换旧列表。
实现搜索视图的活动
public class LoadsActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
@Inject
Lazy<IMyLoadsRetroService> retroService;
@Inject
IUser user;
public LoadsActivity() {
Injector.INSTANCE.getViewComponent(this).inject(this);
}
public static void startLoadsActivity(Context context) {
Intent loadsIntent = new Intent(context, LoadsActivity.class);
context.startActivity(loadsIntent);
}
@BindView(R.id.recyclerView)
RecyclerView recyclerView;
@BindView(R.id.loadProgressBar)
ProgressBar loadProgressBar;
@BindView(R.id.fab)
FloatingActionButton fab;
LoadAdapter loadAdapter;
private List<IMyLoadSummary> myLoadSummaryList = new ArrayList<>();
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loads);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ButterKnife.bind(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
loadAdapter = new LoadAdapter(this, myLoadSummaryList);
recyclerView.setAdapter(loadAdapter);
loadProgressBar.setVisibility(View.VISIBLE);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.startLoadsActivity(LoadsActivity.this);
}
});
retroService.get().getMyLoads().subscribeOn(Schedulers.io()).observeOn(rx.android.schedulers.AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<MyLoadSummary>>() {
@Override
public void onCompleted() {
loadProgressBar.setVisibility(View.GONE);
}
@Override
public void onError(Throwable e) {
Toaster.s(LoadsActivity.this, e.getMessage());
}
@Override
public void onNext(List<MyLoadSummary> myLoadSummaries) {
loadAdapter.setLoadData(myLoadSummaries);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
//leaving blank to disable back button
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
loadAdapter.getFilter().filter(newText);
return true;
}
}
带过滤器的适配器
public class LoadAdapter extends RecyclerView.Adapter<LoadAdapter.ViewHolder> implements Filterable {
private Context mContext;
private List<? extends IMyLoadSummary> originalList;
private List<? extends IMyLoadSummary> filteredList;
public LoadAdapter(Context context, List<IMyLoadSummary> list) {
this.mContext = context;
this.originalList = list;
this.filteredList = list;
getFilter();
}
public void setLoadData(List<? extends IMyLoadSummary> mLoadData) {
originalList = mLoadData;
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_row, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Gson gson = GsonFactory.getDefault();
String jsonListString = gson.toJson(originalList.get(position));
holder.loadsTextView.setText(jsonListString);
}
@Override
public int getItemCount() {
return originalList.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filteredResults = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<IMyLoadSummary> tempList = new ArrayList<>();
//search content for any number...hopefully
for (IMyLoadSummary myLoadSummary : originalList) {
if (Integer.toString(myLoadSummary.getNumber()).contains(constraint.toString())) {
tempList.add(myLoadSummary);
}
}
filteredResults.count = tempList.size();
filteredResults.values = tempList;
} else {
filteredResults.count = originalList.size();
filteredResults.values = originalList;
}
return filteredResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredList = (List<? extends IMyLoadSummary>) results.values;
notifyDataSetChanged();
}
};
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView loadsTextView;
public ViewHolder(View itemView) {
super(itemView);
loadsTextView = (TextView) itemView.findViewById(R.id.loadJSONText);
}
}
}
我想建议的是将数据保存到Android SQLite中,然后运行选择*从table_name其中名称为"%string%"; 查询并在列表视图中显示它 .希望它对您有所帮助 这是一种简单的搜索方法,也是最快的