动态创建资源ID android

时间:2016-04-01 14:08:27

标签: java android

我试图从资源文件动态获取XML数组但是我不断收到异常编号错误。

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 个答案:

答案 0 :(得分:1)

我认为你应该做这样的事情

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

如果您想获取R.array.c<key>的资源ID。