Android Display HTML in ListView

Sorry if this is obvious to everyone else, but I have a little problem understanding how to display html inside my list.

My list has been announced.

ListView lv1 = (ListView) findViewById(R.id.ListView01); 

I fill it (not shown) and then set my list here using the ArrayAdapter.

 lv1.setAdapter(new ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, foods)); 

Next, I create a new array of strings in which I want to have bold tags. Then I add this new array (called arr_sort) to the arrayadapter insdie a onTextChanged () method.

 lv1.setAdapter(new ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort)); 

So now that my new array of strings has a <b> in it. How do I display bold text as a list?

Here is my new_list_view

  <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/grey2" android:textSize="20sp" android:gravity="center_vertical" android:paddingLeft="6dip" android:minHeight="40dip" /> 

And here is my part of ListView in my main layout.

  <ListView android:id="@+id/ListView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/rounded_corners_green" android:cacheColorHint="#00000000" android:divider="@color/green6" android:dividerHeight="1px" android:fastScrollEnabled="true" > </ListView> 

Any help would be greatly appreciated.

+6
source share
8 answers

override the adapter's getItem method and do the following:

 ArrayAdapter<String> adapter= ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort){ public Object getItem(int position) { return Html.fromHtml(arr_sort.get(position)); } }; 
+3
source

Ok, so Jitendra Sharma had the right idea for my script, but I needed to override the getView method. Or at least that's what worked for me. Then, in the getView method, I was able to configure the text to render in html.

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort) { @Override public View getView(int position, View convertView, ViewGroup parent) { View row; if (null == convertView) { row = mInflater.inflate(R.layout.new_list_view, null); } else { row = convertView; } TextView tv = (TextView) row.findViewById(android.R.id.text1); tv.setText(Html.fromHtml(getItem(position))); //tv.setText(getItem(position)); return row; } }; lv1.setAdapter(adapter); 
+15
source

If all you need is to display text where parts of the text should be bold, you only need one text text and correctly formatted text (with the addition of <b>) and do the following:

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

For more information on what TextView + Html supports, see here .

+2
source

If you are using the SimpleAdapter, here is the code that allows HTML on a TextView.

 adapter.setViewBinder(new SimpleAdapter.ViewBinder() { public boolean setViewValue(View view, Object data, String textRepresentation) { if (data instanceof Spanned && view instanceof TextView) { ((TextView) view).setText((Spanned) data); } else { ((TextView) view).setText(Html.fromHtml(String.valueOf(data))); } return true; } } ); 

Link: [Link] ( http://android.jreactor.com/2012/07/17/simpleadapter-spanned-html-fromhtml/ )

+2
source

If you have the opportunity to download your texts from strings.xml by adding a tag, you will automatically select your text. If your texts are dynamic, you will need to create a custom adapter and install the text in it using textView.setText(Html.fromHtml(yourText));

+1
source
 ArrayAdapter<Spanned> listAdapter = new ArrayAdapter<Spanned>(MyActivity.this, R.layout.row); listAdapter.add(Html.fromHtml(htmlText)); listAdapter.add(Html.fromHtml(htmlText)); ... 
0
source

if you use ksoap for html data from any database engine

 yourVariable=String.valueOf(Html.fromHtml(ic.getProperty(0).toString())) 
0
source

It also works and perphaps are much simpler. First pass the 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 an ArrayAdapter using the CharSequence parameter

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

Adapted from here

-1
source

Source: https://habr.com/ru/post/910559/


All Articles