【问题标题】:How to make dynamic text bold having tag <b> </b> in listview?如何在列表视图中使具有标签 <b> </b> 的动态文本加粗?
【发布时间】: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&lt;String, Spanned&gt; map = new HashMap&lt;String, Spanned&gt;();后尝试
  • 好的。但是我怎样才能将它们添加到数组列表中。再次在searchList.add(map); 给我错误说明The method add(HashMap&lt;String,String&gt;) in the type ArrayList&lt;HashMap&lt;String,String&gt;&gt; is not applicable for the arguments (HashMap&lt;String,Spanned&gt;)
  • 你还需要把ArrayList&lt;HashMap&lt;String,String&gt;&gt;改成ArrayList&lt;HashMap&lt;String,Spanned&gt;&gt;

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


【解决方案1】:

只需使用 SpannedString 代替普通字符串,然后使用 setText() 将其显示在 TextView 上。

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

【讨论】:

    【解决方案2】:

    您可以使用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);
    

    【讨论】:

    • 谢谢你的回答,你真的很有帮助。
    【解决方案3】:

    在适配器类中设置标题时尝试作为休闲方式。

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

    【讨论】:

      【解决方案4】:

      在放入 Map 之前使用 Html.fromHtml()

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

      【讨论】:

      • 它不起作用。 SimpleAdapter 调用 toString 所以所有标记信息都将丢失。
      • 出现错误。声明The method put(String, String) in the type HashMap&lt;String,String&gt; is not applicable for the arguments (String, Spanned)。我正在做这样的事情。 Html.fromHtml(title); Html.fromHtml(desc); Html.fromHtml(link); map.put("title", Html.fromHtml(title)); map.put("description", Html.fromHtml(desc)); map.put("link", Html.fromHtml(link));
      • 正如 ρяσѕρєя K 所说,您需要更改哈希映射通用时间 HashMap map
      猜你喜欢
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      相关资源
      最近更新 更多