0

i have a typedArray that contains icons i want to use in my listview.when I tried to reference elements contained in the typedArray in the for-loop this.navDrawerIconsas shown below, eclipse gives an error as if the typedArray should not be referenced like normal arrays.

how can I reference the typedArray element by element from a loop

code

    private void initViews() {
    // TODO Auto-generated method stub
    setContentView(R.layout.activity_main);
    this.navDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    this.drawerListView = (ListView) findViewById(R.id.drawerListView);
    this.navDrawerOptions = getResources().getStringArray(R.array.nav_drawer_items);
    this.navDrawerIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
    this.navDrawerOptionDescription =   
    getResources().getStringArray(R.array.nav_drawer_items_descriptions);

    this.navDrawerArrayList = new ArrayList<NavDrawerItemDesign>();
    setNavDrawerArrayList(this.navDrawerArrayList);

    for (int i = 0; i < this.navDrawerOptions.length; i++) {
        changeSingleOptionContents(this.navDrawerOptions[i], this.navDrawerIcons[i],   this.navDrawerOptionDescription[i],VISIBLE);
    }
}

2 Answers 2

2

obtainTypedArray returns another TypedArray. You can't access elements inside it like regular arrays. So if your R.array.nav_drawer_icons is an array containing resource ids at each index, then use this.navDrawerIcons.getResourceId(i, <some default resource you want to show if there is nothing at index i>) or if you have an array of drawables(i.e., @drawable\R.drawable.something), use this.navDrawerIcons.getDrawable(i).

Also, as a side note, remember to do this.navDrawerIcons.recycle() after the loop and don't access the typed array after that. Something else to think about, do you need to store those arrays and the typed array icons as class members? You only need to access them in that method scope, if you are not going to need them after creating the list, then you can change them to local variables instead of class members.

Sign up to request clarification or add additional context in comments.

Comments

1

use this.navDrawerIcons.getResourceId(i, -1) where i is the number of the element and -1 is the default value returned if the image does not exist.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.