2016-07-20 34 views
-2
JSON數據創建ExpandableListview

任何人都可以表明,如何建立可擴展列表視圖使用JSON數據,我想在我的擴展列表視圖解析JSON數據,PLSS建議如何創建如何與Android的

+0

檢查我的答案,讓我知道 –

+0

@ 0xDEADC0DE IM只是要求知道如何開發它,我已經開發與靜態dat但是當我嘗試解析服務響應json數據時出現問題。 –

+0

是啊@Aditya Vyas - Lakhan我想要這樣.....你在鏈接中提到了...... –

回答

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"); 
    } 
}