create custom view with ImageView, Asynctask not working
I want to add Views programmatically to a ViewFlipper :
flipper.addView(anEpisode(name, null, description), i);
my method is :
public View anEpisode(String n, String i, String d){
View v;
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.episodedescription, null);
TextView nameOfEpisode = (TextView) v.findViewById(R.id.episodedescriptionname);
ImageView imageOfEpisode = (ImageView) v.findViewById(R.id.episodedescriptionimage);
TextView descriptionOfEpisode = (TextView) v.findViewById(R.id.episodedescriptiondescription);
nameOfEpisode.setText(n);
descriptionOfEpisode.setText(d);
createUrlImage(imageOfEpisode, i);
return v;
}
and the createUrlImage is :
private class CreateImage extends AsyncTask<String, Void, Drawable> {
ImageView image;
public CreateImage(ImageView img) {
image = img;
}
protected void onPreExecute() {
}
protected Drawable doInBackground(String... urls) {
InputStream is;
Drawable d = null ;
try {
开发者_开发技巧 is = (InputStream)new URL(urls[0]).getContent();
d = Drawable.createFromStream(is, "Image");
return d;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return d;
}
protected void onPostExecute(Drawable d) {
image.setBackgroundDrawable(d);
}
}
public void createUrlImage(ImageView img, String url){
new CreateImage(img).execute(url);
}
The thing is : it doesn't display image at all so i don't know what to do.
I think the problem is with drawable bounds. Set the bounds in onPostExecute()
method.
protected void onPostExecute(Drawable d) {
d.setBounds (0, 0, getIntrinsicWidth(), getIntrinsicHeight());
image.setBackgroundDrawable(d);
}
精彩评论