Skip to content

Instantly share code, notes, and snippets.

@cheezy
Created October 7, 2012 20:23
Show Gist options
  • Save cheezy/3849469 to your computer and use it in GitHub Desktop.
Save cheezy/3849469 to your computer and use it in GitHub Desktop.
My ImageLoader class
package com.leandog.puppies.data;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.drawable.Drawable;
public class ImageLoader {
public Drawable loadImageFrom(final String url) {
String imageUrl = "http://leandogasn.local:3000/assets/" + url;
Drawable image = imageOperations(imageUrl);
return image;
}
private Object fetch(String address) throws MalformedURLException, IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
private Drawable imageOperations(String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
}
@cheezy
Copy link
Author

cheezy commented Oct 7, 2012

And the update to getView in PuppyAdapter

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View theView = convertView;

        if( null == theView) {
            final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            theView = inflater.inflate(layout.puppy_item, null);
        }

        final Puppy thePuppy = thePuppies.get(position);
        ImageView imageView = findFor(theView, id.puppy_image);
        imageView.setImageDrawable(imageLoader.loadImageFrom(thePuppy.getImageUrl()));
        setText(theView, id.name, thePuppy.getName());
        setText(theView, id.breed, thePuppy.getBreed());
        setText(theView, id.gender, thePuppy.getGender());
        return theView;
    }

@cheezy
Copy link
Author

cheezy commented Oct 7, 2012

And the update to PuppyAdapter

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View theView = convertView;

        if( null == theView) {
            final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            theView = inflater.inflate(layout.puppy_item, null);
        }

        final Puppy thePuppy = thePuppies.get(position);
        ImageView imageView = findFor(theView, id.puppy_image);
        imageView.setImageDrawable(imageLoader.loadImageFrom(thePuppy.getImageUrl()));
        setText(theView, id.name, thePuppy.getName());
        setText(theView, id.breed, thePuppy.getBreed());
        setText(theView, id.gender, thePuppy.getGender());
        return theView;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment