2015-09-27 61 views
1

最終目標是在不修改代碼的情況下向程序中添加其他片段。我想通過更改資源文件的數組值(不是在運行時)來完成此操作。一個資源數組具有微調器值,另一個具有片段文件名稱。它們在資源文件中列出的順序是它們如何關聯的:因此,第一個微調器數組 - 字符串與第一個片段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. 

我該怎麼做呢?是否有我可以申請的演員陣容? 如果不明確,請告訴我。謝謝。

回答

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();