I have a custom adapter with just an ImageView and a TextView. When scrolling, it is very slow and if you scroll enough times, the app will eventually stop. I am thinking the way I approached this is not efficient?
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;
}
}
If I only use the same image for each list element, I do not get the slow scrolling. Any help is appreciated
edit:
CustomAdapter(Context context, ArrayList<Breakfast> breakfastArrayList) {
super(context, R.layout.custom_row, breakfastArrayList);
breakfastArray = breakfastArrayList;
}
it does not allow me to pass breakfastArrayList as the third parameter
Have a breakfast data class like this
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;
}
}
Create an arraylist of Breakfast objects like this
ArrayList<Breakfast> breakfastList = new ArrayList<>();
breakfastList.add(new Breakfast("Bacon",R.drawable.bacon);
...
..
.
Pass this arraylist to your 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 : Given that you are starting to learn Android, would suggest you to look into RecyclerView rather than ListView