我尝试通过在工具栏上放置SearchView来过滤RecyclerView,我从25分钟开始关注this教程,因为它正好表达了我在寻找的内容, 从他们使用的教程实现" ArrayList newList = new ArrayList<>()"哪个工作正常,但我想用这个表单实现它 " ArrayList> newList = new ArrayList<>()"因为我已经完成了项目的格式。我试着这样做:
///活动
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
// DB Class to perform DB related operations
DBController controller = new DBController(MainActivity.this);
// Progress Dialog Object
ArrayList<HashMap<String, String>> arrayList= new ArrayList<HashMap<String, String>> ();
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private UsersAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get User records from SQLite DB
ArrayList<HashMap<String, String>> userList = controller.getAllUsers();
// If users exists in SQLite DB
if (userList.size() != 0) {
recyclerView = (RecyclerView) findViewById(R.id.usersList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
controller = new DBController(MainActivity.this);
ArrayList<HashMap<String, String>> users = controller.getAllUsers();
adapter = new UsersAdapter(users,this);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
ArrayList<HashMap<String, String>> newList= new ArrayList<>();
for( HashMap<String, String> entry : arrayList){
String name= entry.put("userName",newText);
if(name.contains(newText)){
newList.add(entry);
}}
adapter.setFilter(newList);
return true;
}
}
///适配器类
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserViewHolder> {
ArrayList<HashMap<String, String>> mDataSet= new ArrayList<HashMap<String, String>> ();
Context ctx;
public UsersAdapter(ArrayList<HashMap<String, String>> mDataSet,Context ctx) {
this.mDataSet = mDataSet;
this.ctx=ctx;
}
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_row, parent, false);
UserViewHolder userViewHolder = new UserViewHolder(v,mDataSet,ctx);
return userViewHolder;
}
@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
holder.name_entry.setText(mDataSet.get(position).get("userId"));
holder.email_entry.setText(mDataSet.get(position).get("userName"));
holder.icon_entry.setText(""+mDataSet.get(position).get("number"));
}
@Override
public int getItemCount() {
return mDataSet.size();
}
public static class UserViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView name_entry, email_entry, icon_entry;
Context ctx;
ArrayList<HashMap<String, String>> mDataSet= new ArrayList<HashMap<String, String>> ();
public UserViewHolder(View itemView, ArrayList<HashMap<String, String>> mDataSet,Context ctx) {
super(itemView);
this.mDataSet=mDataSet;
this.ctx= ctx;
cardView = (CardView) itemView.findViewById(R.id.user_layout);
name_entry = (TextView) itemView.findViewById(R.id.name_entry);
email_entry = (TextView) itemView.findViewById(R.id.email_entry);
icon_entry = (TextView) itemView.findViewById(R.id.icon_entry);
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public void setFilter(ArrayList<HashMap<String, String>> newList){
//we have used hashmap, find how to use it here
mDataSet= new ArrayList<HashMap<String, String>>();
mDataSet.addAll(newList);
notifyDataSetChanged();
}
}
但是当我点击搜索图标时,回收站视图会随着所有数据消失,直到我恢复应用程序。在我看来我的事情是添加空数据,虽然我不确定,因为我是一个新的android,任何想法我在哪里弄错了,我怎么能让它工作。我知道有this解决方案,但我没弄好。
答案 0 :(得分:0)
您已在Activity类中声明:
ArrayList<HashMap<String, String>> arrayList= new ArrayList<HashMap<String, String>> ();
但你没有分配它。它应该是
arrayList = controller.getAllUsers();
因为你有这个字段的过滤器,那里总是有数据。最好将此变量从arrayList重命名为userList:)