SearchView与ViewPager适配器

时间:2015-09-29 10:50:48

标签: android android-fragments android-viewpager searchview

我有一个活动有三个片段。我正在使用ViewPager标签。一个片段是SearchFragmentListView。它向Service类发送查询,然后在服务完成后调用自定义适配器并显示结果。问题是,我不知道如何从SearchView访问SearchFragment,如何获取用户输入的查询。 日Thnx。 主要活动代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // Do something
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            query = searchView.getQuery().toString();
            Log.e(TAG, "Main query is " + query);

            SearchFragment fragment = (SearchFragment)getSupportFragmentManager().findFragmentByTag("search");
            fragment.beginSearch(query);

            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            return true;
        }
    };

    searchView.setOnQueryTextListener(queryTextListener);
    return true;
}

片段代码:

public void beginSearch(String query) {
    Log.e(TAG, "got query "+ query);
    Intent intent;
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    SharedPreferences.Editor editor = sp.edit();
    String search = sp.getString("search", "a");
    if (search.equals("Free search")) {

        if (connection == NOT_CONNECTED) {
            Toast.makeText(getActivity(), "No Internet connection. Can`t search", Toast.LENGTH_SHORT).show();

        } else {
            if (query.length() == 0) {
                Toast.makeText(getActivity(), "Please enter text", Toast.LENGTH_SHORT).show();
            } else {
                intent = new Intent(getActivity(), Service.class);
                intent.setAction(Service.ACTION_SEARCH_BY_TEXT);
                intent.putExtra("query", query);
                getActivity().startService(intent);
                swipeRefreshLayout.setRefreshing(true);
                editor.putInt("refresh", SEARCH_BY_TEXT);
                editor.commit();
            }
        }
    } else if (search.equals("Search nearby")) {
        if (connection == NOT_CONNECTED) {
            Toast.makeText(getActivity(), "No Internet connection. Can`t search", Toast.LENGTH_SHORT).show();
        } else {
            if (query.length() == 0) {
                Toast.makeText(getActivity(), "Please enter text", Toast.LENGTH_SHORT).show();
            } else {
                intent = new Intent(getActivity(), Service.class);
                intent.setAction(Service.ACTION_SEARCH_NEARBY);
                intent.putExtra("query", query);
                intent.putExtra("lat", lat);
                intent.putExtra("lng", lng);
                Log.e(TAG, "lat" + lat + "lng" + lng);
                getActivity().startService(intent);
                swipeRefreshLayout.setRefreshing(true);
                editor.putInt("refresh", SEARCH_NEARBY);
                editor.commit();
            }
        }
    }
}

0 个答案:

没有答案