0
是否有可能在片段中实现搜索(过滤器ListView),该过滤器将通过文本过滤ListView, ?或者搜索是否需要活动课并且不可能处理它? 例子:像这些家伙做了(我不知道,如果他们使用,也可以胡亚蓉或片段) 通过在操作栏中写入搜索查询来在片段中搜索(过滤器ListView)
当用户添加了一些文本搜索栏 编辑:我究竟做错了什么?
public class AllLists extends Fragment /*implements SearchView.OnQueryTextListener*/{
private TypedArray navMenuIcons;
private Context context;
private int position = 0;
private String location;
private List<Item> items = new ArrayList<Item>();
private ListView listView;
private CustomListAdapter adapter;
public AllLists(Context context) {
this.context = context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.alllists, container, false);
View header = inflater.inflate(R.layout.list_header, null);
// View list_view = inflater.inflate(R.layout.list_item, null);
ImageView image = (ImageView) header.findViewById(R.id.small_icon);
header.setClickable(false);
// ImageView logo = (ImageView) list_view.findViewById(R.id.image);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
Bundle bundle = getArguments();
position = bundle.getInt("position");
location = bundle.getString("location");
image.setImageDrawable(navMenuIcons.getDrawable(position));
DatabaseHandler db = new DatabaseHandler(context);
items = db.getAllItems(location);
listView = (ListView) rootView.findViewById(R.id.list);
adapter = new CustomListAdapter(context, items);
listView.setAdapter(adapter);
listView.addHeaderView(header);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (location.equals("accommodation") || location.equals("eat") || location.equals("events")
|| location.equals("entertainment") || location.equals("places") || location.equals("cinema")) {
Intent i = new Intent(context, ItemScreen.class);
i.putExtra("position", position - 1);
i.putExtra("location", location);
startActivity(i);
}
}
});
setHasOptionsMenu(true);
return rootView;
}
/*SearchView.OnQueryTextListener(new SearchView.OnQueryTextListener){
}*/
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
/*public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
adapter.getFilter().filter(cs);
}*/
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
return false;
}
});
}
}
确定你可以,只需设置SearchView.OnQueryTextListener监听器 – pskink 2014-12-03 21:00:22
你的问题是关于如何设置监听器,如何从监听器中通知你的片段,或者如何过滤分片中的ListViews一旦他们有搜索字符串? – Bruce 2014-12-03 21:22:40
如何在Fragment中访问SearchView,如果我使用的是v7支持库?下面的方法不起作用: MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView =(SearchView)MenuItemCompat.getActionView(searchItem); – Sharptax 2014-12-04 15:45:41