[英]How to display HTML in ListView (SimpleArapter / setViewBinder)
How to display HTML text in listView.如何在 listView 中显示 HTML 文本。 I do not understand... listView works without errors (displays text and numbers), text with HTML markup is displayed as - string.我不明白... listView 正常工作(显示文本和数字),带有 HTML 标记的文本显示为 - 字符串。
I want to display a math or chemical folmule:我想显示一个数学或化学公式:
X <sup><small>n</small> </sup> + Y <sup>
Android Display HTML in a ListView - answers from here do not work for me. Android 在 ListView 中显示 HTML - 这里的答案对我不起作用。
Maybe I am not defining the array correctly?也许我没有正确定义数组?
Where is my mistake?我的错误在哪里? (I am newbie) (我是新手)
Below is my code:下面是我的代码:
<string-array name="catNames">
<item><b>Name 1</b></item>
<item><big>Name 1<small>Name 1</small>Name 1<small>Name 1</small></big></item>
<item><p><b>Lorem ipsum dolor sit amet</b></p></item>
</string-array>
//
ListView listView = (ListView) findViewById(R.id.listView);
String[] catNames = getResources().getStringArray(R.array.catNames);
String[] catNames2 = getResources().getStringArray(R.array.catNames2);
String[] catNames3 = getResources().getStringArray(R.array.catNames3);
int[] catNames4 = getResources().getIntArray(R.array.catNames4);
//
ArrayList<HashMap<String, Object>> data = new ArrayList<>(catNames.length);
HashMap<String, Object> map;
//
for (int i = 0; i < catNames3.length; i++) {
map = new HashMap<>();
map.put("CatName", catNames[i]);
map.put("CatName2", catNames2[i]);
map.put("CatName3", catNames3[i]);
map.put("CatName4", catNames4[i]);
data.add(map);
}
//
String[] from = {"CatName", "CatName2", "CatName3", "CatName4"};
int[] to = {R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4,};
//
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_item, from, to);
adapter.setViewBinder(new TableActivity.MyViewBinder());
listView.setAdapter(adapter);
/////////////////////////////////////////////////////
class MyViewBinder implements SimpleAdapter.ViewBinder {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
int red = getResources().getColor(R.color.colorPrimary);
//
int i = 0;
switch (view.getId()) {
// LinearLayout
case R.id.textView3:
view.setBackgroundColor(red);
//((TextView)view).setText(Html.fromHtml(data + " " + textRepresentation));
((TextView)view).setText(Html.fromHtml(textRepresentation));
return true;
}
return false;
}
}
How to display HTML text in listView.如何在 listView 中显示 HTML 文本。 I do not understand... listView works without errors (displays text and numbers), text with HTML markup is displayed as - string.我不明白... listView 正常工作(显示文本和数字),带有 HTML 标记的文本显示为 - 字符串。
I want to display a math or chemical folmule:我想显示一个数学或化学公式:
X <sup><small>n</small> </sup> + Y <sup>
Android Display HTML in a ListView - answers from here do not work for me. Android 在 ListView 中显示 HTML - 这里的答案对我不起作用。
Maybe I am not defining the array correctly?也许我没有正确定义数组?
Where is my mistake?我的错误在哪里? (I am newbie) (我是新手)
Below is my code:下面是我的代码:
<string-array name="catNames">
<item><b>Name 1</b></item>
<item><big>Name 1<small>Name 1</small>Name 1<small>Name 1</small></big></item>
<item><p><b>Lorem ipsum dolor sit amet</b></p></item>
</string-array>
//
ListView listView = (ListView) findViewById(R.id.listView);
String[] catNames = getResources().getStringArray(R.array.catNames);
String[] catNames2 = getResources().getStringArray(R.array.catNames2);
String[] catNames3 = getResources().getStringArray(R.array.catNames3);
int[] catNames4 = getResources().getIntArray(R.array.catNames4);
//
ArrayList<HashMap<String, Object>> data = new ArrayList<>(catNames.length);
HashMap<String, Object> map;
//
for (int i = 0; i < catNames3.length; i++) {
map = new HashMap<>();
map.put("CatName", catNames[i]);
map.put("CatName2", catNames2[i]);
map.put("CatName3", catNames3[i]);
map.put("CatName4", catNames4[i]);
data.add(map);
}
//
String[] from = {"CatName", "CatName2", "CatName3", "CatName4"};
int[] to = {R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4,};
//
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_item, from, to);
adapter.setViewBinder(new TableActivity.MyViewBinder());
listView.setAdapter(adapter);
/////////////////////////////////////////////////////
class MyViewBinder implements SimpleAdapter.ViewBinder {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
int red = getResources().getColor(R.color.colorPrimary);
//
int i = 0;
switch (view.getId()) {
// LinearLayout
case R.id.textView3:
view.setBackgroundColor(red);
//((TextView)view).setText(Html.fromHtml(data + " " + textRepresentation));
((TextView)view).setText(Html.fromHtml(textRepresentation));
return true;
}
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.