user2552828
user2552828

Reputation: 5

dynamically create resource ids android

Im trying to Dynamically fetch an XML array from a resource file however I keep getting Exception Number error.

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

Upvotes: 1

Views: 1087

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191681

I think you should be doing something like this

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

If you want to get the resource id for R.array.c<key>.

Upvotes: 1

Related Questions