【发布时间】:2013-03-01 11:10:26
【问题描述】:
我正在开发一个应用程序,我在其中解析JSON 数据并在列表视图中修复结果。在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粗体标签内的文本变为粗体?
对此的任何帮助将不胜感激。
【问题讨论】:
-
您需要在 Default 中制作自定义适配器,或者您可以尝试如下:
map.put("title", HTML.fromHtml(title));但我认为这行不通 -
当我这样做时出现错误。我正在这样做
map.put("title", Html.fromHtml(title));with `HashMap类型中的 put(String, String) 方法不适用于参数 (String, Spanned)` -
您必须选择 .第一个:在里面创建一个Custom Adapter。第二个是:将HashMap更改为
HashMap<String, Spanned> map = new HashMap<String, Spanned>();后尝试 -
好的。但是我怎样才能将它们添加到数组列表中。再次在
searchList.add(map);给我错误说明The method add(HashMap<String,String>) in the type ArrayList<HashMap<String,String>> is not applicable for the arguments (HashMap<String,Spanned>) -
你还需要把
ArrayList<HashMap<String,String>>改成ArrayList<HashMap<String,Spanned>>
标签: android html json android-layout android-listview