将搜索数据传递给Searchable Activity

unr*_*007 7 android android-fragments android-activity searchview android-search

我有一个主要活动有2个片段.主要活动SearchView在操作栏中有一个.这两个片段都有一个包含大量字符串的列表List<String>.

流程是:

用户输入片段I - >选择一个字符串(比如说Selection1) - >基于Selection1,在第二个片段中填充一个字符串列表 - >这里用户选择第二个字符串--->基于这两个处理字符串.

现在,由于两个片段都包含大量字符串,因此用户在SearchView其中输入一个查询,该查询过滤列表并将其缩小为显示在中的较小列表SearchableActivity.

现在的问题是如何SearchableActivity获取这两个访问权限以List<String>根据查询过滤它们并向用户显示缩减列表.

目前我所做的是被覆盖onSearchRequested并将数据传递为

    @Override
    public boolean onSearchRequested()
    {
        Bundle appData = new Bundle();
        appData.putString(FRAGMENT_ID, "Fragment_A");
        appData.putStringArrayList(SEARCH_LIST, searchList);
        startSearch(null, false, appData, false);
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法或标准方法来处理这个问题,即一个允许数据基于我的MainActivity实现SearchableActivity

编辑:添加代码.显示数据的设置方式Fragment.onDataReceivedHttpManager接收数据的中调用.

@Override
public void onDataReceived(String type,final Object object)
{
    switch(type)
    {
        case PopItConstants.UPDATE_LIST:
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run()
                {
                    updateCinemaList((List<String>) object);
                }
            });
            break;
    }
}

public void updateDataList(List<String> data)
{
    this.dataList = data;
    spinner.setVisibility(View.GONE);
    mAdapter.updateList(dataList);
}
Run Code Online (Sandbox Code Playgroud)

unr*_*007 1

好吧...我就是这样做的。

基本上,两个片段中收到的数据不仅仅是模型,List<String>而是模型。电影院和地区,包含名称以外的详细信息,包括位置、评级等。

所以,首先,我做了一个界面ISearchable

public Interface ISearchable
{
    // This contains the Search Text. An ISearchable item is included
    // in search results if query is contained in the String returned by this method
    public String getSearchText();

    //This is meant to return the String that must be displayed if this item is in search results
    public String getDisplayText();

    //This is meant to handle onClick of this searchableItem
    public void handleOnClick();
}
Run Code Online (Sandbox Code Playgroud)

Cinema 和 Region 模型均已实现ISearchable

之后,我使用了一个单例类DataManager,在其中维护了一个List<ISearchable> currentSearchList.

public class DataManager
{
    .....<singleton implementation>....
    List<ISearchable> currentSearchList;

    public void setSearchList(List<ISearchable> searchList)
    {
        this.currentSearchList = searchList;
    }

    public List<ISearchable> getSearchList()
    {
        return this.currentSearchList;
    }

}
Run Code Online (Sandbox Code Playgroud)

因此,每当加载片段(Fragment_A 或 Fragment_B)时,它都会更新 this currentSearchList,这样当SearchableActivity执行搜索时,它所要做的就是DataManager.getInstance().getSearchList()使用此列表来过滤掉匹配项的列表。

这就是我处理除了需要执行搜索的 SearchableActivity 之外的 Activity 中的列表问题的方法。

我知道这可能不是最好的解决方案,因此,我期待建议和批评,并利用它们来得出更好的解决方案。