有没有办法在Fragment中使用SearchView?

时间:2016-05-14 13:17:02

标签: java android fragment searchview

首先,谢谢大家的(未来)回复:)

下面解释我的问题:

我的主Activity名为MainActivity,我在其中使用了Fragment。所以,我的Fragment包含一个SearchView和一个ListView,它将包含取决于用户输入的建议......请注意我不在ActionBar中使用它,而是在我的布局文件中...

这是 fragment_board_menu.xml ,xml文件撒谎到我的片段 BoardMenuFragment

<android.support.v7.widget.SearchView
        android:id="@+id/board_searchView"
        android:layout_marginLeft="3dp"
        android:layout_marginStart="3dp"
        android:background="@color/green"
        app:searchIcon="@drawable/ic_search"
        app:searchHintIcon="@drawable/ic_search"
        app:closeIcon="@drawable/ic_close"
        app:defaultQueryHint="@string/create_newboard_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:layout_below="@+id/board_searchView"
        android:background="@drawable/listview_border"
        android:layout_marginBottom="@dimen/listview_border_marginBottom"
        android:layout_marginLeft="3dp"
        android:layout_marginStart="3dp"
        android:id="@+id/board_listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

他是 BoardMenuFragment.java ,我的Fragment java文件:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_board_menu, container, false);
    setupSearchView(view);

    // LISTVIEW
    // TODO This view is an example, don't forget to delete it
    values = new ArrayList<>();
    values.add("pomme");
    values.add("poire");
    values.add("banane");
    values.add("cerise");
    values.add("citron");
    adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_expandable_list_item_1, android.R.id.text1, values);
    board_listView.setAdapter(adapter);

    return view;
}

private void setupSearchView(View view) {
    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) view.findViewById(R.id.searchView);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getActivity().getComponentName());
    searchView.setSearchableInfo(searchableInfo);
}

但我有一个例外:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference

所以我的问题是:是否可以使用 SearchManager SearchableInfo 片段中使用 SearchView ,因为我尝试使用setOnQueryTextListener来做它并且它可以工作,但我不认为这是最好的方法,因为我看到很多教程,你创建一个SearchableActivity ...

1 个答案:

答案 0 :(得分:0)

我有一个类似的问题并找到答案in this question 您需要获取SearchableActivity的组件名称。

NSURLComponents* components = [[NSURLComponents alloc] init];
components.queryItems = [setup iMessageData]; //returns query items array

MSMessageTemplateLayout* layout = [[MSMessageTemplateLayout alloc] init];
//loaded via assets
//layout.image = [UIImage imageNamed:@"testSticker"];

//loaded via bundle
layout.mediaFileURL = [[NSBundle mainBundle] URLForResource:@"testSticker" withExtension:@"png"];    
layout.caption = @"Test String"

MSMessage* message = [[MSMessage alloc] init];
message.URL = components.URL;
message.layout = layout;

[self.activeConversation insertMessage:message completionHandler:^(NSError * _Nullable error)