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中的圖像,但沒有圖像從互聯網上。問題是什麼 ?

+0

你有你的AndroidManifest.xml中的互聯網許可嗎?你也應該使用一個處理程序來設置imageview的drawable。 –

+0

是的,我有互聯網許可。在這種情況下,經理如何提供幫助?你能舉個例子嗎? – Asqan

+0

由於您處於與主線程不同的線程中,因此無法設置繪圖。只需定義一個附加到UI線程的新處理程序,或者您可以使用runonmainthread。 activity.runOnUiThread(新的Runnable(){@Override 公共 無效的run(){// 你的代碼在GUI線程跑這裏 } }); –

回答

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類。有用。