无法使用搜索视图来过滤列表视图

我想使用搜索视图中的输入过滤我的列表视图。 我有以下代码:

package com.example.shiza.dailyquranverses;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.shiza.dailyquranverses.DailyQuranMethods;

public class SearchResultsActivity extends ActionBarActivity implements SearchView.OnQueryTextListener{

    Toolbar toolbar;
    ListView listView;
    SearchView searchView;
    ArrayAdapter<String> adapter;
    DailyQuranMethods dailyQuranMethods = new DailyQuranMethods();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);

        toolbar = (Toolbar) findViewById(R.id.app_bar_search_results);
        toolbar.setLogo(R.mipmap.ic_launcher);
        toolbar.setTitle(" Daily Qura'n Verses");
        setSupportActionBar(toolbar);
        Toast.makeText(this, "Hello from onCreate method", Toast.LENGTH_LONG).show();

//       using searchview to display data in the app

        searchView = (SearchView) findViewById(R.id.mySearchView);


        listView = (ListView) findViewById(R.id.listView);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dailyQuranMethods.getQuranVerses(this));

        listView.setAdapter(adapter);
        searchView.setOnQueryTextListener(this);

    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }
}

现在onQueryTextChange,应用程序停止。 看起来像下面的方法有问题:

public boolean onQueryTextChange(String newText){adapter.getFilter()。filter(newText); 返回false; }

请帮我解决这个问题。

编辑1:Logcat是:

06-19 14:40:25.815 3259-3259 / com.example.shiza.dailyquranverses E / AndroidRuntime:FATAL EXCEPTION:android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)在android上的main java.lang.NullPointerException。 widget.AdapterView.checkFocus(AdapterView.java:700)at android.widget.AdapterView $ AdapterDataSetObserver.onInvalidated(AdapterView.java:823)at android.widget.AbsListView $ AdapterDataSetObserver.onInvalidated(AbsListView.java:5600)at android.database .DataSetObservable.notifyInvalidated(DataSetObservable.java:47)at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:59)at android.widget.ArrayAdapter $ ArrayFilter.publishResults(ArrayAdapter.java:513)at android.widget.Filter $在android.app.ActivityThread.main(ActivityThread)的android.os.Looper.loop(Looper.java:137)android.os.Handler.dispatchMessage(Handler.java:99)上的ResultsHandler.handleMessage(Filter.java:282) .java:4424)at java.lang.reflect.Method.invo java.lang.reflect.Method.invokeNative(Native Method) ke(Method.java:511)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)at dalvik .system.NativeStart.main(本地方法)

链接地址: http://www.djcxy.com/p/92963.html

上一篇: Unable to use search view to filter list view

下一篇: Cancel on SearchView causing the soft keyboard to appear