【问题标题】:How to load values into an array list from a resource如何将值从资源加载到数组列表中
【发布时间】:2015-09-27 03:32:33
【问题描述】:

最终目标是在不修改代码的情况下为程序添加额外的片段。我想通过更改资源文件的数组值(而不是在运行时)来做到这一点。一个资源数组具有微调器值,另一个具有片段文件名。它们在资源文件中列出的顺序是它们的关联方式:因此第一个微调器数组字符串与第一个片段 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.

我是不是走错了路?有没有我可以申请的演员表。 如果这不清楚,请告诉我。谢谢。

【问题讨论】:

    标签: android android-fragments arraylist


    【解决方案1】:
     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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 2011-08-09
      • 2020-08-26
      • 1970-01-01
      • 2011-07-25
      • 2018-02-20
      相关资源
      最近更新 更多