简体   繁体   English

在Android上,如何在ListView中显示html?

[英]On Android, How do I display html in ListView?

I am feeding a ListView from a database in this way (nothing special), except COL_TXT_TRANSL2 contains html formatting: 我以这种方式(没什么特别的)从数据库中馈送ListView,除了COL_TXT_TRANSL2包含html格式:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    mCurrBookID = extras.getString("BookID");
    mCurrChapterNum = extras.getString("ChapterNum");
    mCurrChapterTitle = extras.getString("ChapterTitle");
    mGitaDB= Central.mDB;

    this.setTitle(mCurrChapterNum+"."+mCurrChapterTitle);
    setContentView(R.layout.chapterdisplay);

    //set chapter intro
    TextView tvIntro=(TextView) findViewById(R.id.textIntro);
    tvIntro.setText(Html.fromHtml(extras.getString("ChapterIntro")));


    try {
        String[] columns = new String[] { mGitaDB.COL_TXT_TEXT_NUM, mGitaDB.COL_TXT_TRANSL2 };
        int[] to = new int[] { R.id.number_entry, R.id.title_entry };

        mCursor=mGitaDB.GetGitaTexts(mCurrBookID, mCurrChapterNum);
        mAdapter = new SimpleCursorAdapter(this,
                R.layout.textslist_row, mCursor, columns, to);

        setListAdapter(mAdapter);
    }
    catch (Exception e) {
         String err="Error: " + e.getMessage();
         Toast toast = Toast.makeText(Central.context, err, 15000);

         toast.show();
    }

}

Now the problem is that the text displayed in this ListView has HTML formatting. 现在的问题是,此ListView中显示的文本具有HTML格式。

How can I make listview display this HTML formatting? 如何使Listview显示此HTML格式? Currently it is displayed as a plain text with all tags. 当前,它显示为带有所有标签的纯文本。

My problem was similar to yours. 我的问题和你的相似。 I was reading data from file in json format, where I have objects with id and text fields. 我从json格式的文件中读取数据,其中有带有id和text字段的对象。 text field is html. text字段是html。 I have resolved problem this way: 我已经通过这种方式解决了问题:

ArrayList<MyObject> myObjectsList = new ArrayList<MyObject>();
ArrayList<HashMap<String, CharSequence>> tableElements = new ArrayList<HashMap<String, CharSequence>>();

String keyword = in.getStringExtra(TAG_KEYWORD);

InputStream is = this.getResources().openRawResource(R.raw.data);

JSONParser jParser = new JSONParser();

myObjectsList = jParser.searchForObjects(is, keyword);


for (MyObject element : myObjectsList) 
{

    String id = Integer.toString(element.id);
    CharSequence text = Html.fromHtml(element.text);

    // creating new HashMap
    HashMap<String, CharSequence> map = new HashMap<String, CharSequence>();

    // adding each child node to HashMap key => value
    map.put(TAG_ID, id);
    map.put(TAG_TEXT, text);

    // adding HashList to ArrayList
    tableElements.add(map);
}



ListAdapter adapter = new SimpleAdapter(this,tableElements,
    R.layout.search_item,
    new String[] { TAG_ID, TAG_TEXT.toString()}, new int[] {
    R.id.exercise_id, R.id.text });

setListAdapter(adapter);

Assuming the HTML is fairly simple you can run it through this method: http://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String ) The resulting Spannable can be sent to a TextView in the ListView. 假设HTML非常简单,您可以通过以下方法运行它: http : //developer.android.com/reference/android/text/Html.html#fromHtml (java.lang.String)可以将生成的Spannable发送到ListView中的TextView。 Beware the fromHtml method is very slow and may slow down scrolling, you might want to cache the Spannables. 注意fromHtml方法非常慢并且可能会减慢滚动速度,因此您可能希望缓存Spannables。

Define a CharSequence ArrayList, include all the elements from your database to be displayed in this arraylist as HTML. 定义一个CharSequence ArrayList,包括数据库中要以HTML形式在此数组列表中显示的所有元素。 Include a personal TextView layout for the individual entities of the listView, and display the Charsequence in the list. 为listView的各个实体包括个人TextView布局,并在列表中显示Charsequence。 I had made use of the following code for my app: 我为我的应用程序使用了以下代码:

List<CharSequence> styledItems = new ArrayList<CharSequence>();

droidDB.open();
articles = droidDB.getAllArticleTitles(feed.feedId);
droidDB.close();

for (Article article : articles) {
    styledItems.add(Html.fromHtml(article.title));
}

ArrayAdapter<CharSequence> notes = 
    new ArrayAdapter<CharSequence>(this, R.layout.feeds_row,styledItems);
setListAdapter(notes);

For the feeds_row.xml: 对于feeds_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:textColor="#FFFFFF"/>

Hope this helps. 希望这可以帮助。

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

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