Android Display HTML in a ListView

后端 未结 9 2063
情话喂你
情话喂你 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 05:58

    If you have the possibility of loading your texts from strings.xml, adding the tag there will automatically bold your text. 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));

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

    If you are using a SimpleAdapter, here is the code that enables 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;  
            }  
        }  
    ); 
    

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

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

    if you use ksoap for html data from any database engine

    yourVariable=String.valueOf(Html.fromHtml(ic.getProperty(0).toString()))
    
    0 讨论(0)
  • 2020-12-16 06:12

    Ok, so Jitendra Sharma was had the right idea for my scenario, but I needed to override the getView method. 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.

                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);
    
    0 讨论(0)
  • 2020-12-16 06:17

    override getItem method of the Adapter 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));
         }
    };
    
    0 讨论(0)
  • 2020-12-16 06:17
        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);
    
    0 讨论(0)
提交回复
热议问题