Matteo
Matteo

Reputation: 8142

Populate an ExpandableListView from XML file - Android

In my Android application I've got an Activity containing an ExpandableListView.

The items it will contain should be extracted from an XML file that the application will query to a Server when it starts (suppose the size of the file is not an issue).

The user should then be able to make modifications to the content of the XML file by adding, removing, editing items from the ExpandableListView with functionalities provided by the application. Eventually the application will send back the modified XML file to the Server.

For better understanding this mockup should explain what I'm trying to implement:

enter image description here

I'd like to know:

how can I populate dynamically in Java the area highlighted in red given the XML file?

SAMPLE XML FILE:

<?xml version="1.0" encoding="utf-8"?>
<Category value="Animals">
    <entry>Cat</entry>
    <entry>Dog</entry>
    <entry>Elephant</entry>
</Category>
<Category value="Objects">
    <entry>Aeroplane</entry>
    <entry>Ball</entry>
    <entry>Closet</entry>
</Category>

ADDED DEBUG PART

I've tried to implement the solution proposed in the answer by @Luksprog but am facing a java.lang.NullPointerException when running the following code:

Code:

//gets the View from the Layout file
myCustomExpandableListView = (ExpandableListView) findViewById( R.id.myCustomExpandableListView );
//creates the array list that will contain all labels
ArrayList<Category> labelsInTaxonomy = new ArrayList<Category>();
//fills it with a private method that parses the XML and fills the array list
this.loadTaxonomyFromXml( labelsInTaxonomy );
//creates the custom expandable list adapter
CustomExpandable labelTaxonomyAdapter = new CustomExpandable( this, labelsInTaxonomy );
//sets the adapter
myCustomExpandableListView.setAdapter( labelTaxonomyAdapter );

Error:

E/AndroidRuntime( 5972): FATAL EXCEPTION: main
E/AndroidRuntime( 5972): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.DVA_HLUI/com.DVA_HLUI.DVA_HLUIManageTaxonomyActivity}: java.lang.NullPointerException
E/AndroidRuntime( 5972):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
E/AndroidRuntime( 5972):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
E/AndroidRuntime( 5972):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
E/AndroidRuntime( 5972):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
E/AndroidRuntime( 5972):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 5972):    at android.os.Looper.loop(Looper.java:143)
E/AndroidRuntime( 5972):    at android.app.ActivityThread.main(ActivityThread.java:4196)
E/AndroidRuntime( 5972):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 5972):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 5972):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 5972):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 5972):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 5972): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 5972):    at com.DVA_HLUI.DVA_HLUIManageTaxonomyActivity.onCreate(DVA_HLUIManageTaxonomyActivity.java:80)
E/AndroidRuntime( 5972):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
E/AndroidRuntime( 5972):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)

Note that

com.DVA_HLUI.DVA_HLUIManageTaxonomyActivity.onCreate(DVA_HLUIManageTaxonomyActivity.java:80)

corresponds to this line of the code

myCustomExpandableListView.setAdapter( labelTaxonomyAdapter );

Any ideas of what I'm doing wrong?

Upvotes: 3

Views: 3346

Answers (1)

user
user

Reputation: 87064

I would make a custom class like this:

class Category {

    private ArrayList<String> mEntries = new ArrayList<String>();
    private String mCaTegoryName;

    public void addEntry(String entry) {
        mEntries.add(entry);
    }

    public void setCategoryName(String categoryName) {
        mCaTegoryName = categoryName;
    }

    public ArrayList<String> getEntries() {
        return mEntries;
    }

    public String getCategoryName() {
        return mCaTegoryName;
    }
}

as a data structure and use the ArrayList<Category> that will result from parsing the xml in a custom adapter:

class CustomExpandable extends BaseExpandableListAdapter {

    private ArrayList<Category> mItems;

    public CustomExpandable(Context context, ArrayList<Category> items) {
        mItems = items;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mItems.get(groupPosition).getEntries().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // implement the child view row
        return null;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mItems.get(groupPosition).getEntries().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mItems.get(groupPosition).getCategoryName();
    }

    @Override
    public int getGroupCount() {
        return mItems.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // implement the group View
        return null;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

Upvotes: 2

Related Questions