图片中spannable的EditText显示两次 [英] Picture in spannable edittext display twice

查看:336
本文介绍了图片中spannable的EditText显示两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个审查制度,允许添加文字,并通过imgur上传图片。
当我显示审查,图片显示了两次,我不understant为什么。

I have a review system which allows to add text and upload picture via imgur. When I display the review, the pic displays twice, I don't understant why.

这里是code部分:

public class ReviewListAdapter extends BaseAdapter {
...
    public View getView(int position, View convertView, ViewGroup parent) {
    ....
        ViewHolder holder;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder(convertView);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
          ...

    if (text != null) {

        int begin = text.indexOf("[img]");
        int end = text.indexOf("[/img]");
        if (begin != -1 && end!=-1 && begin<end) {
            String imgUrl = text.subSequence(begin + 5, end).toString();
            imgUrl = imgUrl.substring(0,imgUrl.length()-4)+"m"+imgUrl.substring(imgUrl.length()-4, imgUrl.length());
            Log.e("IMG", imgUrl); // imgUrl == http://i.imgur.com/AL4Em6cm.jpg

            startSignal = new CountDownLatch(1);
            new GetImage().execute(imgUrl);
            try {
                startSignal.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // We should have the picture in the Drawable : myPicture
            SpannableStringBuilder ssb = new SpannableStringBuilder(text);

            Bitmap pic = Utils.drawableToBitmap(myPicture);
            ssb.setSpan(new ImageSpan(pic), begin, end + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.textView.setText(ssb, BufferType.SPANNABLE);
        }else{
            holder.textView.setText(text);
        }


    }

我也把私有类的getImage(里面的同一个文件)

I also put the Private class GetImage ( Inside the same file)

private class GetImage extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... params) {

        URL url;
        boolean isPicture = false;

        try {
            if (!params[0].equals(""))
                isPicture = true;

            if (isPicture)
                url = new URL(params[0]);
            else
                url = new URL(icon);

            Bitmap mIcon1 = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            if (isPicture)
                myPicture = new BitmapDrawable(ctx.getResources(), mIcon1);
            else
                myIcon = new BitmapDrawable(ctx.getResources(), mIcon1);

            startSignal.countDown();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

我的目录布局(list_item.xml)

My list layout (list_item.xml )

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
             android:contentDescription="thumb"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_marginTop="5dp"
                android:id="@+id/author_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/author_name"
                android:layout_gravity="center"
                android:paddingRight="8dp"
                android:textSize="12sp" />
        </RelativeLayout>
    </LinearLayout>

<TextView
    android:id="@+id/texto"
    android:layout_below="@id/ll1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
 />

</RelativeLayout>

我的图片插入到的TextView作为spannable含量

My picture are inserted in the textview as spannable content

推荐答案

我也有类似的问题,因为你的。该ImageSpan是在一定条件下显示两次。 <一href=\"http://stackoverflow.com/questions/22739748/why-does-android-imagespan-show-my-picture-twice-when-setbounds-exceed-certain\">Here是链接到我的问题。我发现如果setBound大于一定宽度宽,所述图像示出两次。虽然你的code不涉及setBound电话。

I had a similar problem as yours. The ImageSpan is shown twice at certain condition. Here is the link to my question. I found out if the setBound is wider than a certain width, the picture is shown twice. Although your code doesn't involve setBound calls.

这篇关于图片中spannable的EditText显示两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆