Android 如何在Action Bar Sherlock中实现搜索小部件?

Android 如何在Action Bar Sherlock中实现搜索小部件?,android,actionbarsherlock,Android,Actionbarsherlock,我试着让搜索框在动作酒吧夏洛克上工作 这是我的定位前活动: @ContentView(R.layout.search) public class PreLocationActivity extends RoboSherlockActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我试着让搜索框在动作酒吧夏洛克上工作

这是我的
定位前活动

    @ContentView(R.layout.search)
    public class PreLocationActivity extends RoboSherlockActivity {

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.map_layout);
        } 

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            //Used to put dark icons on light action bar
             menu.add("Search")
             .setIcon(R.drawable.ic_search_inverse)
             .setActionView(R.layout.collapsible_edittext)
             .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
                return true;
        }

        @Override
        public boolean onSearchRequested() {
            return super.onSearchRequested();
        }
    }
@ContentView(R.layout.search)
public class SearchableActivity extends RoboSherlockFragmentActivity {

    @InjectView(R.id.addressListView) ListView addressView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          doGeoSearch(query);
        }
}

    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, SearchableActivity.this);
            addressView.setAdapter(addressList);
            //ListView addressListView = new ListView();
        } catch (IOException e) {
                //Handle exception
        }
    }
这是我的
可搜索活动

    @ContentView(R.layout.search)
    public class PreLocationActivity extends RoboSherlockActivity {

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.map_layout);
        } 

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            //Used to put dark icons on light action bar
             menu.add("Search")
             .setIcon(R.drawable.ic_search_inverse)
             .setActionView(R.layout.collapsible_edittext)
             .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
                return true;
        }

        @Override
        public boolean onSearchRequested() {
            return super.onSearchRequested();
        }
    }
@ContentView(R.layout.search)
public class SearchableActivity extends RoboSherlockFragmentActivity {

    @InjectView(R.id.addressListView) ListView addressView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          doGeoSearch(query);
        }
}

    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, SearchableActivity.this);
            addressView.setAdapter(addressList);
            //ListView addressListView = new ListView();
        } catch (IOException e) {
                //Handle exception
        }
    }
@ContentView(R.layout.search)
公共类SearchableActivity扩展了RoboSherlockFragmentActivity{
@InjectView(R.id.addressListView)ListView addressView;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//获取意图、验证操作并获取查询
Intent=getIntent();
if(Intent.ACTION_SEARCH.equals(Intent.getAction())){
String query=intent.getStringExtra(SearchManager.query);
DogeSearch(查询);
}
}
公共void dogeSearch(字符串查询){
地理编码器;
数组列表地址;
ArrayList地址=新的ArrayList();
geocoder=新的geocoder(这个,Locale.getDefault());
试一试{
addresses=(ArrayList)geocoder.getFromLocationName(查询,6);
Log.d(“地址”,String.valueOf(地址));
对于(int i=0;iAndroid支持库(v4+v7)):
支持库v7:

谷歌现在支持ActionBar兼容Android 2.1(API 7)。

转换很容易,因为方法名称相同和/或非常相似

使用资源添加支持库:

您的清单:AndroidManifest.xml

<uses-sdk
  android:minSdkVersion="7"
  android:targetSdkVersion="17" />
<item
    android:id="@+id/menu_search"
    android:actionViewClass="android.support.v7.widget.SearchView"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:title="Search"/>
<item
    android:id="@+id/menu_search"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:title="Search"/>

让我知道这是否也适用于您,如果您还需要帮助,请让我知道!

这里有一个类似的问题,我在分配适配器时遇到了问题

我希望这会有帮助,因为我也有同样的问题

要使用适配器,最好的方法是该类实现SearchView.OnQueryTextListener,然后您不必创建内部类,您将拥有适配器

您的代码中将包含以下内容:

public class MainActivity extends SherlockListActivity implements SearchView.OnQueryTextListener
然后在类中定义方法。适配器将是类ArrayAdapter adapter中的适配器。但应在类中将其定义为私有

public boolean onQueryTextChange(String newText) {
    // this is your adapter that will be filtered
    adapter.getFilter().filter(newText);
    return true;
}

public boolean onQueryTextSubmit(String query) {
    // this is your adapter that will be filtered
    adapter.getFilter().filter(query);
    return true;
}
在要设置的行中:

 searchView.setOnQueryTextListener(queryTextListener);
应该是:

 searchView.setOnQueryTextListener(this);

如果您有任何问题,请告诉我,我今天遇到了类似的问题,没有回答就阅读了您的问题。

我已经尝试了此方法。此方法有效,但与可搜索活动不完全相同。历史无法保存。Georgy,我只是尝试了您的解决方案,但我的EditText没有调用TextWatcher的方法。有什么建议吗?您设置了TextChange吗dListener到EditText?出于某种原因,我在这方面遇到了一些主要问题。请检查:。适配器是您的ListView适配器。将数据绑定到视图。好的……谢谢。还有一件事要问,如果我需要在ActionBar上包括一个菜单项,比如一个按钮,该怎么办。通常,对于多个项目,我们使用Switch case,但在这里我感到困惑有一点,我们如何在操作栏上再创建一个项目。在包括android:actionViewClass=“com.actionbarsherlock.widget.SearchView”之后显示ClassCastException。我下载了ABS 4.4,提取了它,转到eclipse,从源代码创建了android项目,并选择了actionbarsherlock,通过引用您的答案创建了一个新项目,但我发现一个错误,找不到类“com.actionbarsherlock.widget.SuggestionsAdapter”。我还将此作为堆栈溢出问题发布@Sar009根据库的不同,您需要确保使用正确的主题。在这里,您可以查看我对类似问题的回答: