最终目标是在不修改代码的情况下向程序中添加其他片段。我想通过更改资源文件的数组值来做到这一点(而不是在运行时)。一个资源数组具有微调器值,另一个资源资源具有片段文件名。它们在资源文件中列出的顺序是它们如何关联的:因此,第一个微调数组-String与第一个片段TypedArray项相关,依此类推。
我编写了一个简单的类,以便可以在微调器值和微调器值表示的片段之间保持关联。这是课程:
import android.support.v4.app.Fragment;
public class SensorList {
int index;
String name;
Fragment sensorFrag;
SensorList(){
name="";
sensorFrag=null;
index=0;
}
}
我想用来自资源TypedList的值填充此类的实例。那就是我遇到问题的地方。到目前为止,这是我的代码。最后一行显然是错误的,因为它没有“ add”的索引。
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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句