如何从资源中将值加载到数组列表中
问题描述:
最终目标是在不修改代码的情况下向程序中添加其他片段。我想通过更改资源文件的数组值(不是在运行时)来完成此操作。一个资源数组具有微调器值,另一个具有片段文件名称。它们在资源文件中列出的顺序是它们如何关联的:因此,第一个微调器数组 - 字符串与第一个片段TypedArray项目相关,依此类推。如何从资源中将值加载到数组列表中
我写了一个简单的类,以便我可以保持我的微调器值和微调器值代表的片段之间的相关性。这是该类:
import android.support.v4.app.Fragment;
public class SensorList {
int index;
String name;
Fragment sensorFrag;
SensorList(){
name="";
sensorFrag=null;
index=0;
}
}
我想用资源TypedList的值填充此类的实例。那是我遇到问题的地方。这是迄今为止我的代码。最后一行显然是错误的,因为它没有“添加”索引。
public ArrayList<SensorList> sensorList = new ArrayList<SensorList>();
Resources res = getResources();
sensorList.(res.obtainTypedArray(R.array.sensor_frag_names)); //This line is a problem.
我该怎么做呢?是否有我可以申请的演员阵容? 如果不明确,请告诉我。谢谢。
答
Resources resources = getResources();
TypedArray name= resources.obtainTypedArray(R.array.sensor_frag_names);
TypedArray sensorFrag= resources.obtainTypedArray(R.array.sensor_frag_codes);
TypedArray index= resources.obtainTypedArray(R.array.sensor_frag_index);
SensorList slist= new SensorList (name.getString(index), sensorFrag.getFragment(index), index.getInt(index));
name.recycle();
sensorFrag.recycle();
index.recycle();