1

嘿,我目前遇到了我正在制作的一个小型Android应用程序的问题。我正在试图打造的是:为不同文字视图使用多个字符串资源

    • 有两个TextViews(标题和字幕)每行列表界面。
    • 将这些值从两个单独的字符串数组资源中抽取出来。

该接口具有在rowLayout.xml以下文本的意见,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:orientation="vertical"> 

<TextView android:text="@+id/rowTitle" 
      android:layout_height="wrap_content" 
      android:id="@+id/title" 
      android:textSize="25px" 
      android:layout_width="match_parent"> 
</TextView> 

<TextView android:text="@+id/rowCaption" 
      android:layout_height="wrap_content" 
      android:id="@+id/caption" 
      android:textSize="25px" 
      android:layout_width="match_parent"> 
</TextView> 

和字符串资源,

<string-array name="menuEntryTitles"> 
    <item>Start</item> 
    <item>Stop</item> 
</string-array> 

<string-array name="menuEntryCaptions"> 
    <item>Starts the update listener service.</item> 
    <item>Stops the update listener service.</item> 
</string-array> 

应该有两行标题开始和停止,每个都有相关的标题。我已经使用ArrayAdapter实施这一企图,

ArrayAdapter listAdapter = ArrayAdapter.createFromResource(this, 
      new int[] {R.array.menuEntryTitles, R.array.menuEntryCaptions}, 
      new int[] {R.id.rowTitle, R.id.rowCaption}); 

,但我得到的错误是由于createFromResource需要int争论时,我只能提供int[]

希望这里有人能指点我正确的方向。

干杯

回答

1

一种解决方案是创建自己的适配器,因为ArrayAdapter只使用一个阵列,而不是两个。

您可以创建适配器作为ListActivity中的私有类。尝试类似的东西(警告:代码未经测试):

public class MyListActivity extends ListActivity { 
    protected void onCreate(){ 

     .... 

     // Get arrays from resources 
     Resources r = getResources();  
     String [] arr1 = r.getStringArray("menuEntryTitles"); 
     String [] arr1 = r.getStringArray("menuEntryCaptions"); 

     // Create your adapter 
     MyAdapter adapter = new MyAdapter(arr1, arr2); 

     setListAdapter(adapter); 
    } 

    private class MyAdapter extends BaseAdapter { 
     private LayoutInflater inflater; 

     String [] arr1; 
     String [] arr2; 

     public MyAdapter(String[] arr1, String[] arr2){ 
      inflater = (LayoutInflater)MyListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      this.arr1 = arr1; 
      this.arr2 = arr2; 
     } 

     @Override 
     public int getCount() { 
      return arr1.length; 
     } 

     @Override 
     public Object getItem(int position) { 
      return null; 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     // Used to keep references to your views, optimizes scrolling in list 
     private static class ViewHolder { 
      TextView tv1; 
      TextView tv2; 
     } 


     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      ViewHolder holder; 
      if (convertView == null){ 
       convertView = inflater.inflate(R.layout.row_layout, null); 

       // Creates a ViewHolder and store references to the two children views 
       // we want to bind data to. 
       holder = new ViewHolder(); 
       holder.tv1 = (TextView) convertView.findViewById(R.id.rowTitle); 
       holder.tv2 = (TextView) convertView.findViewById(R.id.rowCaption); 

       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder)convertView.getTag(); 
      } 

      holder.tv1.setText(arr1[position]); 
      holder.tv2.setText(arr2[position]); 

      return convertView; 
     } 
    } 
} 
+0

你能解释''和'tracks'用于什么吗? – 2011-05-25 16:36:26

+0

对不起,但跟踪相关的代码是来自我身边的复制粘贴错误。我现在已经删除它。你测试过代码吗?它解决了你的问题吗? – jorgenfb 2011-05-26 06:18:46

+0

在启动时得到一个强制关闭错误。几个调试运行后,我设法让它工作得很好:)。 Eclipse抱怨了一些必须删除才能运行的重写。现在来研究一下这些代码,以便让我的头了解正在做什么来显示这些项目。非常感谢你。 – 2011-05-26 08:12:32