我们需要在这个例子中实现过滤列表视图的代码。我们使用了 2 个类:(Car.java
包含 Object Car),ClassCarAdapter
扩展了BaseAdapter
Class。下面是一些代码:
汽车.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)));
}
}
谢谢您的帮助。