按多个标准搜索/过滤ListView

时间:2012-01-18 00:57:39

标签: java android listview search

我目前有一个带有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();
    }
}

1 个答案:

答案 0 :(得分:0)

您需要自己编写BaseAdapter,其范围为Filterable。您可以查看CursorAdapterFilterable如何但不支持更改/添加过滤器的方式。

的CursorAdapter http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/widget/CursorAdapter.java#CursorAdapter