zikezi 2013-06-11 02:17 采纳率: 0%
浏览 3560

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条回答 默认 最新

  • 广州-张瑞宝 2017-02-13 08:26
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘