How to make dynamic text bold having tag <b> </b> in listview?

青春壹個敷衍的年華 提交于 2020-01-07 04:37:07

问题


I'm developing an application in which I'm parsing JSON data and fixing the result in the listview. In JSON response I'm getting the following response:

{
"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)"
        }
    ]
}

}

In title as you can see Android tags are coming. I want to make it bold.

Here is what I'm doing for fixing those response in the listview.

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);
}

By the above code I'm getting the response and storing them into Hashmap. After that I'm using custom layout for displaying the content. Following code helps me getting the values fixed in the layout.

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);

Here, is what it looks like when all things go well.

As you can see the html tags are coming. Can please anyone suggest how to make the text bold inside the html bold tags?

Any help will be appreciated for this.


回答1:


You can use SimpleAdapter.ViewBinder and set a html value to the TextView instead of plain text.

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);



回答2:


Use Html.fromHtml() before you put in to Map

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



回答3:


Just use SpannedString instead of normal string and then display it on TextView using setText().

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




回答4:


In Adapter class while setting the title just try as fallow.

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



来源:https://stackoverflow.com/questions/15386631/how-to-make-dynamic-text-bold-having-tag-b-b-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!