【发布时间】:2016-08-10 09:01:09
【问题描述】:
所以我有以下问题: 我想使用字符串数组作为 TabLayout 的标题。
在这种情况下,我使用 ViewPagerAdapter 进行布局,现在我想将选项卡的标题保存在字符串数组中。
为了提供这些标题的翻译,我使用了 2 个资源字符串,但我不能使用它们,因为字符串数组只能使用字符串,而资源字符串是整数(完美句子)。
不兼容的类型。
必需:java.lang.String
找到:int
public class ViewPagerAdapter extends FragmentPagerAdapter {
String[] tabTitleArray = {
R.string.dice, R.string.presets
};
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return null;
}
@Override
public int getCount() {
return 0;
}
}
错误在这里:
String[] tabTitleArray = {R.string.dice, R.string.presets};
另外:我不能使用getResource().getString();
【问题讨论】:
-
为什么不将这些值转换为字符串而不是资源?
-
如果你不能使用 getResources(),然后做一件事,用 FragmentManager 在你的适配器中传递你的字符串数组,并使用 getResources() 声明字符串数组
-
最终 Stringlabels = getApplicationContext().getResources().getString(R.String.labels);
标签: java android arrays string