我想在标签视图片段中使用来自sqlite数据库的数据实现搜索过滤器,经过三天以后this和this没有成功,最后我遇到了this似乎是一个干净的想法,因为我是Android开发的新手。 我试图实现它,但似乎没有工作它意味着搜索菜单项不膨胀也没有结果,我不知道我哪里出错,并没有任何错误,任何想法的错误和如何使它工作。
这里是片段代码
public class TabFragment3 extends Fragment implements SearchView.OnQueryTextListener {
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private UsersAdapter adapter;
ArrayList<HashMap<String, String>> arrayList;
DBController controller = new DBController(getContext());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.tab_fragment_3, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.usersList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
controller = new DBController(getContext());
arrayList = controller.getAllUsers();
adapter = new UsersAdapter(arrayList,getContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(item,new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
arrayList = controller.getAllUsers();
// Do something when collapsed
adapter.setFilter(arrayList);
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
}
@Override
public boolean onQueryTextChange(String query) {
return false;
}
@Override
public boolean onQueryTextSubmit(String newText) {
// Here is where we are going to implement the filter logic
newText = newText.toLowerCase();
ArrayList<HashMap<String, String>> userlist = controller.getAllUsers();
ArrayList<HashMap<String, String>> newList= new ArrayList<>();
for(HashMap<String, String> entry : userlist){
String name2= entry.get("number").toLowerCase();
if(name2.contains(newText)){
newList.add(entry);
}}
adapter.setFilter(newList);
return true;
}
}
/// adapter class
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){
mDataSet= new ArrayList<HashMap<String, String>>();
mDataSet.addAll(newList);
notifyDataSetChanged();
}
}
/// main
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener,SearchView.OnQueryTextListener {
private ViewPager viewPager;
PagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
public boolean onQueryTextSubmit(String query)
{ return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
答案 0 :(得分:3)
在TabFragment3的setHasOptionsMenu(true)
方法中添加onCreateView()
。它告诉主机活动您的片段具有要添加的菜单选项。
同时添加super.onCreateOptionsMenu(menu,inflater)
..
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu,inflater);
}
在MainActivity onOptionsItemSelected(MenuItem item)
返回false 内,因为菜单项操作将在TabFragment3&#39; onOptionsItemSelected(MenuItem item)
中进行处理。