0

子(サブアイテム)が描画可能なフォルダーまたはテキストからの画像を表示している展開可能なリストを作成しました。

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

現時点では、展開可能なリストには、描画可能なフォルダーとテキストビューの画像しか表示されませんが、インターネットからの画像はありません。何が問題ですか ?

4

1 に答える 1

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 クラスでインターネットから画像を表示するために別のプライベート クラスを作成する必要はありません。できます。

于 2013-04-03T00:06:58.560 に答える