Java 向列表视图行文本添加html标记
在我的应用程序中,我将创建listview,字符串的正常构造如下所示:Java 向列表视图行文本添加html标记,java,android,Java,Android,在我的应用程序中,我将创建listview,字符串的正常构造如下所示: final ListView listview = (ListView) findViewById(R.id.listview); String[] values = new String[] { "one", "two", "three", "four"}; <string-array name="days"> <item>one</item> <
final ListView listview = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "one", "two", "three",
"four"};
<string-array name="days">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
</string-array>
但我想将html标记添加到每行显示的文本中,因此我必须将文本引用到字符串中,如下所示:
one.setText(Html.fromHtml(getString(R.string.one)));
two.setText(Html.fromHtml(getString(R.string.two)));
three.setText(Html.fromHtml(getString(R.string.three)));
four.setText(Html.fromHtml(getString(R.string.four)));
我的意思是,列表视图行中显示的文本可以创建为:
String[] values = new String[] { "one", "two", "three",
"four"};
或以下简称为“串arry”:
final ListView listview = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "one", "two", "three",
"four"};
<string-array name="days">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
</string-array>
它给出了:java.lang.ClassNotFoundException
日志:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
{com.androidhive.androidlistview/com.androidhive.androidlistview.
AndroidListViewActivity.java}:
java.lang.ClassNotFoundException:
com.androidhive.androidlistview.AndroidListViewActivity.java
in loader dalvik.system.PathClassLoader[/data/app/com.androidhive.androidlistview- 1.apk]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException:
com.androidhive.androidlistview.AndroidListViewActivity.
java in loader dalvik.system.PathClassLoader
[/data/app/com.androidhive.androidlistview-1.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
... 11 more
is there is any way to do it , any help will be appreciated , thanks
重写ArrayAdapter的getView方法,获取与项目在ListView中的位置相关联的TextView,然后根据当前位置设置字符串资源中的文本:
public View getView(int position, View convertView, ViewGroup parent) {
tf=Typeface.createFromAsset(context.getAssets(),"BFantezy.ttf");
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item, null);
}
int resourceToUse = R.string.day1;
switch(position){
case 1:
resourceToUse = R.string.day1;
break;
case 2:
resourceToUse = R.string.day2;
break;
case 3:
resourceToUse = R.string.day3;
break;
case 4:
resourceToUse = R.string.day4;
}
TextView mTextView = (TextView) rowView.findViewById(R.id.label);
mTextView.setTypeface(tf);
mTextView.setText(Html.fromHtml(context.getString(resourceToUse)));
return rowView;
}
你的问题不够清楚。但据我所知,您希望在适配器的
strings.xml
中声明字符串数组。为此,首先需要在strings.xml
中创建一个数组,类似于
<string-array name="numbers">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
</string-array>
您可以在适配器的
getView()
中使用此值数组。我仍然不确定我是否回答了你的疑问。如果您不喜欢,请随时发表评论。tv.setText(Html.fromHtml(getString(R.string.Mystring));你可以试试这个code@Raghunandan我知道如何在string.xml中实现它,但是如何在类中实现它,string[]values=newstring[]{在这里写什么},谢谢这个setText(Html.fromHtml(values[0]);但是字符串没有任何htmltags@androidqq6如果您能进一步解释您的问题,我将帮助您Pragnani post Updated请检查update post,但仍有错误,谢谢我假设它说resourceToUse可能为空,所以在初始声明期间将其设置为0。但是,请确保处理null大小写,这样您就不会将null资源Idi上的文本设置为零。请在我的适配器中将其设置为:int resourceToUse=0;但在AndroidListViewActivity类中,我将其设置为这样的问题:私有静态最终字符串resourceToUse=null;setListAdapter(新的MyArrayAdapter(this,resourceToUse));但是仍然没有显示列表和logcat错误,请去掉这整件事:setListAdapter(新的MyArrayAdapter(这个,resourceToUse));resourceToUse与适配器无关,因此不需要整个额外字符串。尝试声明int-resourceToUse=R.string.day1;因为4可能不是实际的资源编号。你的其他东西有点乱。我将为您重做它…这就是getView()方法的外观(您还应该将字体设置为全局变量,并且只设置一次,而不是每次调用getView()时都重新创建资产以提高效率。究竟是哪一行导致了ClassNotFoundError,以及logcat是什么?
String values[] = getResources().getStringArray(R.array.numbers);