如何使用SimpleExpandableListAdapter获取JSON数据

时间:2019-05-20 18:37:08

标签: java android

使用SimpleExpandableListAdapter将json值显示为我在下面显示的ExpandableListview.json值

[{"Studymaterials":[{"subject":"ENGLISH","attachment":[{"docid":"10053","docname":"test one.jpg"},{"docid":"10054","docname":"test two.jpg"}]}]}]
public class ff extends AppCompatActivity {

    private ProgressDialog progressDialog;
    String userid="";
    ArrayList<HashMap<String, String>> itemslistheader;
    ArrayList<ArrayList<HashMap<String, String>>> itemschildlist;
    SimpleExpandableListAdapter itemsadapter;
    ExpandableListView list_LV;
    String[] from = {"subject"};
    int[] to = {R.id.heading, R.id.teachernametxt};
    String[] childfrom = {"docname"};
    int[] childto = {R.id.childItem};

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

        Intent intent = getIntent();
        userid = intent.getStringExtra("userid");

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(false);

        list_LV = (ExpandableListView) findViewById(R.id.list_LV);
        itemslistheader = new ArrayList<HashMap<String, String>>();
        itemschildlist = new ArrayList<ArrayList<HashMap<String, String>>>();
        itemsadapter = new SimpleExpandableListAdapter(this, itemslistheader, R.layout.exp_study_header, from, to,
                itemschildlist, R.layout.exp_study_child, childfrom, childto);
        list_LV.setAdapter(itemsadapter);
        Start();
        progressDialog.show();
    }

    public void Start() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String http = LinkUrl.LINK+"myStudyMaterial.php?userid="+userid;
        String httplink = http.replace(" ","%20");
        System.out.println(httplink);
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(httplink,

                new Response.Listener<JSONArray>() {

                    public void onResponse(JSONArray jsonArray) {

                        for (int i = 0; i < jsonArray.length(); i++) {
                            try {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                                String files = jsonObject.getString("Studymaterials").toString();
                                itemslistheader.clear();
                                if (!files.equals("empty")) {

                                    JSONArray jsonSubNode = jsonObject.getJSONArray("Studymaterials");
                                    int lengthJsonArrsub = jsonSubNode.length();
                                    for (int ii = 0; ii < lengthJsonArrsub; ii++) {
                                        JSONObject jsonChildSubNode = jsonSubNode.getJSONObject(ii);

                                        HashMap<String, String> myMap = new HashMap<String, String>();
                                        myMap.put("subject", jsonChildSubNode.getString("subject").toString());
                                        itemslistheader.add(myMap);

                                        String attachment = jsonChildSubNode.getString("attachment").toString();
                                        //get child data here and need to show child list
                                    }
                                    progressDialog.dismiss();
                                }else{
                                    progressDialog.dismiss();
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        itemsadapter.notifyDataSetChanged();
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                progressDialog.dismiss();
            }
        });
        requestQueue.add(jsonArrayRequest);
    }
}

0 个答案:

没有答案