我有一个只有ImageView和TextView的自定义适配器。滚动时,它非常慢,如果你滚动足够多次,应用程序最终会停止。我在想我接近这个方法的效率不高?
public class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, String[] breakfast) {
super(context, R.layout.custom_row, breakfast);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_row, parent, false);
}
String breakfastElement = getItem(position);
TextView customListTextView = (TextView) convertView.findViewById(R.id.customListTextView);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
customListTextView.setText(breakfastElement);
switch(breakfastElement) {
case "Bacon":
imageView.setImageResource(R.drawable.bacon);
break;
case "Eggs":
imageView.setImageResource(R.drawable.eggs);
break;
case "Toast":
imageView.setImageResource(R.drawable.toast);
break;
case "Ham":
imageView.setImageResource(R.drawable.ham);
break;
case "Hashbrown":
imageView.setImageResource(R.drawable.hashbrowns);
break;
case "Biscuits":
imageView.setImageResource(R.drawable.biscuit);
break;
case "Burrito":
imageView.setImageResource(R.drawable.burrito);
break;
case "Cereal":
imageView.setImageResource(R.drawable.cereal);
break;
case "Oatmeal":
imageView.setImageResource(R.drawable.oatmeal);
break;
case "Sausage":
imageView.setImageResource(R.drawable.sausage);
break;
case "Bagel":
imageView.setImageResource(R.drawable.bagel);
break;
case "Croissant":
imageView.setImageResource(R.drawable.croissant);
break;
case "Orange Juice":
imageView.setImageResource(R.drawable.orangejuice);
break;
case "Milk":
imageView.setImageResource(R.drawable.milk);
break;
}
return convertView;
}
}
如果我只为每个列表元素使用相同的图像,我不会慢速滚动。 任何帮助表示赞赏
编辑:
CustomAdapter(Context context, ArrayList<Breakfast> breakfastArrayList) {
super(context, R.layout.custom_row, breakfastArrayList);
breakfastArray = breakfastArrayList;
}
它不允许我传递breakfastArrayList作为第三个参数
答案 0 :(得分:3)
有这样的早餐数据类
public class Breakfast {
private String name;
private Drawable drawable;
public Breakfast(String name, Drawable drawable) {
this.name = name;
this.drawable = drawable;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Drawable getDrawable() {
return drawable;
}
public void setDrawable(Drawable drawable) {
this.drawable = drawable;
}
}
创建像这样的早餐对象的arraylist
ArrayList<Breakfast> breakfastList = new ArrayList<>();
breakfastList.add(new Breakfast("Bacon",R.drawable.bacon);
...
..
.
将此arraylist传递给您的customadapter
public class CustomAdapter extends ArrayAdapter<Breakfast> {
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public CustomAdapter(Context context, int resource, List<Breakfast> items) {
super(context, resource, items);
}
...
...
...
customListTextView.setText(breakfastList.get(position).getName());
imageView.setImageDrawable(breakfastList.get(position).getDrawable());
}
PS:鉴于你开始学习Android,建议你调查RecyclerView而不是ListView
答案 1 :(得分:2)
我不确定你的错误是什么,但是通过查看你的代码我不知道为什么你使用了ArrayAdapter。如果你想根据文字显示图像,我会创建一个含有早餐项目的'BreakfastItem'类;谷物,流行挞,烤面包,并将图像与早餐项目配对。然后,您可以将其显示为列表项。