Java 如何在android中删除类似的html标记

Java 如何在android中删除类似的html标记,java,android,json,Java,Android,Json,我在我的android应用程序中使用json,实际上在列表视图中,它在我的文本中显示为html标记,我怎么能只显示文本而不显示html标记呢 Mainactivity.java public class MainActivity extends ListActivity implements FetchDataListener { private ProgressDialog dialog; @Override protected void onCreate(Bund

我在我的android应用程序中使用json,实际上在列表视图中,它在我的文本中显示为html标记,我怎么能只显示文本而不显示html标记呢

Mainactivity.java

public class MainActivity extends ListActivity implements FetchDataListener
{
    private ProgressDialog dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_list_item);   
        initView();
    }



    private void initView()
    {
        // show progress dialog
        dialog = ProgressDialog.show(this, "", "Loading...");
        String url = "http://floating-wildwood-1154.herokuapp.com/posts.json";
        FetchDataTask task = new FetchDataTask(this);
        task.execute(url);
    }

    @Override
    public void onFetchComplete(List<Application> data)
    {
        // dismiss the progress dialog
        if ( dialog != null )
            dialog.dismiss();
        // create new adapter
        ApplicationAdapter adapter = new ApplicationAdapter(this, data);
        // set the adapter to list
        setListAdapter(adapter);
    }

    @Override
    public void onFetchFailure(String msg)
    {
        // dismiss the progress dialog
        if ( dialog != null )
            dialog.dismiss();
        // show failure message
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }
}
Applicationadapter.java

public class ApplicationAdapter extends ArrayAdapter<Application>
{
    private List<Application> items;

    public ApplicationAdapter(Context context, List<Application> items)
    {
        super(context, R.layout.app_custom_list, items);
        this.items = items;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = convertView;
        if ( v == null )
        {
            LayoutInflater li = LayoutInflater.from(getContext());
            v = li.inflate(R.layout.app_custom_list, null);
        }
        Application app = items.get(position);
        if ( app != null )
        {
            TextView titleText = (TextView) v.findViewById(R.id.titleTxt);
            if ( titleText != null )
                titleText.setText(app.getContent());
        }
        return v;
    }
}
在我的listview中,它的显示方式如下

你好

frnds

古德明

它用html标记显示,我只需要文本数据,文本必须显示在我的listview中。

用于在TextView中显示带有html标记的文本。示例

字符串str_不带_html=html.fromHtmlback to work.toString

用于在TextView中显示带有html标记的文本。示例

字符串str_不带_html=html.fromHtmlback to work.toString

只需删除标签即可

只要去掉标签

你可以用

String s = value.replaceAll("<br>","");
你可以用

String s = value.replaceAll("<br>","");

解决方案通常需要regex(这是一种容易出错的方法)或安装第三方库,如jsoup或jericho。Android设备上更好的解决方案就是使用Html.fromHtml函数:

代码


它使用Android内置的Html解析器来构建输入Html的跨域表示,而不需要任何Html标记。然后通过将输出转换回字符串来剥离Span标记。

解决方案通常需要正则表达式(这是一种容易出错的方法)或安装第三方库,如jsoup或jericho。Android设备上更好的解决方案就是使用Html.fromHtml函数:

代码


它使用Android内置的Html解析器来构建输入Html的跨域表示,而不需要任何Html标记。然后通过将输出转换回字符串来剥离Span标记。

str=str.replace,;str=str.replace,;你能解释清楚我要添加的行是什么吗?我要添加的行在哪里@prosper@IBetterCommunity:查看我的编辑answer@IBetterCommunity:在将文本设置为文本视图或任何视图以删除所有Html之前,您需要使用Html.fromHtmlyour\u string\u here\u其中包含\u Html\u标记。toStringtags@IBetterCommunity:好的,只需更改titleText.setTextapp.getContent;titleText.setTextHtml.fromHtmlapp.getContent.toString的行;在ApplicationAdapter类中。这将在传递给setText方法之前删除字符串中的所有HTML标记。您应该接受@ρцσѕρєK的答案,而不是给出您自己的答案……您能清楚地解释一下我要添加的行吗,在这里我必须添加行@prosper@IBetterCommunity:查看我的编辑answer@IBetterCommunity:在将文本设置为文本视图或任何视图以删除所有Html之前,您需要使用Html.fromHtmlyour\u string\u here\u其中包含\u Html\u标记。toStringtags@IBetterCommunity:好的,只需更改titleText.setTextapp.getContent;titleText.setTextHtml.fromHtmlapp.getContent.toString的行;在ApplicationAdapter类中。这将在传递到setText方法之前删除字符串中的所有HTML标记。您应该接受@ρцσѕρєK的答案,而不是给出您自己的答案。在我必须使用此代码的地方,它是我的主要活动。我是否正确?是否需要任何其他代码?您可以在任何地方使用此函数。此函数将返回字符串。请浏览此URL。@Karthik为什么不:D@PankajKumar答案将对某人有所帮助…在我必须使用此代码的地方,它在我的主要活动中,我是否正确?是否需要任何其他代码或它是否足够?您可以在任何地方使用此功能。此函数将返回字符串。请浏览此URL。@Karthik为什么不:D@PankajKumar答案会对某人有所帮助。。。
public String stripHtml(String html) {
    return Html.fromHtml(html).toString();
}