0

在我的应用程序中,我将创建 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-array name="days">
    <item>one</item>
    <item>two</item>
    <item>three</item>
    <item>four</item>
 </string-array>

但这不是我想要的,我想从字符串 .xml 中检索文本,该文本将由 html 标签自定义,但我不知道在课堂上写什么。

更新:

正如下面回答的那样,我是这样做的:

我的阵列适配器:

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));  
                      }    
         }

它给了: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
4

2 回答 2

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;  
} 
于 2013-05-18T18:43:16.953 回答
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()您可以在适配器中使用此值数组。我仍然不确定我是否回答了你的疑问。如果不放心发表评论。

于 2013-05-18T18:45:32.447 回答