0

我做了一个可扩展的列表,其中子项(子项)显示来自可绘制文件夹或文本的图像。

我想对来自网络的图像做同样的事情。因此,我还使用了 asynctask 来避免NetworkOnMainThreadException

此代码在我的 ExpandableListAdapter 中,我在其中初始化项目和子项目。Drawable 和 ImageView 是全局定义的,因此所有函数都可以在不发送参数的情况下访问它们。

Drawable drawable;
ImageView imgView;


@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

String child = childList[groupPosition][childPosition];

    if(child.equalsIgnoreCase("none")){
        ImageView i = new ImageView(context);
        i.setImageResource(R.drawable.blank_image_for_view_business);
        i.setPadding(0, 0, 0, 0);
        return i ;          
    }
      else if(child.startsWith("http")){
         imgView = new ImageView(context);
        new asyTask();
        return imgView;
    }else{
        TextView tv = new TextView(context);
        tv.setText(childList[groupPosition][childPosition]);
        tv.setPadding(30, 0, 0, 0);
        return tv;
    }
}

private Drawable LoadImageFromWeb(String url){
      try{
          InputStream is = (InputStream) new URL(url).getContent();
          Drawable d = Drawable.createFromStream(is, "src name");
          return d;
      }catch (Exception e) {
          System.out.println("Exc="+e);
          return null;
      }
}

这是我的异步课程:

private class asyTask extends AsyncTask<String, Void, Integer> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //show a progress bar
    }


    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        imgView.setImageDrawable(drawable);   
    }

    @Override
    protected Integer doInBackground(String... params) {
        drawable  =  LoadImageFromWeb("http://www.ettinger.co.uk/var/ezflow_site/storage/images/collections/ettinger/ettinger-walkie-pen/lifestyle-navy-walkie-pen/navy-walkie-pen-gallery/wlkp_pens_navy-2/20569-2-eng-GB/wlkp_pens_navy-2.jpg"); 
        return 0;
    }

目前,在可扩展列表中,我只能看到来自可绘制文件夹和 textview 的图像,但没有来自互联网的图像。问题是什么 ?

4

1 回答 1

0

我已经修好了!

获取儿童视图():

 ....    
 else if (child.startsWith("http")) {       
     Drawable image = ImageOperations(context,child,"image.jpg");
     ImageView imgView = new ImageView(context);
     imgView.setImageDrawable(image);
     imgView.setPadding(0, 0, 0, 0);
     return imgView;
     }....

这将调用以下方法:

    private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}

因此,无需创建其他私有类来在 ExpandableListAdapter 类中显示来自互联网的图像。有用。

于 2013-04-03T00:06:58.560 回答