0

这就是我使用 ActionBar Search 小部件进行基本搜索的方式。这显然是在布局中的 listView 中提供建议的简单方法。但我想要搜索框本身内的建议。虽然可以在普通搜索框中执行此操作,但我如何使用操作栏搜索框执行相同操作。

 public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        menu.add(0, 1, 1,"Search").setIcon(R.drawable.ic_search_inverse).setActionView(R.layout.collapsible_edittext).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        return super.onCreateOptionsMenu(menu);
     }



    @Override
        public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
            switch (item.getItemId()) {
                case 1:
                    search = (AutoCompleteTextView) item.getActionView();
                    search.addTextChangedListener(filterTextWatcher);
                    search.requestFocus();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                    return true;
            }   
            return false;
        }       

    private TextWatcher filterTextWatcher = new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // your search logic here
            doGeoSearch(String.valueOf(s));



        }

    };

    public void doGeoSearch(String query){
        Geocoder geocoder;
        ArrayList<Address> addresses;
        ArrayList<String> address = new ArrayList<String>() ;
        geocoder = new Geocoder(this, Locale.getDefault());
        try {
            addresses = (ArrayList<Address>) geocoder.getFromLocationName(query, 6);
            Log.d("Address",String.valueOf(addresses));
            for(int i = 0;i<addresses.size();i++)
            {
            String addr = new String();
            addr.concat(addresses.get(i).getAddressLine(0));
            addr.concat(addresses.get(i).getAddressLine(1));
            addr = addresses.get(i).getAddressLine(0) + addresses.get(i).getLocality() + addresses.get(i).getAdminArea();
            //addr.concat(addresses.get(i).getAddressLine(2));
            Log.d("addr",addr);
            address.add(addr);


            }

            SearchAddressAdapater addressList = new SearchAddressAdapater(getApplicationContext(),R.layout.search_list,addresses, LocationActivity.this);
            //addressView.setAdapter(addressList);
            //ListView addressListView = new ListView();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }
4

2 回答 2

1

您可以在 ActionBar Sherlock 中添加搜索小部件,搜索对话框具有此功能,并且实现起来非常简单,因为它是一个简单的可扩展操作项。

教程将向您展示如何使用搜索小部件完成所需的一切,包括搜索建议

于 2012-08-08T16:25:02.253 回答
0

即使 SearchView已实施并有效,搜索建议也适用于较新的设备,但不适用于 Gingerbread 等较旧的设备。这里有一个问题:

https://github.com/JakeWharton/ActionBarSherlock/issues/659

于 2012-11-12T18:18:36.817 回答