How I wanted to use the Custom List adapter so that I could style the list, but the Filter function does not work. I got basic filtering, but the application crashes as soon as the list of filtered results is less than the number of lists displayed when filtering starts.
There is also a second problem that arises in my code that I'm not sure if it is connected, but when clear(); runs in publishResults, then the application also crashes.
Here is the code I'm using.
package com.android.example; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; 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.TextView; public class CustomListAdapter extends ArrayAdapter<String> { private Context mContext; private String[] items; private String[] filtered; public CustomListAdapter(Context context, int textViewResourceId, String[] items) { super(context, textViewResourceId, items); this.filtered = items; this.items = filtered; setNotifyOnChange(true); mContext = context; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.list_item, null); } String o = filtered[position]; if (o != null) { TextView tt = (TextView) v.findViewById(R.id.tvViewRow); if (tt != null) { tt.setText("Name: "+o); } } return v; } public void notifyDataSetInvalidated() { super.notifyDataSetInvalidated(); } private Filter filter; public Filter getFilter() { if(filter == null) filter = new NameFilter(); return filter; } private class NameFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) {
}
android filter listview android-arrayadapter
user482073
source share