textview show html data:图片不显示ListAcitivity

时间:2013-04-19 01:53:24

标签: android image android-listview textview

我想用textview显示html数据。所以我使用Html.fromHTML()方法。 除图像外,所有HTML数据都显示正确。图像只是在一个小型垃圾箱中显示“OBJ”。我无法弄清楚这个问题。

我的代码可能会更清楚地表达我的问题:

public class MyActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    fetchData();
}

private void fetchData() {
    new fetchDataTask().execute();
}

private class fetchDataTask extends AsyncTask<Void, Integer, ArrayList<Map<String, CharSequence>>> {

    @Override
    protected void onPostExecute(ArrayList<Map<String, CharSequence>> result) {
        // set adapter
        String[] from = { "title", "content" };
        int[] to = { R.id.title, R.id.content };
        SimpleAdapter adapter = new SimpleAdapter(MyActivity.this,
                result, R.layout.detail_item, from, to);
        setListAdapter(adapter);
    }

    @Override
    protected ArrayList<Map<String, CharSequence>> doInBackground(Void... params) {
        ArrayList<Map<String, CharSequence>> contentList = new ArrayList<Map<String, CharSequence>>();

        try {
            String dataid = getIntent().getExtras().getString("dataid");
            // fetch json data from webapi
            String result = MyUtil.fetchJsonData("http://www.mywebsite.com/datacontent" + dataid);
            JSONObject data = new JSONObject(result);
            JSONArray contentJArray = data.getJSONArray("content");
            for (int i = 0; i < contentJArray.length(); i++) {
                JSONObject oneObject = contentJArray.getJSONObject(i);
                Map<String, CharSequence> map = new HashMap<String, CharSequence>();
                map.put("id", oneObject.getString("id"));
                map.put("title", oneObject.getString("title"));

                // process content which is html format
                // the content is a html format data as following:
                // hello world, <img src="www.mysite.com/hello.jpg">.<br/>
                ImageGetter imageGetter = new ImageGetter() {
                    @Override
                    public Drawable getDrawable(String source) {
                        Drawable d = null;
                        try {
                            InputStream is = (InputStream) new URL(source)
                                    .getContent();
                            d = Drawable.createFromStream(is, "src");
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                        return d;
                    }

                };
                CharSequence content = Html.fromHtml(
                        oneObject.getString("content"), imageGetter, null);
                map.put("content", content);
                contentList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return contentList;
    }

}

}

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

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="10dp"
        android:textSize="16sp" >
    </TextView>

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="10dp"
        android:textSize="16sp" >
    </TextView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

使用Html.fromHTML()时,HTML中的所有标记都会显示为一般替换图像,然后您的程序可以通过该图像替换为实际图像。