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

私も同じ悩みを抱えていたので参考になれば幸いです。

アダプターを使用する最良の方法は、クラスが 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);

何か問題がある場合はお知らせください。今日も同様の問題があり、回答なしで質問を読みました。

于 2013-05-19T10:00:46.147 に答える