Created
March 12, 2013 13:01
-
-
Save rnystrom/5142700 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UpdateAdapter extends ArrayAdapter<ParseObject> { | |
// omitted vars | |
public List<ParseObject> updates = new ArrayList<ParseObject>(); | |
public HashMap<String, Bitmap> updateImages = new HashMap<String, Bitmap>(); | |
// omitted constructor + @Overrides | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
UpdateHolder holder = null; | |
if (convertView == null) { | |
// omitted inflater setup | |
holder = new UpdateHolder(); | |
holder.imageView = (ImageView) convertView.findViewById(R.id.update_image); | |
convertView.setTag(holder); | |
} | |
else { | |
holder = (UpdateHolder) convertView.getTag(); | |
} | |
ParseObject update = updates.get(position); | |
ParseFile imageData = (ParseFile) update.get(Update.IMAGE_KEY); | |
if (imageData != null && holder.imageView.getTag() == null) { | |
Bitmap bmp = updateImages.get(imageData.getUrl()); | |
if (bmp != null) { | |
holder.imageView.setImageBitmap(bmp); | |
} | |
else { | |
holder.imageView.setImageResource(R.drawable.placeholder_square); | |
holder.imageView.setTag(this); | |
loadImage(imageData, holder.imageView); | |
} | |
} | |
return convertView; | |
} | |
void loadImage(final ParseFile parseFile, final ImageView imageView) { | |
parseFile.getDataInBackground(new GetDataCallback() { | |
@Override | |
public void done(byte[] data, ParseException e) { | |
if(imageView.getTag() != null && data != null) { | |
InputStream is = new ByteArrayInputStream(data); | |
Bitmap bmp = BitmapFactory.decodeStream(is); | |
imageView.setImageBitmap(bmp); | |
updateImages.put(parseFile.getUrl(), bmp); | |
} | |
imageView.setTag(null); | |
} | |
}); | |
} | |
public class UpdateHolder { | |
// omitted view vars | |
ImageView imageView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment