Search code examples
androidlistviewparse-platformandroid-arrayadapter

Retrieving an image from parse.com in a List View using an Array Adapter


I have data stored on parse.com. I am trying to retrieve an image and a string and place it in a list view. I have an adapter that I have been using to grab the text but unsure how to do with image. There is one line in particular I need help with.

public class AppetizerAdapter extends ArrayAdapter {

protected Context mContext;
protected List mAppetizer;

public AppetizerAdapter(Context context, List appetizer) {
    super(context, R.layout.custom_appetizers, appetizer);

    mContext = context;
    mAppetizer = appetizer;
}

// inflates each row of the app
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    // initialize view holder
    ViewHolder holder;
    if (convertView == null) { // If no items in the view to be displayed
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.custom_appetizers, null);

        // initialize the views
        holder = new ViewHolder(); // creates a new view
        holder.aPPetizer = (TextView) convertView // calls the view
                .findViewById(R.id.appetizer);

        holder = new ViewHolder();
        holder.appetizerImage = (ImageView) convertView
                .findViewById(R.id.appetizerImage);

        // call the view and setTag to the parameter (holder)
        convertView.setTag(holder);

    } else { // if view is previously displayed

        // initialize holder
        holder = (ViewHolder) convertView.getTag();

    }

    // get the position of the row
    ParseObject appetizerObject = (ParseObject) mAppetizer.get(position);

    // title
    String appetizers = appetizerObject.getString("appetizers"); 
    holder.aPPetizer.setText(appetizers);

    // image
    ???
    holder.appetizerImage.setImageResource();

    return convertView; // return the view
}

public static class ViewHolder {

    // declaration of variables
    TextView aPPetizer;
    ImageView appetizerImage;

}

The part I am having a problem with is:

// title
String appetizers = appetizerObject.getString("appetizers");

This allows me to store the string in the column "appetizers" - which is a string, but for an image, I am not sure how to code this.


Solution

  • Assuming you can get your image's url from the appetizerObject reference, you will just need to download the bitmap from the network and set it as the ImageView's bitmap.

    While there are many ways to implement it manually, i would recommend using a library to manage both the download and the caching process. From my experience, i have used the following libraries:

    Both of them have a really simple (and similar) api.