简体   繁体   English

Android在ListView中显示HTML

[英]Android Display HTML in a ListView

Sorry if this is obvious to everyone else but I am having a minor difficulty understanding how to display html inside my listview. 抱歉,如果这对于其他所有人来说都是显而易见的,但是我在理解如何在列表视图中显示html时遇到了一个小困难。

My list view is declared. 我的列表视图已声明。

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

I populate it (not shown) then set my listview here with an ArrayAdapter. 我填充它(未显示),然后在此处使用ArrayAdapter设置列表视图。

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

Further down I create a new array of strings that I want to have bold tags in. I then add this new array (called arr_sort) to the arrayadapter insdie a onTextChanged() method. 再往下,我创建一个新的字符串数组,我想要在其中包含粗体标签。然后,我将此新数组(称为arr_sort)添加到位于onTextChanged()方法的arrayadapter中。

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

So now that my new Array of Strings has < b > tags in it. 因此,现在我的新字符串数组中具有<b>标记。 How do I make my listview display the bold text? 如何使列表视图显示粗体文本?

Here is my new_list_view 这是我的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 ListView part in my main layout. 这是我的主布局中的ListView部分。

        <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 much appreciated. 任何帮助将非常感激。

Ok, so Jitendra Sharma was had the right idea for my scenario, but I needed to override the getView method. 好的,因此Jitendra Sharma对于我的情况是正确的主意,但是我需要重写getView方法。 Or at least that is what worked for me. 或至少这对我有用。 Then in the getView method I was able to set my text to render in html. 然后,在getView方法中,我可以将我的文本设置为以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);

If you are using a SimpleAdapter, here is the code that enables HTML on a TextView. 如果您使用的是SimpleAdapter,则以下代码可在TextView上启用HTML。

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

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

override getItem method of the Adapter and do the following: 重写Adapter的getItem方法并执行以下操作:

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

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,并正确格式化文本(添加<b>),然后执行以下操作:

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

For more information on what TextView+Html can support, see here 有关TextView + HTML可以支持的更多信息,请参见此处。

If you have the possibility of loading your texts from strings.xml, adding the tag there will automatically bold your text. 如果可以从strings.xml加载文本,则在其中添加标签将自动使文本加粗。 If however your texts are dynamic, you will have to create a custom adapter , and in it to set the text using textView.setText(Html.fromHtml(yourText)); 但是,如果文本是动态的,则必须创建一个自定义适配器 ,并在其中使用textView.setText(Html.fromHtml(yourText));设置文本textView.setText(Html.fromHtml(yourText));

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

如果您将ksoap用于来自任何数据库引擎的html数据

yourVariable=String.valueOf(Html.fromHtml(ic.getProperty(0).toString()))
    adapter = new ArrayAdapter<String>(rr.getContext(),android.R.layout.simple_list_item_1,titulos)
    {
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View row;

            if (null == convertView) {
                row = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null);
            } else {
                row = convertView;
            }
            TextView tv = (TextView) row.findViewById(android.R.id.text1);
            tv.setText(Html.fromHtml(getItem(position)));
            return row;
        }
    };
    lista.setAdapter(adapter);

This also works and is perphaps a lot simpler. 这也有效,并且简化了很多。 First, pass your data from String[] to Spanned[] 首先,将您的数据从String []传递到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 然后使用CharSequence参数声明ArrayAdapter

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

Borrowed from here 这里借来的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM