Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有JSON数组的ExpandableListView_Android_Json_Expandablelistview - Fatal编程技术网

Android 带有JSON数组的ExpandableListView

Android 带有JSON数组的ExpandableListView,android,json,expandablelistview,Android,Json,Expandablelistview,如果我想要一个ExpandableListView,它从服务器(JSON数组)的响应中获取组及其子组的数据。 我该怎么做? 我知道有SimpleCursorTreeAdapter,但它只用于cursor,我以前也使用过它,但我需要这个适配器的任何等价物来与JSON数组一起使用它 这是获取groupItems的函数 protected void BindOrederItemList(final int order_id) { // TODO Auto-ge

如果我想要一个ExpandableListView,它从服务器(JSON数组)的响应中获取组及其子组的数据。
我该怎么做?
我知道有SimpleCursorTreeAdapter,但它只用于cursor,我以前也使用过它,但我需要这个适配器的任何等价物来与JSON数组一起使用它

这是获取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();

        }
    }
protectedvoid bindoredireItemList(最终整数顺序\u id)
{
//TODO自动生成的方法存根
//从order_id=??的订单项目中选择所有类别??
//选择类别=??
字符串Url=”http://192.168.3.113/mywebservices.php?op=GetOrderCategory&lang_id=1&order_id=“+订单编号;
GetNetworkData.OnPostExecuteListener侦听器=新建GetNetworkData.OnPostExecuteListener()
{
@凌驾
public void onPostExecute(字符串结果){
//TODO自动生成的方法存根
如果(结果!=null)
{
尝试
{
JSONArray jArray=新JSONArray(结果);
HashMap Catmap=新的HashMap();
//ArrayList类别=新建ArrayList();
for(int i=0;i

谢谢

一种方法是使用


在这种情况下,您必须首先获取JSON数据,然后将其存储为映射列表。您可以使用获取并重新打包数据,然后在后台创建适配器的新实例,然后将其交换到ExandableListView中。

我已经解决了当数据是动态的并且来自mySQL数据库(JSON数组)时如何处理可扩展列表的问题。以下是答案

        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;


    }
protectedvoid bindoredireItemList(最终整数顺序\u id)
{
//TODO自动生成的方法存根
//从order_id=??的订单项目中选择所有类别??
//选择类别=??
JSONObject参数=新的JSONObject();
//int no_pepole=Integer.valueOf(noOfGest_txt.getText().toString());
尝试
{
//参数put(“订单状态”,myStatus);
int rest\u id=prefs.getInt(“rest\u id”,0);
参数put(“订单id”,订单id);
参数put(“lang_id”,1);
参数put(“剩余id”,剩余id);
//参数put(“订单状态”,0);
//参数put(“noOfpepole”,客人数量);
}
捕获(例外e)
{
e、 printStackTrace();
}
字符串Url=”http://192.168.3.113/mywebservices.php?op=GetOrderCategory";
GetNetworkData.OnPostExecuteListener侦听器=新建GetNetworkData.OnPostExecuteListener()
{
@凌驾
public void onPostExecute(字符串结果){
//TODO自动生成的方法存根
如果(结果!=null)
{
尝试
{
catItemsList.clear();
JSONArray jArray=新JSONArray(结果);
ArrayList=新建ArrayList();
//ArrayList类别=新建ArrayList();
for(int i=0;i