Reputation: 61
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 ;)
Upvotes: 1
Views: 6220
Reputation: 61
Some time ago I solved this problem but I forget to post the answer that worked for me, so here it is:
package android.codigo;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
public class MiAdaptador extends ArrayAdapter<Item2> {
private ArrayList<Item2> allItems;
@SuppressWarnings("unused")
private ArrayList<Item2> subItems;
public ArrayList<Item2> getItems() {
return allItems;
}
public MiAdaptador(Context context, int textViewResourceId,
ArrayList<Item2> items) {
super(context, textViewResourceId, items);
this.allItems = items;
this.subItems = items;
}
@Override
public int getCount(){
return subItems.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.item2_lista, null);
}
Item2 it = subItems.get(position);
if (it != null) {
TextView nombre = (TextView) v.findViewById(R.id.textView1);
ImageView img = (ImageView) v.findViewById(R.id.imageView1);
ImageView img2 = (ImageView) v.findViewById(R.id.imageView2);
ImageView img3 = (ImageView) v.findViewById(R.id.imageView3);
if (nombre != null) {
nombre.setText(it.getNombre());
}
if (img != null) {
img.setImageResource(it.getID());
}
if (img != null) {
img2.setImageResource(it.getID2());
}
if (img != null) {
img3.setImageResource(it.getID3());
}
}
return v;
}
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence prefijo) {
FilterResults results = new FilterResults();
ArrayList<Item2> aux = new ArrayList<Item2>();
// El prefijo tiene que ser mayor que 0 y existir
if (prefijo != null && prefijo.toString().length() > 0) {
for (int index = 0; index < allItems.size(); index++) {
Item2 si = allItems.get(index);
String nombre = si.getNombre();
String nom[] = nombre.split(" ");
for(int i=0; i<nom.length; i++){
if (nom[i].toLowerCase().startsWith(prefijo.toString().toLowerCase())) {
aux.add(si);
}
}
}
results.values = aux;
results.count = aux.size();
} else {
synchronized (allItems) {
results.values = allItems;
results.count = allItems.size();
}
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence prefijo,FilterResults results) {
subItems = (ArrayList<Item2>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
}
and added this in the main class:
ListView lv = (ListView)findViewById(R.id.list);
lv.setTextFilterEnabled(true);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
TextWatcher filtro = new TextWatcher(){
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
adapter.getFilter().filter(e.getText().toString());//filtra el texto metido en el edittext en tiempo real
}
};
e.addTextChangedListener(filtro);
}
Upvotes: 3
Reputation: 14435
EDIT: Yeah, try using BaseAdapter, I just read something that a SimpleAdapter can't change dynamically or something like that...
I'm not really sure, but this is what I think... You're AsyncTask has to implement
@Override protected Void doInBackground(String... params) {
SQLWebVer.this.runOnUiThread(new Runnable() {
public void run() {
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);
}
});
}
and because you say ...extends AsyncTask , you should try the execute method with a string, like:
new cargaCiudades().execute("");
hopefully this helps, I haven't tried it myself, no time for now, but this is what I think at first sight...
Upvotes: 0