2 zikezi zikezi 于 2013.06.11 10:17 提问

android4.2 在listview中解析带有img标签的html,并显示在textview中

代码均在listeview的适配器里,主要代码如下:

public View getView(int position, View v, ViewGroup parent) {
        LiveContextHolder lvcHolder;
        if (v == null) {
            v = inflater.inflate(R.layout.live_contxt_item, null);
            lvcHolder = new LiveContextHolder();
            lvcHolder.txtv_time = (TextView) v
                    .findViewById(R.id.lvcitm_txtv_time);
            lvcHolder.txtv_contxt = (TextView) v
                    .findViewById(R.id.lvcitm_txtv_contxt);
            lvcHolder.txtv_rmax = (TextView) v
                    .findViewById(R.id.lvcitm_txtv_rmax);
            lvcHolder.txtv_qmax = (TextView) v
                    .findViewById(R.id.lvcitm_txtv_qmax);

            v.setTag(lvcHolder);
        } else {
            lvcHolder = (LiveContextHolder) v.getTag();
        }

        LiveContxt lvc = liveContxts.get(position);

        lvcHolder.txtv_time.setText(lvc.getTime());
        lvcHolder.txtv_qmax.setText(lvc.getQmax());
        lvcHolder.txtv_rmax.setText(lvc.getRmax());
        CharSequence html;
        String html_c = lvc.getM_alias()+": "+StringCL.rplceStr(lvc.getContxt());
        StringBuffer html_r =  new StringBuffer("");
        if (lvc.getRplyLst() != null && lvc.getRplyLst().size() > 0) {
            for (RplyContxt rply : lvc.getRplyLst()) {
                html_r.append("--答--").append(rply.getM_alias()).append(":")
                        .append(StringCL.rplceStr(rply.getRply()));
                html_r.append("   ");
            }
        }
        html = Html.fromHtml(html_c + "  " +html_r.toString(),imageGetter,null);
        lvcHolder.txtv_contxt.setText(html);

        return v;
    }



    final Html.ImageGetter imageGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            //source="http://192.168.0.145:8888/hxzb/data/Image/2013-06/20130609152823009.jpg";
            // 在此必须异步加载图片
            Drawable d = null;
            try {
                InputStream is = new DefaultHttpClient().execute(new HttpGet(source)).getEntity().getContent();
                Bitmap bm = BitmapFactory.decodeStream(is);
                d = new BitmapDrawable(context.getResources(), bm);
                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicWidth());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return d;
        }
    };

问题在于在imageGetter时要开线程,但开线程也会引发其他问题,如图片还么下载完,函数就已经返回了,会有空值,并且我也不知怎么写这个线程,望高手指导!!!

1个回答

u010434246
u010434246   2017.02.13 16:26

建立楼主使用框架进行图片的加载

Csdn user default icon
上传中...
上传图片
插入图片