0

I'm working on a simple app which gets data using public data(JSON) and show them on a list view. And it works okay. Now I want to implement 'search view' on action bar. So I followed youtube tutorial(https://www.youtube.com/watch?v=hoEY2n8CCSk) and did exactly what he did. Though the searchview appeared as I intended, search fuction doesn't working at all. I think the youtube tutorial filtered just string but I need to filter the object on the list view by name variable of each object..using getName() method. What should I do??

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_search, menu);
    MenuItem item = menu.findItem(R.id.menuSearch);
    SearchView searchView = (SearchView) item.getActionView();

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

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

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

    return super.onCreateOptionsMenu(menu);
}

AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/myAppTheme">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   </application>

</manifest>

menu_search.xml

<item android:id="@+id/menuSearch"
    android:title="Search"
    android:icon="@android:drawable/ic_menu_search"
    app:actionViewClass="android.widget.SearchView"
    app:showAsAction="always">

</item>
JessJ
  • 57
  • 9

1 Answers1

0

Try this way..

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.menuSearch) {
        SearchView searchView = (SearchView) item.getActionView();

        searchView
                .setOnQueryTextListener(new SearchView.OnQueryTextListener() {

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

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        mAdapter.getFilter().filter(newText);
                        return false;
                    }
                });
        return true;
    }
    return super.onOptionsItemSelected(item);
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50