Apurv Apurv - 5 months ago 22
Android Question

Use Json data in Exapndable listview

[
{
"questionid": "1",
"question": "What is PHP?",
"answer": "fvsawf",
"categoryId": "1",
"subcategory_id": "1",
"isactive": "1",
"createddate": "2016-06-04 09:32:39",
"subcategoryId": "1",
"subcategoryname": "PHP",
"categoryid": "2",
"categoryname": "PHP",
"displayorder": "2",
"createdate": "2016-06-04 09:17:53",
"modifieddate": "0000-00-00",
"isActive": "1"
},
{
"questionid": "2",
"question": "what is object ?",
"answer": "hello",
"categoryId": "1",
"subcategory_id": "1",
"isactive": "1",
"createddate": "2016-06-04 09:32:39",
"subcategoryId": "1",
"subcategoryname": "PHP",
"categoryid": "2",
"categoryname": "PHP",
"displayorder": "2",
"createdate": "2016-06-04 09:17:53",
"modifieddate": "0000-00-00",
"isActive": "1"
}
]


I am new in android.

I want to know that how can i use this json data in exapndable listview.

i want to set "question" as a GROUP and "answer" as a CHILD.

I see lots of examples but they have static data. i couldn't find any example with json data. So please help me.

Thanks in advance:)

Answer

i am using this url for Expandable LINK

DOWNLOAD DEMO

json data

    {
  worldpopulation: [
    {
      rank: 1,
      country: "China",
      population: "1,354,040,000",
      flag: "http://www.androidbegin.com/tutorial/flag/china.png"
    },
    {
      rank: 2,
      country: "India",
      population: "1,210,193,422",
      flag: "http://www.androidbegin.com/tutorial/flag/india.png"
    },
    {
      rank: 3,
      country: "United States",
      population: "315,761,000",
      flag: "http://www.androidbegin.com/tutorial/flag/unitedstates.png"
    },
    {
      rank: 4,
      country: "Indonesia",
      population: "237,641,326",
      flag: "http://www.androidbegin.com/tutorial/flag/indonesia.png"
    }
  ]
}

Proof

enter image description here

Fetch json data like this

 private class DownloadJason extends AsyncTask<Void, Void, Void> {


        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            mprocessingdialog.setTitle("Please Wait..");
            mprocessingdialog.setMessage("Loading");
            mprocessingdialog.setIndeterminate(false);
            mprocessingdialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            listDataHeader = new ArrayList<String>();
            JSONParser jp = new JSONParser();
            String jsonstr = jp.makeServiceCall(url);
            Log.d("Json url view", jsonstr);

            if (jsonstr != null) {

                listDataHeader = new ArrayList<String>();
                listDataChild = new HashMap<String, List<String>>();

                try {
                    JSONObject jobj = new JSONObject(jsonstr);
                    JSONArray jarray = jobj.getJSONArray("worldpopulation");
                    for (int hk = 0; hk < jarray.length(); hk++) {
                        JSONObject d = jarray.getJSONObject(hk);
                        // Adding Header data

                        listDataHeader.add(d.getString("country"));
                        // Adding child data for lease offer
                        List<String> lease_offer = new ArrayList<String>();

                        lease_offer.add(d.getString("country") + "'s Rank is : "
                                + d.getString("rank"));
                        lease_offer.add("And Population is "+d.getString("population"));
                        // Header into Child data
                        listDataChild.put(listDataHeader.get(hk), lease_offer);

                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            } else {
                Toast.makeText(getApplicationContext(),
                        "Please Check internet Connection", Toast.LENGTH_SHORT)
                        .show();

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            mprocessingdialog.dismiss();

            listAdapter = new ExpandableListAdapter(getApplicationContext(), listDataHeader, listDataChild);

            // setting list adapter
            expListView.setAdapter(listAdapter);

        }
    }

MyAdapter

    import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

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

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

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

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

Download demo here

DOWNLOAD DEMO

Comments