1

我们正在开发一个产品,我们必须在其中解析一个 json 数组并将其显示在一个具有 3 个级别的可扩展列表视图中。我尝试了这个概念,我只达到了我无法进入下两个级别的第一级。我是 android 开发新手,请帮助我实现这一目标。我的代码如下....

package com.example.expanadbalelistview;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    int aa;
    ArrayList<String[]> subcats1;
    String results;
    JSONArray jarray,jk;
    ExpandableListView explvlist;
    String top_cats[];
    String sub_cats[];

    private LayoutInflater inflater;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           inflater = LayoutInflater.from( MainActivity.this);
           new as(aa).execute();
           explvlist = (ExpandableListView) findViewById(R.id.ParentLevel222);

    }

    public class ParentLevel extends BaseExpandableListAdapter {
        @Override
        public Object getChild(int arg0, int arg1)
        {
               return arg1;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition)
        {

               return childPosition;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent)
        {

Log.e("grp first",String.valueOf(groupPosition));
                View v = null;
                if( convertView != null )
                    v = convertView;
                else
                    v = inflater.inflate(R.layout.child_row1, parent, false); 
                    TextView colorGroup = (TextView)v.findViewById( R.id.grp_child );
                    colorGroup.setText(subcats.get(groupPosition)[childposition]);
                    Log.e("kkk",String.valueOf(k));
                      return v;
            //CustExpListview SecondLevelexplv = new //CustExpListview(MainActivity.this);
           // SecondLevelexplv.setAdapter(new SecondLevelAdapter());
            //SecondLevelexplv.setGroupIndicator(null);
            //return SecondLevelexplv;
        }

        @Override
        public int getChildrenCount(int groupPosition)
        {
               Log.e("ss",String.valueOf(subcats1.get(groupPosition).length));
               return subcats1.get(groupPosition).length;

        }

        @Override
        public Object getGroup(int groupPosition) 
        {
               return groupPosition;
        }

        @Override
        public int getGroupCount()
        {
               return jarray.length();
        }

        @Override
        public long getGroupId(int groupPosition)
        {
            Log.e("group pos",String.valueOf(groupPosition));
               return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
        {
            Log.e("group",String.valueOf(groupPosition));
            View v = null;
             if( convertView != null )
                 v = convertView;
             else
                 v = inflater.inflate(R.layout.group_row, parent, false); 
                TextView colorGroup = (TextView)v.findViewById( R.id.row_name );
                colorGroup.setText(top_cats[groupPosition]);

                return v;
        }

        @Override
        public boolean hasStableIds() {
               return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
               return true;
        }
 }

 public class CustExpListview extends ExpandableListView {

        int intGroupPosition, intChildPosition, intGroupid;

        public CustExpListview(Context context) 
        {

               super(context);
               Log.e("intGroupPostion",String.valueOf(intGroupPosition));   
        }

        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
               widthMeasureSpec = MeasureSpec.makeMeasureSpec(960,
                            MeasureSpec.AT_MOST);
               heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,
                            MeasureSpec.AT_MOST);
               super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
 }

 public class SecondLevelAdapter extends BaseExpandableListAdapter 
 {
     int k=0;
        @Override
        public Object getChild(int groupPosition, int childPosition) {
               return childPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition)
        {

               return childPosition;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
               TextView tv = new TextView(MainActivity.this);
               tv.setText("child");
               tv.setTextColor(Color.BLACK);
               tv.setTextSize(20);
               tv.setPadding(15, 5, 5, 5);
               tv.setBackgroundColor(Color.YELLOW);
               tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
               return tv;
        }

        @Override
        public int getChildrenCount(int groupPosition)
        {
               return 5;
        }

        @Override
        public Object getGroup(int groupPosition)
        {
               return groupPosition;
        }

        @Override
        public int getGroupCount() {
               return 1;
        }

        @Override
        public long getGroupId(int groupPosition)
        {
               return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
        {
            View v=null;
                return v;
       }

        @Override
        public boolean hasStableIds() {
               // TODO Auto-generated method stub
               return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
               // TODO Auto-generated method stub

               return true;
        }

 }

 public class as extends AsyncTask
    {

        public as(int aa)
        {

        }

        @Override
        protected Object doInBackground(Object... params) 
        {

            try {


                   URL twitter = new URL("https://testaws.bigbasket.com/mapi/v0.0.1/browse-category/");
                   System.out.println(twitter);
                   URLConnection tc = twitter.openConnection();
                   BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
                   String line;

                   while ((line = in.readLine()) != null) 
                   {  
                       jarray=new JSONArray(line);
                       top_cats=new String[jarray.length()];
                       subcats1 = new ArrayList<String[]>();
                       for (int i = 0; i < jarray.length(); i++)
                       {
                       JSONObject jo = (JSONObject) jarray.get(i);
                       String top=(jo.getString("top_category"));
                       jk =new JSONArray(jo.getString("sub_cats"));
                       int comat=top.indexOf(",");
                       top=top.substring(1, comat);
                       top=top.replace("<br\\/>", "");
                       top=top.replaceAll(""+'"'+"", "");
                       Log.e("tt",top);
                       top_cats[i]=top;
                       sub_cats=new String[jk.length()];

                      for(int j=0;j<jk.length();j++)
                      {
                          Log.e("de",String.valueOf(j));
                          JSONObject jo1 = (JSONObject) jk.get(j);
                          String top1=(jo1.getString("sub_category"));
                          sub_cats[j]=top1;
                          Log.e("dd",String.valueOf(sub_cats[j]));

                      }
                   subcats1.add(i,sub_cats);

                      }


        }
            }
            catch(Exception e)
            {


            }
            return null;
        }
        @Override
        protected void onPostExecute(Object result)
        {
             explvlist.setAdapter(new ParentLevel());


        }

        @Override
        protected void onPreExecute()
        {}

    }




}

请帮我 。提前致谢....

json数组

[
    {
        "top_category": [
            "Fruits & <br/> Vegetables",
            "fruits-vegetables"
        ],
        "sub_cats": [
            {
                "cats": [],
                "sub_category": [
                    "Frozen Vegetables",
                    "frozen-veg"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Apple",
                            "apple"
                        ]
                    },
                    {
                        "cat": [
                            "Banana",
                            "bananas"
                        ]
                    },
                    {
                        "cat": [
                            "Exotic Fruits",
                            "exotic-fruits"
                        ]
                    },
                    {
                        "cat": [
                            "Melon",
                            "melon"
                        ]
                    },
                    {
                        "cat": [
                            "Oranges & Sweet Lime",
                            "citrus-fruits"
                        ]
                    },
                    {
                        "cat": [
                            "Other Fruits",
                            "other-fruits"
                        ]
                    }
                ],
                "sub_category": [
                    "Fruits",
                    "fruits"
                ]
            },
            {
                "cats": [],
                "sub_category": [
                    "Organic F&V",
                    "organic-fv"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Beans",
                            "beans"
                        ]
                    },
                    {
                        "cat": [
                            "Brinjals",
                            "brinjals"
                        ]
                    },
                    {
                        "cat": [
                            "Chilli, Lemon, Garlic & Ginger",
                            "chilli-lemon-garlic-ginger"
                        ]
                    },
                    {
                        "cat": [
                            "Exotic Vegetables",
                            "exotic-vegetables"
                        ]
                    },
                    {
                        "cat": [
                            "Gourd & Cucumber",
                            "gourds-cucumbers"
                        ]
                    },
                    {
                        "cat": [
                            "Leafy Vegetables",
                            "leafy-vegetables"
                        ]
                    },
                    {
                        "cat": [
                            "Other Vegetables",
                            "other-vegetables"
                        ]
                    },
                    {
                        "cat": [
                            "Potato, Onion & Tomato",
                            "potato-onion-tomato"
                        ]
                    },
                    {
                        "cat": [
                            "Root Vegetables",
                            "root-vegetables"
                        ]
                    }
                ],
                "sub_category": [
                    "Vegetables",
                    "vegetables"
                ]
            }
        ]
    },

    {
        "top_category": [
            "Bread <br/> Dairy & Eggs",
            "bread-dairy-eggs"
        ],
        "sub_cats": [
            {
                "cats": [
                    {
                        "cat": [
                            "Bread",
                            "breads"
                        ]
                    },
                    {
                        "cat": [
                            "Buns & Pavs",
                            "buns-cakes"
                        ]
                    },
                    {
                        "cat": [
                            "Cakes",
                            "cakes"
                        ]
                    }
                ],
                "sub_category": [
                    "Bread & Bakery",
                    "bread-bakery"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Butter & Cream",
                            "butter"
                        ]
                    },
                    {
                        "cat": [
                            "Cheese",
                            "cheese"
                        ]
                    },
                    {
                        "cat": [
                            "Condensed Milk",
                            "condensed-milk"
                        ]
                    },
                    {
                        "cat": [
                            "Curds & Buttermilk",
                            "curds-buttermilk"
                        ]
                    },
                    {
                        "cat": [
                            "Milk",
                            "milk"
                        ]
                    },
                    {
                        "cat": [
                            "Milk Drinks",
                            "milk-milk-drinks"
                        ]
                    },
                    {
                        "cat": [
                            "Paneer & Tofu",
                            "paneer-tofu"
                        ]
                    },
                    {
                        "cat": [
                            "Soya Drinks",
                            "soya-drinks"
                        ]
                    },
                    {
                        "cat": [
                            "Yogurt & Lassi",
                            "yogurt-lassi"
                        ]
                    }
                ],
                "sub_category": [
                    "Dairy Products",
                    "dairy-products"
                ]
            }
        ]
    },


    {
        "top_category": [
            "Household",
            "household"
        ],
        "sub_cats": [
            {
                "cats": [
                    {
                        "cat": [
                            "Brooms",
                            "brooms"
                        ]
                    },
                    {
                        "cat": [
                            "Cleaning Agents",
                            "cleaning-agents"
                        ]
                    },
                    {
                        "cat": [
                            "Tissues",
                            "tissues"
                        ]
                    }
                ],
                "sub_category": [
                    "Cleaning Articles",
                    "cleaning-articles"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Washing Bars",
                            "washing-bars"
                        ]
                    },
                    {
                        "cat": [
                            "Washing Liquids",
                            "washing-liquids"
                        ]
                    },
                    {
                        "cat": [
                            "Washing Powders",
                            "washing-powders"
                        ]
                    }
                ],
                "sub_category": [
                    "Detergents",
                    "detergents"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Batteries/Cells",
                            "batteriescells"
                        ]
                    },
                    {
                        "cat": [
                            "CFL's",
                            "cfls"
                        ]
                    }
                ],
                "sub_category": [
                    "Electricals",
                    "electricals"
                ]
            },
            {
                "cats": [],
                "sub_category": [
                    "Garbage Bags",
                    "garbage-bags"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Disposable Tableware",
                            "disposable-tableware"
                        ]
                    },
                    {
                        "cat": [
                            "Other Kitchenware",
                            "other-kitchenware"
                        ]
                    }
                ],
                "sub_category": [
                    "Kitchenware",
                    "kitchenware"
                ]
            },
            {
                "cats": [],
                "sub_category": [
                    "Pet Food",
                    "pet-food"
                ]
            },
            {
                "cats": [],
                "sub_category": [
                    "Plasticware",
                    "plasticware"
                ]
            },
            {
                "cats": [],
                "sub_category": [
                    "Pooja Needs",
                    "pooja-needs"
                ]
            },
            {
                "cats": [],
                "sub_category": [
                    "Shoe Care",
                    "shoe-care"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Other Stationery",
                            "other-stationery"
                        ]
                    },
                    {
                        "cat": [
                            "Printing Accessories",
                            "printing-accessories"
                        ]
                    }
                ],
                "sub_category": [
                    "Stationery",
                    "stationery"
                ]
            }
        ]
    },
    {
        "top_category": [
            "Confectionery",
            "confectionery"
        ],
        "sub_cats": [
            {
                "cats": [],
                "sub_category": [
                    "Biscuits",
                    "biscuits"
                ]
            },
            {
                "cats": [],
                "sub_category": [
                    "Chocolates",
                    "chocolates"
                ]
            },
            {
                "cats": [],
                "sub_category": [
                    "Cookies",
                    "cookies"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Gums & Freshners",
                            "gums-freshners"
                        ]
                    },
                    {
                        "cat": [
                            "Sweets",
                            "sweets"
                        ]
                    },
                    {
                        "cat": [
                            "Toffees",
                            "toffees"
                        ]
                    }
                ],
                "sub_category": [
                    "Sweets & Candies",
                    "sweets-candies"
                ]
            },
            {
                "cats": [],
                "sub_category": [
                    "Wafers",
                    "wafers"
                ]
            }
        ]
    },
    {
        "top_category": [
            "Meat",
            "meat"
        ],
        "sub_cats": [
            {
                "cats": [
                    {
                        "cat": [
                            "Fresh Chicken",
                            "fresh-chicken"
                        ]
                    }
                ],
                "sub_category": [
                    "Chicken",
                    "chicken"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Fresh Mutton",
                            "fresh-mutton"
                        ]
                    }
                ],
                "sub_category": [
                    "Mutton",
                    "mutton"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Fresh",
                            "fresh"
                        ]
                    }
                ],
                "sub_category": [
                    "Pork & Other Meats",
                    "pork-other-meats"
                ]
            },
            {
                "cats": [
                    {
                        "cat": [
                            "Fish",
                            "fish"
                        ]
                    },
                    {
                        "cat": [
                            "Other Seafood",
                            "other-seafood"
                        ]
                    }
                ],
                "sub_category": [
                    "Seafood",
                    "seafood"
                ]
            }
        ]
    }
]

我改变了我的代码。现在我得到了第二级。但是第三层有一个问题。我无法创建groupView()到第二级。因此我无法建立第三级视图。请提前帮助谢谢

4

0 回答 0