2

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ricefieldboy
  • 347
  • 1
  • 4
  • 15

2 Answers2

3

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

Arun Shankar
  • 2,295
  • 16
  • 20
  • sorry to bother you, what should the constructor of CustomAdapter look like? I have this: i'll edit my original post – ricefieldboy Jun 13 '17 at 01:28
  • I have edited my answer with the constructor. If this works for you, please accept this answer. Thanks – Arun Shankar Jun 13 '17 at 01:34
  • Anyway, i finally got it working, thank you. There is an issue with using ArrayList.add too many times and would cause it to stop. – ricefieldboy Jun 13 '17 at 02:12
2

I'm not sure what your error is but from looking at your code your I'm not sure why you used an ArrayAdapter for this. If you want to display an image based on the the text I would create a 'BreakfastItem' class with an breakfast item; cereal, pop tart, toast, and pair the image with the breakfast item. You can then display that as a list item.

Sav
  • 23
  • 4
  • I just started learning android dev, I am following a tutorial and playing around with it – ricefieldboy Jun 13 '17 at 00:52
  • 1
    Okay so the array adapter is just used to tell the list view how you want each array list item to look. Say you have your different breakfast choices, you can create a class that takes in a name and an image. Then in the ArrayAdapter class you're telling the adapter to put both items in space that corresponds with the widget in xml. is you have a (TextView) in your list_item xml your setting the breakfast item name to that and the image to an (ImageView). But on the actual activity where the data is displayed is where you're adding variations of breakfast choices to your list... – Sav Jun 13 '17 at 00:58