I’m making a ListView with data I get from a database placed in a server. The ListView has TWO TextViews for each row, one for the cities, and the other one for the ID’s each city has on the database, but this textview(the ID’s one) is HIDDEN from the ListView, so the user can only see cities.
Everything works fine, but now I want to add a search bar to the listview so I can filter the cities, but i’m getting the following problems:
1.-The adapter of the ListView is a Simple Adapter, as you can see on the code, and I have found no examples of how to filter a Listview using such an adapter.
2.-I’m getting the info for the ListView through an Async Task, and setting up the ListView inside the onPostExecute method of the Async Task, and again, I have found no examples of filtering it when its in such a place.
Here is the “relevant” code. If somebody needs the complete code, just ask me for it.
public class SQLWebVer extends ListActivity implements OnItemClickListener{
ProgressDialog barraProgreso;
ArrayList<HashMap<String, String>> listaCiudades;
.
.//some more varibles for the class
.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlwebver);
// Hashmap de la ListView
listaCiudades = new ArrayList<HashMap<String, String>>();
new cargaCiudades().execute();//this is the call to the async task
}
class cargaCiudades extends AsyncTask<String, String, String>{//the async task
.
.//here are the onPreExecute and doInBackground methods
.
protected void onPostExecute(String file_url) {
barraProgreso.dismiss();//quita la barra de la pantalla
runOnUiThread(new Runnable(){//actualiza la UI(la listView)
public void run() {
// TODO Auto-generated method stub
ListAdapter adapter = new SimpleAdapter
(SQLWebVer.this, listaCiudades,
R.layout.sqlweblist_item,
new String[]{"key_id", "key_ciudad"},new int[]
{R.id.ID_CIUDAD, R.id.CIUDAD});
setListAdapter(adapter);
}
});
}
}
I have tried to add the filter feature both on the onCreate and onPostExecute method, but with no luck so, any suggestions?
P.D:BONUS question, how could I sort my ListView alphabetically by cities but keeping each ID related to his city?(without changing the php files)
EDIT: I solved the problem by creating a Custom Base Adapter which implements filterable. Wasn´t as hard as I thought at first 😉
Some time ago I solved this problem but I forget to post the answer that worked for me, so here it is:
and added this in the main class: