搜索视图的操作栏。反向兼容性问题

时间:2012-11-24 11:02:39

标签: android android-actionbar actionbarsherlock searchview

我正在构建一个示例应用程序来演示带有过滤器和其他Action Bar项目的SearchView。 我能够在4.2(Nexus 7)上成功运行这个应用程序。但它不是在2.3上运行。我用Google搜索了这个问题。来了解我应该使用SherLock Action吧。我刚刚去了http://actionbarsherlock.com/download.html,下载了zip文件并按照WiseManDesigns视频中的信息添加了库:http://www.youtube.com/watch?v=4GJ6yY1lNNY&feature=player_embedde。但我仍然无法弄清楚这个问题。

这是我的代码:

SearchViewActionBar.java

public class SearchViewActionBar extends Activity implements SearchView.OnQueryTextListener 
{
    private SearchView mSearchView;
    private TextView mStatusView;
    int mSortMode = -1;
    private ListView mListView;
    private ArrayAdapter<String> mAdapter;

    protected CharSequence[] _options = { "Wild Life", "River", "Hill Station", "Temple", "Bird Sanctuary", "Hill", "Amusement Park"};
    protected boolean[] _selections =  new boolean[ _options.length ];


    private final String[] mStrings = Cheeses.sCheeseStrings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

        setContentView(R.layout.activity_main);

       // mStatusView = (TextView) findViewById(R.id.status_text);
       // mSearchView = (SearchView) findViewById(R.id.search_view);
        mListView = (ListView) findViewById(R.id.list_view);
        mListView.setAdapter(mAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                mStrings));
        mListView.setTextFilterEnabled(true);
        //setupSearchView();
    }

    private void setupSearchView() {
        mSearchView.setIconifiedByDefault(true);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setSubmitButtonEnabled(false);
        //mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.searchview_in_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) searchItem.getActionView();
       //setupSearchView(searchItem);
       setupSearchView();

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
    {
        if (mSortMode != -1) 
        {
            Drawable icon = menu.findItem(mSortMode).getIcon();
            menu.findItem(R.id.action_sort).setIcon(icon);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String c="Category";
        String s=(String) item.getTitle();
        if(s.equals(c))
        {
            System.out.println("same");
            showDialog( 0 );
        }
        //System.out.println(s);
        Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
        return true;
    }

    protected Dialog onCreateDialog( int id ) 
    {
        return 
        new AlertDialog.Builder( this )
            .setTitle( "Category" )
            .setMultiChoiceItems( _options, _selections, new DialogSelectionClickHandler() )
            .setPositiveButton( "SAVE", new DialogButtonClickHandler() )
            .create();
    }


    public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
    {
        public void onClick( DialogInterface dialog, int clicked, boolean selected )
        {
            Log.i( "ME", _options[ clicked ] + " selected: " + selected );
        }
    }


    public class DialogButtonClickHandler implements DialogInterface.OnClickListener
    {
        public void onClick( DialogInterface dialog, int clicked )
        {
            switch( clicked )
            {
                case DialogInterface.BUTTON_POSITIVE:
                    printSelectedPlanets();
                    break;
            }
        }
    }

    protected void printSelectedPlanets()
    {
        for( int i = 0; i < _options.length; i++ ){
            Log.i( "ME", _options[ i ] + " selected: " + _selections[i] );
        }
    }

    public void onSort(MenuItem item) 
    {
        mSortMode = item.getItemId();
        invalidateOptionsMenu();
    }


    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {
            mListView.clearTextFilter();
        } else {
            mListView.setFilterText(newText.toString());
        }
        return true;
    }

    public boolean onQueryTextSubmit(String query) 
    {
        mStatusView.setText("Query = " + query + " : submitted");
        return false;
    }

    public boolean onClose() 
    {
        mStatusView.setText("Closed!");
        return false;
    }

    protected boolean isAlwaysExpanded() 
    {
        return false;
    }
}

1 个答案:

答案 0 :(得分:8)

您需要使用ActionBarSherlock。为此,您的活动需要扩展和ActionBarSherlock组件。更像是这样:

MySearchActivity extends SherlockActivity {}

或者如果你想使用build in list API,你可以这样做:

MySearchActivity extends SherlockListActivity {}

然后在res / menu文件夹中创建activity_mysearch.xml文件,其中包含以下内容:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >    
    <item 
        android:id="@+id/menu_search"
        android:icon="@drawable/ic_menu_search"
        android:title="@string/menu_search"
        android:showAsAction="ifRoom|collapseActionView"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
</menu>

现在让您的活动实现OnQueryTextListener。在onCreateOptionsMenu中:

inflater.inflate(R.menu.activity_mysearch, menu);
MenuItem searchViewMenuItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchViewMenuItem.getActionView();
searchView.setOnQueryTextListener(this);

您现在可以使用

onQueryTextSubmit(String query)
onQueryTextChange(String newText)

查询列表并更新列表视图项的方法。您还可以向适配器添加过滤器,并创建一个接口,通知活动在适配器的数据集发生更改后使列表视图无效。