我有一個行列表,每行都有一個Meals列表。什麼是填充我的ListView的最佳方式?這就是它看起來像現在:http://imgur.com/dMEcVUF這是我的適配器:Android:性能問題填充我的(複雜)ListView
public CanteenPlanAdapter(Context context, Plan plan) {
this.context = context;
if (plan != null) lines = plan.getLines();
}
@Override
public Object getItem(int position) {
if (lines != null) return lines.get(position);
else return null;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Line line = (Line) getItem(position);
List<Meal> meals = line.getMeals();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.canteen_list_group_item, parent, false);
for (int i = 0; i < meals.size(); i++) {
Meal meal = meals.get(i);
View child = inflater.inflate(R.layout.canteen_list_child_item, null);
TextView name = (TextView) child.findViewById(R.id.canteen_meal_name);
name.setText(meal.getMeal());
TextView price = (TextView) child.findViewById(R.id.canteen_meal_price);
price.setText(String.valueOf(meal.getPrice1()) + " €");
if (meal.isBio()) {
ImageView bio = (ImageView) child.findViewById(R.id.meal_bio);
bio.setVisibility(1);
}
if (meal.isCow()) {
ImageView cow = (ImageView) child.findViewById(R.id.meal_cow);
cow.setVisibility(1);
}
if (meal.isCow_aw()) {
ImageView cow_aw = (ImageView) child.findViewById(R.id.meal_cow_aw);
cow_aw.setVisibility(1);
}
if (meal.isFish()) {
ImageView fish = (ImageView) child.findViewById(R.id.meal_fish);
fish.setVisibility(1);
}
if (meal.isPork()) {
ImageView pork = (ImageView) child.findViewById(R.id.meal_pork);
pork.setVisibility(1);
}
if (meal.isVeg()) {
ImageView veg = (ImageView) child.findViewById(R.id.meal_veg);
veg.setVisibility(1);
}
if (meal.isVegan()) {
ImageView vegan = (ImageView) child.findViewById(R.id.meal_vegan);
vegan.setVisibility(1);
}
((ViewGroup) convertView).addView(child);
}
TextView name = (TextView) convertView.findViewById(R.id.canteen_line_name);
convertView.setTag(name);
name.setText(line.getName());
return convertView;
}
它的工作,但表現顯然很糟糕......我試着用Viewholder,但我不知道如何添加膳食列表項目..