i need apply a filter for a ListView. I read a lot of documentation but i can’t understand the rule of the filter. The filter works but i don’t know what are it doing.
I use this class:
public class ArtistasActivity extends Activity {
private EditText filterText = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundColor(Color.rgb(181, 9, 97));
setContentView(R.layout.artistas_l);
final ListView m_listview = (ListView) findViewById(R.id.listView1);
ListAdaptor adaptor = new ListAdaptor(this, R.layout.artistas_l, loadArtistas());
m_listview.setFastScrollEnabled(true);
m_listview.setScrollingCacheEnabled(true);
m_listview.setAdapter(adaptor);
m_listview.setTextFilterEnabled(true);
// Search
filterText = (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
public void afterTextChanged(Editable text) {
Log.d("search", ""+text);
ListAdaptor adaptor = (ListAdaptor) m_listview.getAdapter();
adaptor.getFilter().filter(text);
adaptor.notifyDataSetChanged();
}
});
And ListAdaptor is typical:
private class ListAdaptor extends ArrayAdapter<Artista> {
private ArrayList<Artista> artistas;
public ListAdaptor(Context context, int textViewResourceId, ArrayList<Artista> items) {
super(context, textViewResourceId, items);
this.artistas = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.artistas_list, null);
}
Object content = null;
Artista o = artistas.get(position);
TextView tt = (TextView) v.findViewById(R.id.ArtistTopText);
tt.setText(o.getNombre());
v.setClickable(true);
v.setFocusable(true);
v.setOnClickListener(myClickListener);
return v;
}
}
The aplications works, and when write in the texteditor the list is filtered but when i put a R, appear names with Dani or Diego…
I override the toString method for only return de name.
@Override
public String toString() {
return this.nombre;
}
Thx for help !! Rgds
There is no need to maintain the
aristasarray member, as you are already passing it to the super. In thegetViewmethod, get the item by callinggetItem(position).. This correction will fix the problem..