子(サブアイテム)が描画可能なフォルダーまたはテキストからの画像を表示している展開可能なリストを作成しました。
Webの画像でも同じことをしたい。したがって、NetworkOnMainThreadException を回避するために asynctask も使用しました。
このコードは、アイテムとサブアイテムを初期化する 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;
}
現時点では、展開可能なリストには、描画可能なフォルダーとテキストビューの画像しか表示されませんが、インターネットからの画像はありません。何が問題ですか ?