如何在Android中使用json数据创建ExpandableListview

时间:2016-07-20 11:27:55

标签: android json listview expandablelistview

任何人都可以建议如何使用Json数据创建可扩展的列表视图,我想在我的可扩展列表视图中解析json数据,plss建议我如何创建

1 个答案:

答案 0 :(得分:1)

这正是您所寻找的,您可以解析并向ExpandableListview显示数据

请参阅:http://www.tutorialsbuzz.com/2015/02/android-expandable-listview-json-http.html

public class MainActivity extends Activity {
    String url = "http://api.tutorialsbuzz.com/cricketworldcup2015/cricket.json";
    ProgressDialog PD;

    private ExpandListAdapter ExpAdapter;
    private ExpandableListView ExpandList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ExpandList = (ExpandableListView) findViewById(R.id.exp_list);

        PD = new ProgressDialog(this);
        PD.setMessage("Loading.....");
        PD.setCancelable(false);

        makejsonobjreq();
    }

    private void makejsonobjreq() {
        PD.show();

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url,
                null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        ArrayList<Group> list = new ArrayList<Group>();
                        ArrayList<Child> ch_list;

                        try {
                            Iterator<String> key = response.keys();
                            while (key.hasNext()) {
                                String k = key.next();

                                Group gru = new Group();
                                gru.setName(k);
                                ch_list = new ArrayList<Child>();

                                JSONArray ja = response.getJSONArray(k);

                                for (int i = 0; i < ja.length(); i++) {

                                    JSONObject jo = ja.getJSONObject(i);

                                    Child ch = new Child();
                                    ch.setName(jo.getString("name"));
                                    ch.setImage(jo.getString("flag"));

                                    ch_list.add(ch);
                                } // for loop end
                                gru.setItems(ch_list);
                                list.add(gru);
                            } // while loop end

                            ExpAdapter = new ExpandListAdapter(
                                    MainActivity.this, list);
                            ExpandList.setAdapter(ExpAdapter);

                            PD.dismiss();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        PD.dismiss();
                    }
                });
        MyApplication.getInstance().addToReqQueue(jsonObjReq, "jreq");
    }
}