【问题标题】:How to implement search widget with a listview using SherlockActionbar?如何使用 SherlockActionbar 实现带有列表视图的搜索小部件?
【发布时间】:2013-03-27 03:29:21
【问题描述】:

所以,我正在尝试在我的应用程序上实现此代码 (https://stackoverflow.com/a/14085524/2213992),但到目前为止没有成功。

这是我的 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:

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

提前感谢您的帮助。

哈维尔

【问题讨论】:

  • 目前发生了什么但不起作用?
  • 不知道怎么连接searhview过滤器代码:adapter.getFilter().filter(newText);到我的列表视图适配器: ArrayAdapter adapter = new ScosListAdapter(this, scoss);
  • 使用我发布的解决方案应该可以解决您将搜索视图与适配器连接的问题。
  • 我遇到了menu.findItem(R.id.menu_search).getActionView() 的问题。它总是返回空值。有什么想法吗?
  • 哈维尔,你的问题解决了吗?我有同样的问题

标签: android listview actionbarsherlock searchview


【解决方案1】:

我希望它会有所帮助,因为我遇到了同样的问题。

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

在您的代码中将是:

public class MainActivity extends SherlockListActivity implements SearchView.OnQueryTextListener

然后你必须在类中定义方法。适配器将是您在类 ArrayAdapter 适配器中拥有的适配器。但是你应该在类中将它定义为私有的。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    相关资源
    最近更新 更多