在Android上,如何在ListView中显示html?
我以这种方式从数据库中输入ListView(没有什么特别的),除了 COL_TXT_TRANSL2包含html格式:在Android上,如何在ListView中显示html?,android,html,listview,Android,Html,Listview,我以这种方式从数据库中输入ListView(没有什么特别的),除了 COL_TXT_TRANSL2包含html格式: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle extras = getIntent().getExtras(); mCurrBookID = extras.getString("BookID"); mCurr
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();
}
}
现在的问题是,此ListView中显示的文本具有HTML格式
如何使listview显示此HTML格式?当前,它显示为包含所有标记的纯文本。假设HTML相当简单,您可以通过以下方法运行它:)生成的Spannable可以发送到ListView中的TextView。注意fromHtml方法非常慢,可能会减慢滚动速度,您可能需要缓存可扩展项。定义一个CharSequence ArrayList,将数据库中的所有元素作为HTML显示在此ArrayList中。为listView的各个实体包括个人TextView布局,并在列表中显示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);
List styledItems=new ArrayList();
droidDB.open();
articles=droidDB.getAllArticleTitles(feed.feedId);
droidDB.close();
用于(第条:第条){
add(Html.fromHtml(article.title));
}
ArrayAdapter注释=
新的ArrayAdapter(此,R.layout.feeds_行,styledItems);
setListAdapter(注释);
对于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"/>
希望这能有所帮助。我的问题与你的类似。我从json格式的文件中读取数据,其中有id和文本字段的对象<代码>文本字段为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);
ArrayList myObjectsList=new ArrayList();
ArrayList tableElements=新的ArrayList();
String关键字=in.getStringExtra(TAG_关键字);
InputStream is=this.getResources().openRawResource(R.raw.data);
JSONParser jParser=新的JSONParser();
myObjectsList=jParser.searchForObjects(is,关键字);
用于(MyObject元素:MyObject列表)
{
字符串id=Integer.toString(element.id);
CharSequence text=Html.fromHtml(element.text);
//创建新的HashMap
HashMap=newHashMap();
//将每个子节点添加到HashMap key=>value
地图放置(标签标识,标识);
map.put(标签文本,文本);
//将哈希列表添加到ArrayList
tableElements.add(map);
}
ListAdapter=新的SimpleAdapter(此,tableElements,
R.layout.search_项,
新字符串[]{TAG_ID,TAG_TEXT.toString()},新int[]{
R.id.exercise_id,R.id.text});
setListAdapter(适配器);
从这一行开始,您想以html格式显示吗?