создать пользовательский вид с ImageView, Asynctask не работает - PullRequest
2 голосов
/ 10 сентября 2011

Я хочу добавить View программным способом к ViewFlipper:

flipper.addView(anEpisode(name, null, description), i); 

Мой метод:

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;
}

, а createUrlImage:

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);
}

Дело в том, что он вообще не отображает изображение, поэтому я не знаю, что делать.

1 Ответ

0 голосов
/ 17 сентября 2011

Я думаю, что проблема в рисованных границах. Установите границы в методе onPostExecute().

 protected void onPostExecute(Drawable d) {
    d.setBounds (0, 0, getIntrinsicWidth(), getIntrinsicHeight());
    image.setBackgroundDrawable(d);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...