而是多值项,我wrote about another方法,您可以存储您的复杂的对象作为一个数组,然后用后缀名一个增量整数。循环遍历它们,并根据需要从那里创建一个强类型对象列表。
1
Food
2
Health
3
Garden
然后您可以创建一个静态方法来检索它们:
public class ResourceHelper {
public static List getMultiTypedArray(Context context, String key) {
List array = new ArrayList<>();
try {
Class res = R.array.class;
Field field;
int counter = 0;
do {
field = res.getField(key + "_" + counter);
array.add(context.getResources().obtainTypedArray(field.getInt(null)));
counter++;
} while (field != null);
} catch (Exception e) {
e.printStackTrace();
} finally {
return array;
}
}
}
,它可以像现在这样消耗:
for (TypedArray item : ResourceHelper.getMultiTypedArray(this, "categories")) {
Category category = new Category();
category.ID = item.getInt(0, 0);
category.title = item.getString(1);
mCategories.add(category);
}