1

If I want to have an ExpandableListView, which gets data from a response from server(JSON array) for its groups and their children.
How can I do it?
I know that there is SimpleCursorTreeAdapter, but it used only for cursor and I used it before, but I need any equivalent for this adapter to use it with JSON arrays

This is the function to get the groupItems

        protected void BindOrederItemList(final int order_id) 
    {
        // TODO Auto-generated method stub
        //select all categories from order items where order_id =??
        //select items where category=??
        String Url="http://192.168.3.113/mywebservices.php?op=GetOrderCategory&lang_id=1&order_id="+order_id;
        GetNetworkData.OnPostExecuteListener listener=new GetNetworkData.OnPostExecuteListener() 
        {

            @Override
            public void onPostExecute(String result) {
                // TODO Auto-generated method stub
                if (result!=null)
                {
                    try
                    {

                 JSONArray jArray = new JSONArray(result);
                 HashMap<String, String> Catmap = new HashMap<String, String>();
               //  ArrayList<Integer> category=new ArrayList<Integer>();
                 for (int i = 0; i < jArray.length(); i++) 

                    {

                        JSONObject e = jArray.getJSONObject(i);
                            id=e.getInt("order_id");

                         cat_name=e.getString("cat_name");
                          cat_id=e.getInt("cat_id");
                          Catmap.put("cat_id",String.valueOf(cat_id));
                          Catmap.put("cat_name", cat_name);
                          catItemsList.add(Catmap);

                    }
                Log.i("Insid For Loop", "Group size = "+catItemsList.toArray());



                      //  Log.i("Bind item", "CAT SIZE "+catItemsList.size());

                        }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }

            }

        };

        try
        {
        GetNetworkData task = new GetNetworkData(Url,null,listener);
        task.execute();
        }
        catch(Exception e)
        {

            e.printStackTrace();

        }
    }

Thanks

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
Basant
  • 305
  • 1
  • 8
  • 23
  • @aromero you edit my question but i can't find the changes could u tell me how can i solve my problem ..Thank you – Basant May 07 '12 at 21:58

2 Answers2

1

One way of doing it is using a SimpleExandableListAdapter.

In that case you will have to first fetch and then store your JSON data as a List of Maps. That you could accomplish using an AsyncTaskLoader, in which you fetch and repackage the data, and then create a new instance of your adapter in the background, which you then swap into the ExandableListView.

Mikael Ohlson
  • 3,074
  • 1
  • 22
  • 28
  • i edit my question to put the code i write till now could you tell me please where to put AsyncTaskLoader in my code ,Thanks – Basant May 06 '12 at 13:26
  • i used AsyncTask to get data from server is it different from AsyncTaskLoader – Basant May 06 '12 at 13:33
  • @Basant, you'll get by with AsyncTask as well. [Here is an example](http://www.coderzheaven.com/2011/04/10/expandable-listview-in-android-using-simpleexpandablelistadapter-a-simple-example/) on how to use/populate the SimpleExpandableListAdapter if that was what you where asking for. If not, please ask me again? – Mikael Ohlson May 06 '12 at 16:00
  • Thank u I'll see this example and I'll give you a feadback ,thanks Mikael :) – Basant May 06 '12 at 17:45
  • i see the example but this is used for static data ,if i want to get the data from database it will be the same ?? – Basant May 07 '12 at 14:52
  • ,this is like what i want but i want to get groups(categories) and its child(Items) dynamically from database and i couldn't know how to do this ..do u got my point ,Thanks for help – Basant May 07 '12 at 21:39
  • @Basant, possibly [this SO answer will help you](http://stackoverflow.com/questions/5188196/how-to-write-custom-expandablelistadapter/5199828#5199828) – Mikael Ohlson May 08 '12 at 15:33
  • @Basant, glad to see that it helped, although I would have thought you'd award me with the correct answer, and not yourself... – Mikael Ohlson May 10 '12 at 08:51
1

i have solve the problem of how to deal with expandable list when the data is dynamic and from mySQL database (JSON array) here is the answer

        protected void BindOrederItemList(final int order_id) 
    {
        // TODO Auto-generated method stub
        //select all categories from order items where order_id =??
        //select items where category=??

        JSONObject params = new JSONObject();
        //int    no_pepole=Integer.valueOf(noOfGest_txt.getText().toString());
            try
            {
        //  params.put("order_status",myStatus);
         int rest_id=prefs.getInt("Rest_id", 0);
            params.put("order_id", order_id);
            params.put("lang_id", 1);
            params.put("rest_id", rest_id );
            //params.put("order_status", 0);
        //  params.put("noOfpepole",number_of_guest);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }



        String Url="http://192.168.3.113/mywebservices.php?op=GetOrderCategory";
        GetNetworkData.OnPostExecuteListener listener=new GetNetworkData.OnPostExecuteListener() 
        {

            @Override
            public void onPostExecute(String result) {
                // TODO Auto-generated method stub
                if (result!=null)
                {
                    try
                    {
                     catItemsList.clear();
                 JSONArray jArray = new JSONArray(result);
                 ArrayList<ArrayList<HashMap<String, String>>> list=new ArrayList<ArrayList<HashMap<String, String>>>();
               //  ArrayList<Integer> category=new ArrayList<Integer>();

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

                    {
                      HashMap<String, String> Catmap = new HashMap<String, String>();

                        JSONObject e = jArray.getJSONObject(i);
                            id=e.getInt("order_id");

                         cat_name=e.getString("cat_name");
                          cat_id=e.getInt("cat_id");
                          Catmap.put("cat_id",String.valueOf(cat_id));
                          Catmap.put("cat_name", cat_name);
                          catItemsList.add(Catmap);
                          Log.i("Insid For Loop", "order ID "+order_id);

                          list=  BindCatItems(cat_id, order_id);



                        Log.i("Insid For Loop", "Child size = "+list.size());
                    }
               // Log.i("Insid For Loop", "Group size = "+catItemsList.size());

                 SimpleExpandableListAdapter expandListAdapter= new SimpleExpandableListAdapter(getActivity(), 
                          catItemsList,  R.layout.group_item, 
                          new String[] {"cat_name"},new int[]{R.id.lbl_cat_group},
                          BindCatItems(cat_id, order_id), R.layout.category_row, new String[]{"item_name"}, new int[]{R.id.txt_category_row});


                 order_items_list.setAdapter(expandListAdapter);
                      //  Log.i("Bind item", "CAT SIZE "+catItemsList.size());

                        }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }

            }

        };

        try
        {
        GetNetworkData task = new GetNetworkData(Url,params,listener);
        task.execute();
        }
        catch(Exception e)
        {

            e.printStackTrace();

        }
    }

    protected ArrayList<ArrayList<HashMap<String, String>>> BindCatItems(int cat_id,int order_id)

    {
        // TODO Auto-generated method stub
        ItemsList.clear();
        JSONObject params = new JSONObject();

        //int    no_pepole=Integer.valueOf(noOfGest_txt.getText().toString());
            try
            {
        //  params.put("order_status",myStatus);
         int rest_id=prefs.getInt("Rest_id", 0);
            params.put("order_id", order_id);
            params.put("lang_id", 1);
            params.put("cat_id",cat_id );
            //params.put("order_status", 0);
        //  params.put("noOfpepole",number_of_guest);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }


        String Url="http://192.168.3.113/mywebservices.php?op=GetOrderItems";
        GetNetworkData.OnPostExecuteListener listener=new GetNetworkData.OnPostExecuteListener() 

        {

            @Override
            public void onPostExecute(String result) {
                // TODO Auto-generated method stub
                if (result!=null)
                {
                    try
                    {
                        Log.i("log bind","Inside Bind Category items");
                        // catItemsList.clear();
                         ArrayList<HashMap<String, String>> mapList = 
                                  new ArrayList<HashMap<String, String>>(); 
                        JSONArray jArray = new JSONArray(result);

                            for (int i = 0; i < jArray.length(); i++) 
                            {
                                HashMap<String, String> map = new HashMap<String, String>();

                                JSONObject e = jArray.getJSONObject(i);

                                int id=e.getInt("item_id");
                                if (id==0)
                                {

                                }


                                else
                                {



                                map.put("item_id",String.valueOf(e.getInt("item_id")));
                                map.put("oi_id", String.valueOf(e.getInt("oi_id")));
                                map.put("item_name", e.getString("item_name"));
                                map.put("quantity",String.valueOf( e.getString("quantity")));
                                map.put("price", String.valueOf("price"));
                       mapList.add(map);

                                }
                            }

                            ItemsList.add(mapList);

                         //   Log.i("Bind Item Order", "CAT SIZE "+catItemsList.size());



                           /*  ItemsAdapter=new SimpleAdapter(getActivity(), catItemsList, 
                                    R.layout.list_item,
                                    new String[] {"item_name"},
                                    new int[]{R.id.list_item_title});
                           */
                           //  Log.i("Add Section","ItemsAdapter count= "+ItemsAdapter.getCount());


                             //order_list.setAdapter(adapter);
                            //adapter.notifyDataSetChanged();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        };

        try
        {
        GetNetworkData task = new GetNetworkData(Url,params,listener);
        task.execute();
        }
        catch(Exception e)
        {

            e.printStackTrace();

        }
        return ItemsList;


    }
Basant
  • 305
  • 1
  • 8
  • 23
  • 1
    Hey @Basant, if you had use of my answers, it's not very polite to write your own answer based on that info, then award yourself the correct answer and not even upvote my answer.. Come on, be a good sport. – Mikael Ohlson May 11 '12 at 16:27