私のアプリでは 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"};
または、次のように string-arry を参照します。
<string-array name="days">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
</string-array>
しかし、これは私が望むものではありません.htmlタグによってカスタマイズされる文字列.xmlからテキストを取得したいのですが、代わりにクラスに何を書くべきかわかりません。
アップデート:
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
ログキャット:
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