Android 如何创建与字符串数组名关联的动态变量?
有一些字符串数组是微调器的数据源:Android 如何创建与字符串数组名关联的动态变量?,android,Android,有一些字符串数组是微调器的数据源: <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="regions"> <item>Analamanga</item> <item>Diana</item> </string-array> <string-array
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="regions">
<item>Analamanga</item>
<item>Diana</item>
</string-array>
<string-array name="districts_region0"> // the 0 corresponds to the item at position 0
<item>Central</item>
</string-array>
<string-array name="districts_region1"> // the 1 corresponds to the item at position 1
<item>Nosy-be</item>
<item>Sambava</item>
</string-array>
</resources>
但是这个崩溃了!那么如何动态设置spinnerDistrict的适配器呢?尝试使用以下帮助器方法:
public static int getResIdFromString(Context context, String path){
int resID = context.getResources().getIdentifier(path, "drawable", context.getPackageName());
return resID;
}
int rid = Utils.getResIdFromString(context, "icon" + i.toLowerCase());
private class ExampleAdapter extends ArrayAdapter<String> {
private final int[] strings = new int[] {
R.id.string1,
R.id.string2,
R.id.string3,
};
private ExampleAdapter(Context context, int resource) {
super(context, resource);
}
private ExampleAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
public ExampleAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
}
private ExampleAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
private ExampleAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
}
private ExampleAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int stringResId = getStringForPosition(position);
TextView tvText = view.findViewById(R.id.tvText);
tvText.setText(stringResId);
return view;
}
private int getStringForPosition(int position) {
return strings[position % strings.length];
}
}
你可以这样使用它:
public static int getResIdFromString(Context context, String path){
int resID = context.getResources().getIdentifier(path, "drawable", context.getPackageName());
return resID;
}
int rid = Utils.getResIdFromString(context, "icon" + i.toLowerCase());
private class ExampleAdapter extends ArrayAdapter<String> {
private final int[] strings = new int[] {
R.id.string1,
R.id.string2,
R.id.string3,
};
private ExampleAdapter(Context context, int resource) {
super(context, resource);
}
private ExampleAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
public ExampleAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
}
private ExampleAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
private ExampleAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
}
private ExampleAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int stringResId = getStringForPosition(position);
TextView tvText = view.findViewById(R.id.tvText);
tvText.setText(stringResId);
return view;
}
private int getStringForPosition(int position) {
return strings[position % strings.length];
}
}
您可以像这样使用自定义
适配器
:
public static int getResIdFromString(Context context, String path){
int resID = context.getResources().getIdentifier(path, "drawable", context.getPackageName());
return resID;
}
int rid = Utils.getResIdFromString(context, "icon" + i.toLowerCase());
private class ExampleAdapter extends ArrayAdapter<String> {
private final int[] strings = new int[] {
R.id.string1,
R.id.string2,
R.id.string3,
};
private ExampleAdapter(Context context, int resource) {
super(context, resource);
}
private ExampleAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
public ExampleAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
}
private ExampleAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
private ExampleAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
}
private ExampleAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int stringResId = getStringForPosition(position);
TextView tvText = view.findViewById(R.id.tvText);
tvText.setText(stringResId);
return view;
}
private int getStringForPosition(int position) {
return strings[position % strings.length];
}
}
私有类ExampleAdapter扩展了ArrayAdapter{
私有最终整数[]字符串=新整数[]{
R.id.1,
R.id.2,
R.id.3,
};
私有ExampleAdapter(上下文,int资源){
超级(上下文、资源);
}
私有ExampleAdapter(上下文上下文、int资源、int textViewResourceId){
超级(上下文、资源、textViewResourceId);
}
公共示例适配器(上下文、int资源、字符串[]对象){
超级(上下文、资源、对象);
}
私有ExampleAdapter(上下文上下文、int资源、int textViewResourceId、字符串[]对象){
超级(上下文、资源、textViewResourceId、对象);
}
私有ExampleAdapter(上下文、int资源、列表对象){
超级(上下文、资源、对象);
}
私有ExampleAdapter(上下文上下文、int资源、int textViewResourceId、列表对象){
超级(上下文、资源、textViewResourceId、对象);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=super.getView(位置、转换视图、父级);
int stringResId=getStringForPosition(位置);
TextView tvText=view.findViewById(R.id.tvText);
tvText.setText(stringResId);
返回视图;
}
私有整型getStringForPosition(整型位置){
返回字符串[位置%strings.length];
}
}
此
ExampleAdapter
将循环遍历顶部定义的字符串,并将它们分配给列表项中的TextView
——在本例中为R.id.tvText
。您需要找到字符串数组的资源id,如下所示
String tag = "districts_region"+pos;
int resourceId = getResources().getIdentifier(tag, "array", getPackageName());
然后呢,
ArrayAdapter<CharSequence> districtAdapter = ArrayAdapter.createFromResource(this,
resourceId, android.R.layout.simple_spinner_item);
ArrayAdapter districtAdapter=ArrayAdapter.createFromResource(此,
resourceId,android.R.layout.simple\u微调器\u项);
请发布您的logcat输出。您可以创建自定义适配器来执行此操作。logcat为空,应用程序在打开活动之前崩溃!