当前位置: 动力学知识库 > 问答 > 编程问答 >

android - how to add json array data in expandable listview

问题描述:

This is my json data how to fetch this in ExpandableListView

{

"category": [

{

"id": 6,

"name": "Vegetarian",

"parentid": "1",

"childs": [

{

"id": "6A",

"name": "Vegetarian new",

"parentid": "6"

},

{

"id": "6B",

"name": "Vagetarian old",

"parentid": "6"

}

]

},

{

"id": 7,

"name": "Non-Vegetarian",

"parentid": "1",

"childs": [

{

"id": "7A",

"name": "Non-Vegetarian new",

"parentid": "7"

},

{

"id": "7B",

"name": "Non-Vagetarian old",

"parentid": "7"

}

]

}

]

}

Thank you in advance.

网友答案:

First of all parse the json data in hashmap or arraylist or in a model class you can take reference of this.

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ How to parse JSON in Android

Check out the custom expandable listview demo here :

http://www.demoadda.com/demo/android/custom-expandable-listview-latest-demo-in-android_39

You will get more idea to implement the custom expandable list view.

private class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private LayoutInflater inflater;

    public MyExpandableListAdapter() {
        // Create Layout Inflator
        inflater = LayoutInflater.from(ExpandableListMain.this);
    }

    // This Function used to inflate parent rows view

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parentView) {
        final Parent parent = parents.get(groupPosition);

        // Inflate grouprow.xml file for parent rows
        convertView = inflater
                .inflate(R.layout.grouprow, parentView, false);

        // Get grouprow.xml file elements and set values
        ((TextView) convertView.findViewById(R.id.text1)).setText(parent
                .getText1());
        ((TextView) convertView.findViewById(R.id.text)).setText(parent
                .getText2());
        ImageView image = (ImageView) convertView.findViewById(R.id.image);
        image.setImageResource(getResources().getIdentifier(
                "com.androidexample.customexpandablelist:drawable/setting"
                        + parent.getName(), null, null));
        // ImageView rightcheck = (ImageView) convertView
        // .findViewById(R.id.rightcheck);

        // Log.i("onCheckedChanged", "isChecked: "+parent.isChecked());

        // Change right check image on parent at runtime
        // if (parent.isChecked() == true) {
        // rightcheck
        // .setImageResource(getResources()
        // .getIdentifier(
        // "com.androidexample.customexpandablelist:drawable/rightcheck",
        // null, null));
        // } else {
        // rightcheck
        // .setImageResource(getResources()
        // .getIdentifier(
        // "com.androidexample.customexpandablelist:drawable/button_check",
        // null, null));
        // }

        // Get grouprow.xml file checkbox elements
        CheckBox checkbox = (CheckBox) convertView
                .findViewById(R.id.checkbox);
        checkbox.setChecked(parent.isChecked());

        // Set CheckUpdateListener for CheckBox (see below
        // CheckUpdateListener class)
        checkbox.setOnCheckedChangeListener(new CheckUpdateListener(parent));

        return convertView;
    }

    // This Function used to inflate child rows view
    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parentView) {
        final Parent parent = parents.get(groupPosition);
        final Child child = parent.getChildren().get(childPosition);

        // Inflate childrow.xml file for child rows
        convertView = inflater
                .inflate(R.layout.childrow, parentView, false);

        // Get childrow.xml file elements and set values
        ((TextView) convertView.findViewById(R.id.text1)).setText(child
                .getText1());
        ImageView image = (ImageView) convertView.findViewById(R.id.image);
        image.setImageResource(getResources().getIdentifier(
                "com.androidexample.customexpandablelist:drawable/setting"
                        + parent.getName(), null, null));

        return convertView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // Log.i("Childs", groupPosition+"=  getChild =="+childPosition);
        return parents.get(groupPosition).getChildren().get(childPosition);
    }

    // Call when child row clicked
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        /****** When Child row clicked then this function call *******/

        // Log.i("Noise",
        // "parent == "+groupPosition+"=  child : =="+childPosition);
        if (ChildClickStatus != childPosition) {
            ChildClickStatus = childPosition;

            Toast.makeText(
                    getApplicationContext(),
                    "Parent :" + groupPosition + " Child :" + childPosition,
                    Toast.LENGTH_LONG).show();
        }

        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int size = 0;
        if (parents.get(groupPosition).getChildren() != null)
            size = parents.get(groupPosition).getChildren().size();
        return size;
    }

    @Override
    public Object getGroup(int groupPosition) {
        Log.i("Parent", groupPosition + "=  getGroup ");

        return parents.get(groupPosition);
    }

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

    // Call when parent row clicked
    @Override
    public long getGroupId(int groupPosition) {
        Log.i("Parent", groupPosition + "=  getGroupId "
                + ParentClickStatus);

        if (groupPosition == 2 && ParentClickStatus != groupPosition) {

            // Alert to user
            Toast.makeText(getApplicationContext(),
                    "Parent :" + groupPosition, Toast.LENGTH_LONG).show();
        }

        ParentClickStatus = groupPosition;
        if (ParentClickStatus == 0)
            ParentClickStatus = -1;

        return groupPosition;
    }

    @Override
    public void notifyDataSetChanged() {
        // Refresh List rows
        super.notifyDataSetChanged();
    }

    @Override
    public boolean isEmpty() {
        return ((parents == null) || parents.isEmpty());
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    /******************* Checkbox Checked Change Listener ********************/

    private final class CheckUpdateListener implements
            OnCheckedChangeListener {
        private final Parent parent;

        private CheckUpdateListener(Parent parent) {
            this.parent = parent;
        }

        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            Log.i("onCheckedChanged", "isChecked: " + isChecked);
            parent.setChecked(isChecked);

            ((MyExpandableListAdapter) getExpandableListAdapter())
                    .notifyDataSetChanged();

            final Boolean checked = parent.isChecked();
            Toast.makeText(
                    getApplicationContext(),
                    "Parent : " + parent.getName() + " "
                            + (checked ? STR_CHECKED : STR_UNCHECKED),
                    Toast.LENGTH_LONG).show();
        }
    }
    /***********************************************************************/

}

And for child and parent model

public class NameOfClass {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: