【问题标题】:Using multiple string resources for different textviews为不同的文本视图使用多个字符串资源
【发布时间】:2011-05-25 15:25:55
【问题描述】:

嘿,我目前正在制作一个小型 Android 应用程序时遇到问题。我正在尝试构建的是:

  • 列表界面
    • 每行有两个 TextView(一个标题和一个标题)。
    • 从两个单独的字符串数组资源中提取这些值。

界面在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[] 时出现错误。

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

干杯

【问题讨论】:

    标签: android android-layout android-listview textview


    【解决方案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;
            }
        }
    }
    

    【讨论】:

    • 您能解释一下&lt;Track&gt;tracks 的用途吗?
    • 对不起,与 Track 相关的代码是我这边的复制粘贴错误。我现在已经删除了。你测试过代码吗?它解决了你的问题吗?
    • 试一试,启动时出现强制关闭错误。后来运行了一些调试,我设法让它工作得很好:)。 Eclipse 抱怨一些必须删除才能运行的覆盖。现在稍微研究一下代码,让我了解显示这些项目所要做的事情。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 2019-07-13
    • 2014-06-07
    • 2020-09-23
    • 2016-03-14
    • 1970-01-01
    相关资源
    最近更新 更多