Java 为了加载ExpandableListView,我可以在android中将Spanable转换为字符串吗?
我创建了一个数组,然后将其加载到HashMap中,然后在ExpandableListView中使用。但是,我想让部分数据显示为指数的下标和上标。在谷歌搜索了很多次之后,上标和下标的解决方案似乎是使用如下的东西:Java 为了加载ExpandableListView,我可以在android中将Spanable转换为字符串吗?,java,android,arrays,string,spannablestring,Java,Android,Arrays,String,Spannablestring,我创建了一个数组,然后将其加载到HashMap中,然后在ExpandableListView中使用。但是,我想让部分数据显示为指数的下标和上标。在谷歌搜索了很多次之后,上标和下标的解决方案似乎是使用如下的东西: text.setText(Html.fromHtml(getString(R.string.acceleration))) constant_common_data = new ListViewItem[] { new L
text.setText(Html.fromHtml(getString(R.string.acceleration)))
constant_common_data = new ListViewItem[]
{
new ListViewItem(R.drawable.star, "Atomic Mass Constant ", "1.660 539 040 e-27", "kg", "0.000 000 020 e-27"),
new ListViewItem(R.drawable.star, "Avogadro Constant", "6.022 140 857 e23"," mol^-1", "0.000 000 074 x e23"),
new ListViewItem(R.drawable.star, "Boltzmann Constant", "1.380 648 52 e-23", "K^-1", "0.000 000 79 e-23"),
new ListViewItem(R.drawable.star, "Conductance Quantum", "7.748 091 7310 e-5", "s","0.000 000 0018 e-5"),
new ListViewItem(R.drawable.star, "Electric Constant", "8.854 187 817... e-12", "F m^-1", "(exact)"),
new ListViewItem(R.drawable.star, "Electron mass", "9.109 383 56 e-31", "kg", "0.000 000 11 e-31"),
new ListViewItem(R.drawable.star, "Electron volt", "1.602 176 6208 e-19"," J", "0.000 000 0098 e-19"),
new ListViewItem(R.drawable.star, "Elementary charge ", "1.602 176 6208 e-19", "C", "0.000 000 0098 e-19"),
new ListViewItem(R.drawable.star, "Faraday constant ", "96 485.332 89 e-5", "C mol^-1", "0.000 59"),
new ListViewItem(R.drawable.star, "Fine-structure constant ", "7.297 352 5664 e-3", " ", "0.000 000 0017 e-3"),
new ListViewItem(R.drawable.star, "Inverse fine-structure constant", "137.035 999 139", " ", "0.000 000 031"),
new ListViewItem(R.drawable.star, "Magnetic constant", "4pi e-7 = 12.566 370 614... e-7"," N A^-2", "(exact)"),
new ListViewItem(R.drawable.star, "Magnetic Flux Quantum", "2.067 833 831 e-15", "Wb", "0.000 000 013 e-15"),
new ListViewItem(R.drawable.star, "Molar Gas constant", "8.314 4598", "J mol^-1 K^-1", "0.000 0048"),
new ListViewItem(R.drawable.star, "Newtonian constant of gravitation ", "6.674 08 e-11", "m^3 kg^-1 s^-2", "0.000 31 e-11"),
new ListViewItem(R.drawable.star, "Planck constant", "6.626 070 040 e-34", "J s", "0.000 000 081 e-34"),
new ListViewItem(R.drawable.star, "Planck constant over 2 pi", "1.054 571 800 e-34"," J s", "0.000 000 013 e-34"),
new ListViewItem(R.drawable.star, "Proton Mass", "1.672 621 898 e-27", "kg", "0.000 000 021 e-27"),
new ListViewItem(R.drawable.star, "Proton-Electron Mass Ratio", "1836.152 673 89", " ", "0.000 000 17"),
new ListViewItem(R.drawable.star, "Rydberg constant", "10 973 731.568 508", "m^-1", "0.000 065"),
new ListViewItem(R.drawable.star, "Speed of Light in Vacuum", "299 792 458", "m s^-1", "(exact)"),
new ListViewItem(R.drawable.star, "Stefan-Boltzmann constant", "5.670 367 e-8", "Wm^-2 K^-4", "0.000 013 e-8"),
new ListViewItem(R.drawable.star, "Bohr Radius", "0.529 177 210 67 e-10", "m", "0.000 000 000 12 e-10"),
new ListViewItem(R.drawable.star, "Bohr Magneton ", "927.400 9994 e-26"," J T^-1", "0.000 0057 e-26"),
new ListViewItem(R.drawable.star, "Josephson constant", "483 597.8525 e9", "Hz V^-1", "0.0030 e9"),
new ListViewItem(R.drawable.star, "Von Klitzing constant", "25 812.807 4555", "Ohm", "0.000 0059"),
new ListViewItem(R.drawable.star, "Unified Atomic Mass Unit", "1.660 539 040 e-27"+ test , "kg", "0.000 000 020 e-27")
};}
在引用中存储的字符串对象中使用HTML格式化。但是,这是为了使用TextView或按钮,而不是ListView。问题是我现在意识到这实际上创建了一个可扩展对象,我对它知之甚少,但我知道它不是字符串,所以我不能像我希望的那样将它添加到数组中字符串的末尾。
我的数组如下所示:
text.setText(Html.fromHtml(getString(R.string.acceleration)))
constant_common_data = new ListViewItem[]
{
new ListViewItem(R.drawable.star, "Atomic Mass Constant ", "1.660 539 040 e-27", "kg", "0.000 000 020 e-27"),
new ListViewItem(R.drawable.star, "Avogadro Constant", "6.022 140 857 e23"," mol^-1", "0.000 000 074 x e23"),
new ListViewItem(R.drawable.star, "Boltzmann Constant", "1.380 648 52 e-23", "K^-1", "0.000 000 79 e-23"),
new ListViewItem(R.drawable.star, "Conductance Quantum", "7.748 091 7310 e-5", "s","0.000 000 0018 e-5"),
new ListViewItem(R.drawable.star, "Electric Constant", "8.854 187 817... e-12", "F m^-1", "(exact)"),
new ListViewItem(R.drawable.star, "Electron mass", "9.109 383 56 e-31", "kg", "0.000 000 11 e-31"),
new ListViewItem(R.drawable.star, "Electron volt", "1.602 176 6208 e-19"," J", "0.000 000 0098 e-19"),
new ListViewItem(R.drawable.star, "Elementary charge ", "1.602 176 6208 e-19", "C", "0.000 000 0098 e-19"),
new ListViewItem(R.drawable.star, "Faraday constant ", "96 485.332 89 e-5", "C mol^-1", "0.000 59"),
new ListViewItem(R.drawable.star, "Fine-structure constant ", "7.297 352 5664 e-3", " ", "0.000 000 0017 e-3"),
new ListViewItem(R.drawable.star, "Inverse fine-structure constant", "137.035 999 139", " ", "0.000 000 031"),
new ListViewItem(R.drawable.star, "Magnetic constant", "4pi e-7 = 12.566 370 614... e-7"," N A^-2", "(exact)"),
new ListViewItem(R.drawable.star, "Magnetic Flux Quantum", "2.067 833 831 e-15", "Wb", "0.000 000 013 e-15"),
new ListViewItem(R.drawable.star, "Molar Gas constant", "8.314 4598", "J mol^-1 K^-1", "0.000 0048"),
new ListViewItem(R.drawable.star, "Newtonian constant of gravitation ", "6.674 08 e-11", "m^3 kg^-1 s^-2", "0.000 31 e-11"),
new ListViewItem(R.drawable.star, "Planck constant", "6.626 070 040 e-34", "J s", "0.000 000 081 e-34"),
new ListViewItem(R.drawable.star, "Planck constant over 2 pi", "1.054 571 800 e-34"," J s", "0.000 000 013 e-34"),
new ListViewItem(R.drawable.star, "Proton Mass", "1.672 621 898 e-27", "kg", "0.000 000 021 e-27"),
new ListViewItem(R.drawable.star, "Proton-Electron Mass Ratio", "1836.152 673 89", " ", "0.000 000 17"),
new ListViewItem(R.drawable.star, "Rydberg constant", "10 973 731.568 508", "m^-1", "0.000 065"),
new ListViewItem(R.drawable.star, "Speed of Light in Vacuum", "299 792 458", "m s^-1", "(exact)"),
new ListViewItem(R.drawable.star, "Stefan-Boltzmann constant", "5.670 367 e-8", "Wm^-2 K^-4", "0.000 013 e-8"),
new ListViewItem(R.drawable.star, "Bohr Radius", "0.529 177 210 67 e-10", "m", "0.000 000 000 12 e-10"),
new ListViewItem(R.drawable.star, "Bohr Magneton ", "927.400 9994 e-26"," J T^-1", "0.000 0057 e-26"),
new ListViewItem(R.drawable.star, "Josephson constant", "483 597.8525 e9", "Hz V^-1", "0.0030 e9"),
new ListViewItem(R.drawable.star, "Von Klitzing constant", "25 812.807 4555", "Ohm", "0.000 0059"),
new ListViewItem(R.drawable.star, "Unified Atomic Mass Unit", "1.660 539 040 e-27"+ test , "kg", "0.000 000 020 e-27")
};}
您可以看到每个ListViewItem都将字符串作为参数,然后在将所有内容加载到列表中之后,我调用ListViewItem类的toString方法,如下所示:
public String toString() {
return "Value: " + this.value + "\nUnits: " + this.unit + "\nStandard Uncertainty: " + this.uncertainty;
}
显示在ExpandableListView的下拉部分。我希望我在ListViewItem中输入的参数能够像这样打印,但我也希望它们使用sub和super脚本进行格式化
我的问题是,有没有一种方法可以将可扩展对象转换为字符串,这样我就可以将其包含在字符串参数中,并让该对象在其toString()方法中返回它(仍然是样式化的)
谢谢您,我感谢您的帮助。经过大量搜索,我找到了以下解决方案:
public String stripHtml(String html) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
} else {
return Html.fromHtml(html);
}
}
首先,要将Spannable
转换为String
对象,我使用了:
Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
此方法将返回一个Spannable
(当然),在这里您可以使用toString()
方法来生成字符串
:
String yourString = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY).toString();
如果需要将HTML
转换为String
(删除标记),可以使用以下解决方案:
public String stripHtml(String html) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
} else {
return Html.fromHtml(html);
}
}
如果将
ListViewItem
构造函数更改为使用CharSequence
参数而不是String
,则可以使用String
或span
(由.fromHtml()
的结果),因为它们都扩展了CharSequence,您可以使用静态方法到html()Html
类中的
,但它只能处理一些Html
输出,而不能处理任意的事情。
此外,您还可以使用SpannableStringBuilder进行更改:
String photoHeading = mContext.getString(R.string.photo_heading);
SpannableStringBuilder builder = new SpannableStringBuilder(photoHeading);
您还可以有一个ArrayList
来保存Spannable
而不是String
的转换Spannable
您是否使用自定义列表适配器来为列表项定制布局?如果不是,那就是简单的解决方案。您只需定义一个自定义列表项,它允许您在列表项中显示所需的任何特殊图形。你不想使用像ArrayAdapter
这样的东西,因为它只允许一个文本项。如果你已经有了一个自定义BaseAdapter
或类似的东西,这应该相当简单,不需要太多额外的工作。如果您不确定如何实现这一点,请随时发布适配器代码,我会看一看,也许会为您指出正确的方向。问题是如何将Spannable转换为字符串,而不是更改构造函数。我不想使用CharSequence,只使用字符串。但是将其转换为字符串不会保留格式-字符串不会保留跨距