Android Display HTML in a ListView

后端 未结 9 2066
情话喂你
情话喂你 2020-12-16 05:54

Sorry if this is obvious to everyone else but I am having a minor difficulty understanding how to display html inside my listview.

My list view is declared.

相关标签:
9条回答
  • 2020-12-16 06:19

    If all you wanted is to display some text where parts of the text should be bold, all you need is one TextView, and properly formatted text (with <b> added) and do the following:

    textview.setText(Html.fromHtml(text));
    

    For more information on what TextView+Html can support, see here

    0 讨论(0)
  • 2020-12-16 06:20

    This also works and is perphaps a lot simpler. First, pass your data from String[] to Spanned[]

    Spanned[] myhtmldata = new Spanned[mydata.length];
    for(int i = 0 ; i < mydata.length; i++) {
    myhtmldata[i] = Html.fromHtml(mydata[i]);
    }
    

    Then declare the ArrayAdapter using the CharSequence parameter

    ArrayAdapter<CharSequence> linksadapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.list_item, R.id.textview, myhtmldata);         
    setListAdapter(linksadapter);
    

    Borrowed from here

    0 讨论(0)
  • 2020-12-16 06:23
    ArrayAdapter<Spanned> listAdapter = new ArrayAdapter<Spanned>(MyActivity.this, R.layout.row);
    listAdapter.add(Html.fromHtml(htmlText));
    listAdapter.add(Html.fromHtml(htmlText));
    ...
    
    0 讨论(0)
提交回复
热议问题