searchView中的setQuery

时间:2014-09-25 10:07:48

标签: android searchview

我想在Android应用中的searchView中设置文本。 setQuery不起作用。

这是我的代码:

 private SearchView searchPostalDelivery;
        private PostalDeliveriesAdapter listAdapter;
        private ExpandableListView myList;

        private ArrayList<PostalDeliveries> postalDeliveryList = new ArrayList<PostalDeliveries>();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_tab_single_delivery);

            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchPostalDelivery = (SearchView) findViewById(R.id.searchPostalDelivery);
            searchPostalDelivery.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchPostalDelivery.setIconifiedByDefault(false);
            searchPostalDelivery.setOnQueryTextListener(this);
            searchPostalDelivery.setOnCloseListener(this);


             searchPostalDelivery.setQuery("test", false);



        }

}

2 个答案:

答案 0 :(得分:4)

以下是解决方案:

final String query = getIntent().getStringExtra(SearchManager.QUERY);

    if(!TextUtils.isEmpty(query) && mSearchView != null){

        mSearchView.post(new Runnable() {
            @Override
            public void run() {

                MenuItemCompat.expandActionView(mSearchItem);
                mSearchView.setQuery(query, true);
            }
        });
    }

答案 1 :(得分:0)

你有没有试过像:

searchPostalDelivery.setQueryHint("Query Hint");

如果您想调用可搜索的活动,可以这样做:

    Intent i = new Intent(MyCurrentActivity.this,MySearchableActivity.class);
    i.setAction(Intent.ACTION_SEARCH);
    i.putExtra(SearchManager.QUERY,"Your barcode");
    startActivity(i);

在您的可搜索活动中,您可以阅读您的查询。