2016-04-01 58 views
0

我試圖從資源文件動態獲取XML數組,但是我不斷收到Exception Number錯誤。動態創建資源編號android

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_class, container, false); 
      TextView classTitle = (TextView) rootView.findViewById(R.id.class_title); 
      classTitle.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 

      //FETCH THE XML ARRAY 
      String[] classAvgArray = getResources().getStringArray(R.array.classAvg); 
      //INDEX OF THE ARRAY 
      int key = getArguments().getInt(ARG_SECTION_NUMBER); 

      //DISPLAY THE AVG AMOUNT 
      TextView classTextAverage = (TextView) rootView.findViewById(R.id.classAvgAmount); 
      classTextAverage.setText("Avg: "+classAvgArray[key]); 

      //FETCH THE XML ARRAY FOR ITEMS 
      String ID = "c"+key; 

      int resID = getResources().getIdentifier(ID, "R.array.c"+key, "za.co.infomycito.mycitoapp"); 

      String[] classactivities = getResources().getStringArray(resID); 

回答

1

,我認爲你應該做這樣的

int resID = getResources().getIdentifier("c"+key, "array", getPackageName()); 

的東西,如果你想獲得的資源ID R.array.c<key>

+0

感謝您的幫助! – user2552828