English 中文(简体)
On Anders, 我如何在名单上显示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:

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

}

现在的问题是,本清单所载案文有超文本格式。

我怎么能用清单来显示这种超文本格式? 目前,它作为通向各方的便捷文本展示。

最佳回答

假定超文本相当简单,你可以通过这一方法操作:。 http://developer.android.com/vis/android/text/Html.html#fromHtml(java.lang.String 由此得出的可满足条件可发送至《清单》中的案文。 了解Html方法非常缓慢,而且可能放慢滚动速度,你可能希望穿透浮标。

问题回答

定义一个CharSequence ArrayList,包括你数据库的所有内容,这些内容将作为“超文本”列入这一阵列清单。 列入名单各实体的个人意见书,并在名单上显示Charsequence。 我用了以下法典作我的评估:

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

供餐:

<?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"/>

希望这一帮助。

我的问题与你一样。 我正在用json格式阅读卷宗中的数据,在那里,我有id和文字领域的物体。 <代码>text field is html. 我以这种方式解决了问题:

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




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签