Frage

I am not exactly clear on how to implement ExpandableList as array resource instead of hard-coded array (by replacing an existing hard-coded array with XML layout). Here is my strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">adaptor_ExpandableList1</string>

    <string name="expandable_list_sample_action">Sample action</string>
    <string-array name="Groups">
        <item>People Names</item>
        <item>Dog Names</item>
        <item>Cat Names</item>
        <item>Fish Names</item>
    </string-array>
    <string-array name="People Names">
        <item>Arnold</item>
        <item>Barry</item>
        <item>Chuck</item>
        <item>David</item>
    </string-array>
    <string-array name="Dog Names">
        <item>Ace</item>
        <item>Bandit</item>
        <item>Cha-Cha</item>
        <item>Deuce</item>
    </string-array>
    <string-array name="Cat Names">
        <item>Fluffy</item>
        <item>Snuggles</item>      
    </string-array>
    <string-array name="Fish Names">
        <item>Goldy</item>
        <item>Bubbles</item>
    </string-array>
</resources>

Here is what I changed in the Java file:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = getResources().getStringArray(R.array.Groups);
    private String[][] children = {getResources().getStringArray(R.array.People Names),
        getResources().getStringArray(R.array.Dog Names),
        getResources().getStringArray(R.array.Cat Names),
        getResources().getStringArray(R.array.Fish Names)
    };

Of course R.array.xxx Names does not work. So how can I correct the problem when there are statements in R.java such as this: public static final int Cat Names=0x7f040003;. There must be a rule for this but I don't know it yet.

Browsing through numerous examples, I think I also need to have group and child layout files and perhaps a main layout file (I think I know how to implement them if I need to). Is this correct?

Thanks in advance for your help!

War es hilfreich?

Lösung

you have space in the name of string array

remove space from "people names" and other string array names.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top