【问题标题】:How to remove html tags from listview?如何从列表视图中删除 html 标签?
【发布时间】:2015-02-19 04:50:16
【问题描述】:

我正在使用 json 解析,我正在从服务器获取响应,在我的响应中它包含一些 html 标签,它显示在我的 listview 中。我在我的 listview 中使用 imageview 和 textview 。如何删除 html 标签?

回复

[  
{  
 "title_tag":"business",  
"description":"<p>\r\n\t<strong>Lorem Ipsum<\/strong> is simply dummy   text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<\/p>\r\n",  
  "image":"1.jpg"    

    }  
    ]

我的适配器

public class CustomAdapterAdvertisement extends BaseAdapter{

private Context context;
private ArrayList<HashMap<String,String>> listData;
private AQuery aQuery;

private static final String ADD_NAME="title_tag";
private static final String ADD_DESC="description";
private static final String ADD_IMAGE="image";

public CustomAdapterAdvertisement(Context context,ArrayList<HashMap<String,String>> listData) {
    this.context = context;
    this.listData=listData;
    aQuery = new AQuery(this.context);
}

@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public String stripHtml(String html) {
    return Html.fromHtml(html).toString();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.advertisement_detail, null);
        holder.propic = (ImageView) convertView.findViewById(R.id.advertisement_img);
        holder.txtproname = (TextView) convertView.findViewById(R.id.txtnameadvertisement);
       // holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidsearch);
        holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txtadvertisementdescription);


        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtproname.setText(listData.get(position).get(ADD_NAME));
    //holder.txtproid.setText(listData.get(position).get(TAG_PROFILE));
    holder.txtprofilecast.setText(listData.get(position).get(ADD_DESC));
   holder.txtprofilecast.setText(Html.fromHtml("description").toString());

    aQuery.id(holder.propic).image(listData.get(position).get(ADD_IMAGE),true,true,0,R.drawable.ic_launcher);

    // image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image


    return convertView;
}
class ViewHolder{
    ImageView propic;
    TextView txtproname;
   // TextView txtproid;
    TextView txtprofilecast;

}

}

【问题讨论】:

标签: android html json listview


【解决方案1】:

如何从列表视图中删除html标签?

在提供的description 关键文本中,Html.fromHtml 支持所有 html 标签。

要在 TextView 中显示 html 格式的文本,请使用 Html.fromHtml:

String strDes=Html.fromHtml(description).toString();

【讨论】:

  • 我正在使用基本适配器我的列表项..我的活动文件和基本适配器文件是分开的..我在哪里可以使用你的代码?
  • @fazilpuriasa :在适配器 textView 中,您当前是 description 值。将其添加为textview_in_getview_method.setText(Html.fromHtml(description).toString())
  • 我的适配器文件是这样的..stackoverflow.com/questions/28576379/…
  • @fazilpuriasa:在business_card.xml 布局中,您要在哪个TextView 中显示description 字符串?
  • 名片是不同的模块..我已经完成了那个..我分享链接,这样你就可以了解我的基本适配器类..正如你所说,我添加了这一行 holder.txtprofilecast.setText (Html.fromHtml("description").toString());...但是所有文字都消失了
【解决方案2】:

请找到以下解决方案

您可以尝试以下方法,在您的包中创建类并将其命名为“Util”

public class Util{
public static String stripHtml(String html) 
{
  //html = "<p sapn = 'div'>hhhhhhhhh<p> Hello World </p></span>";
  while(html.contains("<"))
    html = html.replace(html.substring(html.indexOf("<"),html.indexOf(">")+1),"");
  return html;//will return hhhhhhhh Hello World
}
}

此方法将搜索以“”结尾的任何文本并将其替换。

那么你可以在任何你想要的地方使用该方法

String myNewStringWithoutHTML = Util.stripHtml(STRING);

希望以上解决方案对您有所帮助。

如果需要我这边的更多帮助,请告诉我。

【讨论】:

  • 欢迎您,如果您需要更多帮助,请告诉我。
【解决方案3】:
var book = {  
 "title_tag":"business",  
"description":"<p>\r\n\t<strong>Lorem Ipsum<\/strong> is simply dummy   text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<\/p>\r\n",  
  "image":"1.jpg"    

    } 

$(function(){
    $('body').append("<div class='text'>"+book.description+"</div>");
    alert($('.text').text());
    $('.text').hide();
})

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    相关资源
    最近更新 更多