在我的应用程序中,我将创建listview,字符串的正常构造将如下:
final ListView listview = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "one", "two", "three",
"four"};
但是我想在每行上添加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如下:
<string-array name="days">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
</string-array>
但这不是我想要的,我想从字符串.xml中检索文本,这将由html标签自定义,但我不知道在课堂上写什么。
更新
AS回答如下:我这样做了:
MyArrayAdapter:
public class MyArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
Typeface tf;
static class ViewHolder {
public TextView text;
}
public MyArrayAdapter(Activity context, String string) {
super(context, R.layout.list_item);
this.context = context;}
@Override
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;}}
AndroidListViewActivity:
public class AndroidListViewActivity extends ListActivity {
private String resourceToUse;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new MyArrayAdapter(this,resourceToUse));
}
}
IT GAVE:java.lang.ClassNotFoundException
logcat的:
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
答案 0 :(得分:1)
覆盖ArrayAdapter的getView方法,获取与ListView中项目位置相关联的TextView,然后根据当前位置设置String资源中的文本:
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;
}
答案 1 :(得分:0)
你的问题不够明确。但据我所知,您希望在适配器的strings.xml
中声明字符串数组。要首先执行此操作,您需要在strings.xml
中创建一个类似于
<string-array name="numbers">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
</string-array>
然后您可以在代码中获取字符串数组
String values[] = getResources().getStringArray(R.array.numbers);
您可以在适配器的getView()
中使用此值数组。我仍然不确定我是否回答了你的疑问。如果没有随意评论。