2014-12-03 59 views
0

是否有可能在片段中实现搜索(过滤器ListView),该过滤器将通过文本过滤ListView, ?或者搜索是否需要活动课并且不可能处理它? 例子:像这些家伙做了(我不知道,如果他们使用,也可以胡亚蓉或片段) enter image description here通过在操作栏中写入搜索查询来在片段中搜索(过滤器ListView)

当用户添加了一些文本搜索栏 enter image description here 编辑:我究竟做错了什么?

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; 
     } 
    }); 
} 
} 
+1

确定你可以,只需设置SearchView.OnQueryTextListener监听器 – pskink 2014-12-03 21:00:22

+0

你的问题是关于如何设置监听器,如何从监听器中通知你的片段,或者如何过滤分片中的ListViews一旦他们有搜索字符串? – Bruce 2014-12-03 21:22:40

+0

如何在Fragment中访问SearchView,如果我使用的是v7支持库?下面的方法不起作用: MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView =(SearchView)MenuItemCompat.getActionView(searchItem); – Sharptax 2014-12-04 15:45:41

回答

0

你的目标是做的是发送搜索查询到包含列表中的片段......要做到这一点,你需要的活动,它包含片段之间的通信..所以作为用户键入搜索查询片段包含列表将相应地过滤...

第1步:在片段中定义一个公共方法,该片段将被它所连接的活动访问。

Class ExampleFragment { 

public void refreshList(String search){ 
// code to refresh the list view 
} 

第2步:通过下面的方法找到连接到您的活动片段

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); 

第3步:使用片段访问该片段的公共方法,这将刷新列表视图例如:

String searchQuery = "apple"; 
fragment.refreshList(searchQuery) 

}