2012-08-08 195 views
0

這就是我使用ActionBar Search小部件進行基本搜索的方式。這顯然是在佈局中的listView中提供建議的簡單方法。但我想在搜索框內提出建議。儘管可以在普通搜索框中執行此操作,但如何使用「操作欄搜索」框執行相同操作。在ActionBarSherlock搜索欄中製作自定義搜索建議

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(); 
     } 



    } 

回答

1

您可以將搜索插件到你的動作條福爾摩斯,搜索對話框具有這種功能,這是很容易實現,因爲它是一個簡單的展開行動項目。

This教程將告訴你怎麼做,你需要搜索小工具,包括搜索建議