Cannot use search in filter list view
I want to filter my list using search input. I have the following code:
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;
}
}
Now the onQueryTextChange application is terminated. Similar to the following method:
public boolean onQueryTextChange (String newText) {. Adapter.getFilter () filter (newText); return false; }
Please help me to solve this problem.
Edit 1: Logcat:
06-19 14: 40: 25.815 3259-3259 / com.example.shiza.dailyquranverses E / AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at android.widget.ArrayAdapter.getCount (ArrayAdapter.java:330) on android. widget.AdapterView.checkFocus (AdapterView.java:700) on android.widget.AdapterView $ AdapterDataSetObserver.onInvalidated (AdapterView.java:823) at android.widget.AbsListView $ AdapterDataSetObserver.onInvalidated (AbsListView.djavaatabase600) .DataSetObservable.notifyInvalidated (DataSetObservable.java:47) at android.widget.BaseAdapter.notifyDataSetInvalidated (BaseAdapter.java:59) at android.widget.ArrayAdapter $ ArrayFilter.publishResults (ArrayAdapter.java:47) ResultsHandler.handleMessage (Filter.java:282) at android.os.Handler.dispatchMessage (Handler.java:99) at android.os.Looper.loop (Looper.java:137) at android.app.ActivityThread.main (ActivityThread.java:4424) in java. lang.reflect.Method.invokeNative (native method) at java.lang.reflect.Method.invoke (Method.java:511) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:551) in dalvik.system.NativeStart.main (native method)reflect.Method.invoke (Method.java:511) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 551) in dalvik.system.NativeStart.main (native method)reflect.Method.invoke (Method.java:511) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 551) in dalvik.system.NativeStart.main (native method)
source to share
No one has answered this question yet
Check out similar questions: