gpt4 book ai didi

android - 为不同的 TextView 使用多个字符串资源

转载 作者:行者123 更新时间:2023-11-30 04:40:10 25 4
gpt4 key购买 nike

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

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

界面在rowLayout.xml中有如下 TextView ,

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

但是当我只能提供 int[] 时,由于 createFromResource 需要 int 参数,我遇到了错误。

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

干杯

最佳答案

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

您可以在 ListActivity 中将适配器创建为私有(private)类。尝试这样的事情(警告:代码未经测试):

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

关于android - 为不同的 TextView 使用多个字符串资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6126945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com