我目前有一个带有ListView和EditText小部件的布局。 ListView中的对象包含出发地点和目的地。 EditText用于过滤出发地点。 我想添加另一个EditText小部件,以便能够按出发地点和目的地过滤列表。 任何想法如何添加另一个过滤器类并通过离开然后由目的地过滤ListView内容?
public class PlacesActivity extends Activity{
private EditText SearchText;
private RelativeLayout search;
private ImageButton btnSearchClose;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_layout);
SearchText = (EditText) findViewById(R.id.SearchBox);
SearchText.addTextChangedListener(filterTextWatcher);
btnSearchClose = (ImageButton)findViewById(R.id.SearchClose);
placesListView = (ListView) findViewById(R.id.PlacesListView);
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
adapter.getFilter().filter(s);
}
private final class MyFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults oReturn = new FilterResults();
ArrayList<Routes> results = new ArrayList<Routes>();
orig = routes;
}
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (Routes o : orig) {
String Constr = constraint.toString();
constraint = Constr.subSequence(0, Constr.length());
String FromRoute = o.getTripFrom().toLowerCase();
if (FromRoute.startsWith((String) constraint)) {
results.add(o);
}
}
oReturn.values = results;
}
}
return oReturn;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<Routes> objects = new ArrayList<Routes>();
objects = (ArrayList<Routes>)results.values;
routes = objects;
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
您需要自己编写BaseAdapter
,其范围为Filterable
。您可以查看CursorAdapter
(Filterable
如何但不支持更改/添加过滤器的方式。