如何在listview中生成带有标记<b> </b>的动态文本?

时间:2013-03-13 13:24:52

标签: android html json android-layout android-listview

我正在开发一个应用程序,我正在解析JSON数据并将结果修复到listview中。在JSON回复中,我得到以下回复:

{
"searchdata": {
    "webresult": [
        {
            "title": "<b>Android</b>",
            "desc": "Discover a new flavor of Jelly Bean <b>Android</b> 4.2 introduces a completely new camera experience, a new form of typing that helps you power ...",
            "link": "http://www.android.com/"
        },
        {
            "title": "<b>Android</b> (operating system) - Wikipedia, the free encyclopedia",
            "desc": "<b>Android</b> is a Linux -based operating system designed primarily for touchscreen mobile devices such as smartphones and tablet computers. Initially developed by <b>Android</b> ...",
            "link": "http://en.wikipedia.org/wiki/Android_(mobile_phone_platform)"
        }
    ]
}

}

title,您可以看到 Android 标签即将推出。我想大胆一点。

以下是我在列表视图中修复这些响应的工作。

try {
    HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet("MY API");
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet, rHandler);

JSONObject rootObj = new JSONObject(data);

JSONObject jSearchData = rootObj.getJSONObject("searchdata");

JSONArray jsonArray = jSearchData.getJSONArray("webresult");

for (int i = 0; i < jsonArray.length(); i++) {

    JSONObject objJson = jsonArray.getJSONObject(i);

    String title = objJson.getString("title");
    String desc = objJson.getString("desc");
    String link = objJson.getString("link");

    HashMap<String, String> map = new HashMap<String, String>();

    map.put("title", title);
    map.put("description", desc);
    map.put("link", link);

    searchList.add(map);
    }

} catch (Exception e) {
       System.out.println("Exception: " + e);
}

通过上面的代码,我得到了响应并将它们存储到Hashmap中。之后我使用自定义布局来显示内容。以下代码可帮助我在布局中修复值。

ListAdapter adapter = new SimpleAdapter(getApplicationContext(),
searchList, R.layout.list_row, new String[] { "title","description", "link" }, new int[] { R.id.title,R.id.desc, R.id.url });

setListAdapter(adapter);

这里是一切顺利的样子。

Screenshot from device of the layout

正如您所见,html标签即将发布。任何人都可以建议如何在html粗体标签内加粗文字?

对此,我们将不胜感激。

4 个答案:

答案 0 :(得分:4)

您可以使用SimpleAdapter.ViewBinder并将html值设置为TextView,而不是纯文本。

SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), searchList, R.layout.list_row, new String[] { "title","description", "link" }, new int[] { R.id.title,R.id.desc, R.id.url });
SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Object object, String value) {
        if (view instanceof TextView) {
            ((TextView) view).setText(Html.fromHtml(value));
            return true;
        }

        return false;
    }
};

adapter.setViewBinder(binder);
setListAdapter(adapter);

答案 1 :(得分:2)

在输入Map

之前使用Html.fromHtml()
map.put("title", HTML.fromHtml(title));
map.put("description", HTML.fromHtml(desc));
map.put("link", HTML.fromHtml(link));

答案 2 :(得分:1)

只需使用SpannedString而不是普通字符串,然后使用setText()在TextView上显示它。

示例: SpannedString spannedText = new SpannedString(Html.fromHtml(value)); textView.setText(spannedText);

答案 3 :(得分:0)

在Adapter类中设置标题时,只需尝试休闲。

holder.title.setText(Html.fromHtml(searchList.getTitle))。