如何实现用SherlockActionbar列表视图搜索小插件? [英] How to implement search widget with a listview using SherlockActionbar?

查看:190
本文介绍了如何实现用SherlockActionbar列表视图搜索小插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想实现这个code( http://stackoverflow.com/a/14085524/2213992 )对我的申请,但至今没有成功。

So, I'm trying to implement this code (http://stackoverflow.com/a/14085524/2213992) on my application but no success so far.

这是我的MainActivity.java:

This is my MainActivity.java:

import java.util.List;

import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.SearchView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends SherlockListActivity implements
// public class MainActivity extends ListActivity implements
        ActionBar.TabListener {
    private List<Scos> scoss;
    ScosDataSource datasource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        datasource = new ScosDataSource(this);
        datasource.open();
        scoss = datasource.findAll();
        if (scoss.size() == 0) {
            createData();
            scoss = datasource.findAll();
        }
        refreshDisplay();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getSupportMenuInflater().inflate(R.menu.options, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
                .getActionView();
        if (null != searchView) {
            searchView.setSearchableInfo(searchManager
                    .getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
        }

        SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {

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

        return super.onCreateOptionsMenu(menu);

        // return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            return (true);
        }
        return (super.onOptionsItemSelected(item));
    }

    public void refreshDisplay() {

        ArrayAdapter<Scos> adapter = new ScosListAdapter(this, scoss);
        setListAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        datasource.open();
    }

    @Override
    protected void onPause() {
        super.onPause();
        datasource.close();

    }

    private void createData() {
    }

}

这我option.xml文件:

This my option.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/menu_search"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView"
    android:showAsAction="ifRoom|collapseActionView"
    android:icon="@drawable/ic_action_search"
    android:title="@Search">
</item>

</menu>

感谢您的帮助提前。

Thanks for your help in advance.

哈维尔

推荐答案

我希望这将有助于,因为我有同样的问题。

I hope that it would help because I had the same problem.

要使用适配器的最好办法是,类实现SearchView.OnQueryTextListener然后你没有必要创建内部类,你将有适配器。

To use the adapter the best way is that the class implements SearchView.OnQueryTextListener and then you don´t have to create the inner class and you will have the adapter.

在您的code将东西如下:

In your code will be something as:

public class MainActivity extends SherlockListActivity implements SearchView.OnQueryTextListener

,然后在类,你必须定义的方法。该适配器将是你在你的类ArrayAdapter适配器具有适配器。但是,你应该把它定义在类私有的。

and then in the class you have to define the methods. The adapter will be the adapter that you have in your class ArrayAdapter adapter. But you should define it as private in the class.

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

如果您有任何问题,让我知道,我今天用了类似的问题,没有答案读你的问题。

If you have any problem let me know, I was with a similar problem today and read your question without answer.

这篇关于如何实现用SherlockActionbar列表视图搜索小插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆