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

时间:2015-06-19 09:06:10

标签: android searchview

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

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()过滤器(newText);               返回false;           }

请帮我解决这个问题。

编辑1: Logcat是:

  

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

0 个答案:

没有答案