2012-01-18 166 views
4

我目前有一個帶有ListView和EditText小部件的佈局。 ListView中的對象包含出發地點和目的地。 EditText用於過濾出發地點。 我想添加另一個EditText小部件,以便能夠通過出發地和目的地過濾列表。 任何想法如何添加另一個過濾器類,並過濾ListView內容的偏離,然後由目的地?按多個標準搜索/過濾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(); 
    } 
} 

回答