Search code examples
listadaptersimpleadapter

How to use getFilter() with ListAdapter to filter ListView?


I am struggling to Filter the listView using TextWatcher on EditText, List is populated by SimpleAdapter and data is fetched from web-server. Actully, I am using ListAdapter there with SimpleAdapter and ArrayList(See Code Below).

I have searched in SO here & here and much more sites but didn't find solution for my error.

Error is on following line written inside TextWatcher method:

grabedDb.this.adapter.getFilter().filter(s);

getFilter() function is not working.( Cannot resolve method 'getFilter()' ). Please help me in this error.

grabDb.java

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class grabedDb extends AppCompatActivity {
EditText grabResults;
String url = "http://MY_LINK_HERE";
ArrayList<HashMap<String, String>> Item_List;
ProgressDialog PD;
ListAdapter adapter;

// JSON Node names
public static final String ITEM_ID = "name";
public static final String ITEM_NAME = "rate";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grabed_db);
    grabResults = (EditText) findViewById(R.id.grabed_et_search);
/////////////////////
//////////TextWatcher////////////

    grabResults.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //perform Search
            if (s.toString().equals("")){
                //Reset List
                ReadDataFromDB();
            } else {
//////////////////    Error on getFilter() method ////////////
                grabedDb.this.adapter.getFilter().filter(s);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    Item_List = new ArrayList<HashMap<String, String>>();

    PD = new ProgressDialog(this);
    PD.setMessage("Please Wait...");
    PD.setCancelable(false);

    ReadDataFromDB();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main_actions, menu);
    return super.onCreateOptionsMenu(menu);
}
private void ReadDataFromDB() {
    PD.show();
    JsonObjectRequest jreq = new JsonObjectRequest(Request.Method.GET, url,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try {
                        int success = response.getInt("success");

                        if (success == 1) {
                            JSONArray ja = response.getJSONArray("mydrugs");

                            for (int i = 0; i < ja.length(); i++) {

                                JSONObject jobj = ja.getJSONObject(i);
                                HashMap<String, String> item = new HashMap<String, String>();
                                item.put(ITEM_ID, jobj.getString(ITEM_ID));
                                item.put(ITEM_NAME,
                                        jobj.getString(ITEM_NAME));

                                Item_List.add(item);

                            } // for loop ends

                            String[] from = { ITEM_ID, ITEM_NAME };
                            int[] to = { R.id.item_name, R.id.item_id };

                            adapter = new SimpleAdapter(
                                    getApplicationContext(), Item_List,
                                    R.layout.list_items, from, to);
                            ListView myList = (ListView) findViewById(R.id.list_list_listView);
                            myList.setAdapter(adapter);
                            PD.dismiss();
                            myList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position,
                                                        long id) {

                                    Intent modify_intent = new Intent(grabedDb.this,
                                            updatedata.class);

                                    modify_intent.putExtra("item", Item_List.get(position));

                                    startActivity(modify_intent);

                                }
                            });
                        } // if ends

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            PD.dismiss();
        }
    });

    // Adding request to request queue
    MyApplication.getInstance().addToReqQueue(jreq);

}

}

I don't want to use ListActivity


Solution

  • Just replace ListAdapter to SimpleAdapter and keep everything same. It will work.