在可扩展列表中查看图像列表

时间:2013-03-28 18:32:47

标签: android multithreading image android-asynctask expandablelistview

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

我想对来自网络的图片做同样的事情。因此我也使用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的图像,但没有来自互联网的图像。问题是什么 ?

1 个答案:

答案 0 :(得分:0)

我已经修好了!

getChildView():

 ....    
 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类中显示来自Internet的图像。它有效。