如何在多个片段上搜索查询 [英] How Do I search query on Multiple Fragments

查看:72
本文介绍了如何在多个片段上搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试在多个片段上实现搜索功能,但我只能让它只在一个片段上工作。我做了什么?在我的每个片段上,我都有这行代码。

Hello I am trying to implement the search functionality on multiple fragment but I only got it to work on only one Fragment. What I did? On each of my fragment, I have this Line of code.

public void bindData(String strSearchText) {
   // This method gets its array from Resources. and its different from fragment to fragment 
   Object[] arrayItems = getResources().getStringArray(R.array.BikeList);
   if (strSearchText != null && strSearchText.length() > 0) {
    List<Object> lstFilteredItems = new ArrayList<>();
    for (Object item : arrayItems) {
        if (((String)item).contains(strSearchText)) {
            lstFilteredItems.add(item);
        }
    }
    arrayItems = lstFilteredItems.toArray();
}
ArrayAdapter<Object> adapter = new ArrayAdapter<>(getActivity(),R.layout.fragment_sub_list,R.id.topText,arrayItems);    
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}





在我的主要活动中,我有这段代码。



On my main Activity, I have this piece of code.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.home_page, menu);
    SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(
            new ComponentName(getApplicationContext(), SearchResultActivity.class)));
    searchView.setOnQueryTextListener(this);
    return true;
}


@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String strSearchText) {
    //how do i switch between them? based on whatever fragment the user is
    bikeSublist.bindData(strSearchText); //bikefragment  
    //carSublist.bindData(strSearchText);//carfragment

    return true;
}



我一次只能使用一个。并且用户必须(在这种情况下现在)在自行车片段上。但我想要一种情况,即无论用户使用什么片段,它都能够从不同的片段中获取搜索查询。对于我发布的案例,我能够在一个地方收集所有资源,但如果用户从 bikeSublist.bindData(strSearchText); //自行车碎片。在此先感谢。


I can only use one at a time. And the user must be ( for this case now) on the bike fragment. But I want a situation that, no matter what fragment the user is on, it can be able to get the search query from different fragment. For the case I posted, I was able to gather all the resources at a place, but my App would crash if the user searches from a fragment outside bikeSublist.bindData(strSearchText); //bikefragment. Thanks in advance.

推荐答案

您应该创建一个单独的类或进行搜索的公共方法。然后,您可以从应用程序中的任何片段或意图中调用该类或方法。
You should probably create a separate class, or public method that does the searching. You then call that class or method from any fragment or intent within your application.


这篇关于如何在多个片段上搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆