我正在开发一个应用程序,我正在解析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);
这里是一切顺利的样子。
正如您所见,html
标签即将发布。任何人都可以建议如何在html
粗体标签内加粗文字?
对此,我们将不胜感激。
答案 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))。