Reputation: 1
We need to implement the code of filter list view in this example. We are using a 2 classes : Car.java
(contains the Object Car), Class CarAdapter
extends the BaseAdapter
Class.
Below is some of codes:
Car.java
public class Car {
public String title;
public int car_id;
public Car(String title,int car_id) {
this.title = title;
this.car_id = car_id;
}
}
CarAdapter.java
public class CarAdapter extends BaseAdapter {
private List<Car> mCarList;
private LayoutInflater mInflater;
public CarAdapter(List<Car> list, LayoutInflater inflater) {
mCarList = list;
mInflater = inflater;
}
@Override
public int getCount() {
return mCarList.size();
}
@Override
public Object getItem(int position) {
return mCarList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewItem item;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.prototype, null);
item = new ViewItem();
item.CarTitle = (TextView) convertView
.findViewById(R.id.TextViewPrototype);
convertView.setTag(item);
} else {
item = (ViewItem) convertView.getTag();
}
Car curCar = mCarList.get(position);
item.CarTitle.setText(curCar.title);
return convertView;
}
private class ViewItem {
TextView CarTitle;
}
}
CarActivity.java
public class ActivityCar extends Activity {
private List<Car> mCarList;
private EditText et;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.car);
// Obtain a reference to the product catalog
mCarList = CarHelper.getCar(getResources());
// Create the list
ListView listViewCatalog = (ListView) findViewById(R.id.list_car);
listViewCatalog.setAdapter(newCarAdapter(mCarList,getLayoutInflater)));
}
}
Thank you for your help.
Upvotes: 0
Views: 1437
Reputation: 5375
Follow the below steps to make the listview filterable:
In CarAdapter.java
implement the Filterable interface. Take a look at similar question answered here.
In CarActivity.java
set Listview adapter to be filterable.
listViewCatalog.setTextFilterEnabled(true);
If you had used android.widget.SearchView
then implement SearchView.OnQueryTextListener or if you used EditText
then implement TextWatcher interface.
Upvotes: 1