Created
October 7, 2012 20:23
-
-
Save cheezy/3849469 to your computer and use it in GitHub Desktop.
My ImageLoader class
This file contains hidden or 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
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; | |
} | |
} | |
} |
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
And the update to getView in PuppyAdapter