1

所以,我试图在我的应用程序上实现这个代码(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>

提前感谢您的帮助。

哈维尔

4

1 回答 1

1

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

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.

In your code will be something as:

public class MainActivity extends SherlockListActivity implements SearchView.OnQueryTextListener

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

In the line where you are setting:

 searchView.setOnQueryTextListener(queryTextListener);

Should be:

 searchView.setOnQueryTextListener(this);

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

于 2013-05-19T10:00:46.147 回答