0

I am doing expandable list view with three levels.But the first level is right.But second and third level children are not correct.Actually I am converting json into expandable listview. my code is below main activity

public class MainActivity extends Activity
{
    ExpandableListView explvlist;
    TextView ui_hot = null;
    int hot_number = 0;
   // private ExpandListAdapter ExpAdapter;
    private ArrayList<Group> ExpListItems;
    private ExpandableListView ExpandList;
   // private SliderLayout mDemoSlider;
    Intent intent = null;
    RequestQueue requestQueue1, requestQueue2;
    int[] firstcatid = null,secondcatid = null;
    String firsttitle[] = null,secondtitle[] = null;
    int j = 0;
    ArrayList<Group> list,list2,list3;
    CustExpListview SecondLevelexplv;
    ExpandableListView elv;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestQueue1 = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://www.fasr.com/cntrldata/ezproductfristcategory.php",
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {
                            list = new ArrayList<Group>();
                            JSONArray ja = response.getJSONArray("hi");
                            JSONArray ja2 = response.getJSONArray("hi2");
                            JSONArray ja3 = response.getJSONArray("hi3");
                            firsttitle = new String[ja.length()];
                            firstcatid = new int[ja.length()];
                            JSONObject jsonObject2=null,jsonObject3=null,jsonObject;
                            for (int i = 0; i < ja.length(); i++) {

                                jsonObject = ja.getJSONObject(i);


                                // Log.e("hie",jsonObject2.getInt("second_category_id")+"");
                                //jsonObject3 = ja3.getJSONObject(i);
                                firstcatid[i] = jsonObject.getInt("first_category_id");
                                firsttitle[i] = jsonObject.getString("first_category_name");
                            }
                            secondcatid=new int[ja2.length()];
                            for(int i =0;i<ja2.length();i++){
                                jsonObject2 = ja2.getJSONObject(i);
                                secondcatid[i]=jsonObject2.getInt("second_category_id");
                            }
                            for(int i = 0;i<ja.length();i++){
                                Group gru = new Group();
                                gru.setName(firsttitle[i]);
                                list2 = new ArrayList<Group>();
                                for(int j=0;j<ja2.length();j++){
                                    Group gru2 = new Group();
                                    jsonObject2 = ja2.getJSONObject(j);

                                    //Toast.makeText(getApplicationContext(),jsonObject2.getInt("second_category_id"),Toast.LENGTH_SHORT).show();
                                    if(firstcatid[i]==jsonObject2.getInt("first_category_id")){
                                        gru2.setName(jsonObject2.getString("second_category_name"));
                                        list3= new ArrayList<Group>();
                                        for(int k=0;k<ja3.length();k++){
                                            Group gru3= new Group();
                                            jsonObject3 = ja3.getJSONObject(k);
                                            if(secondcatid[j]==jsonObject2.getInt("second_category_id")){
                                                Log.e("hie", jsonObject2.getInt("second_category_id") + "");
                                                gru3.setName(jsonObject3.getString("third_category_name"));
                                                list3.add(gru3);
                                            }
                                            gru2.setItems(list3);
                                        }

                                        list2.add(gru2);

                                    }

                                }
                                gru.setItems(list2);
                                list.add(gru);
                            }

                            // Toast.makeText(getApplicationContext(),url[2],Toast.LENGTH_SHORT).show();
                            //output.setText(data);
                            ExpListItems =  list;
                            explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
                            explvlist.setAdapter(new ParentLevel(getApplicationContext(),ExpListItems,list2,list3));
                            //ExpAdapter = new ExpandListAdapter(getApplicationContext(), ExpListItems);
                           // ExpandList.setAdapter(ExpAdapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Volley", error.toString());

                    }
                }
        );
        requestQueue1.add(jsonObjectRequest);


    }

    public class ParentLevel extends BaseExpandableListAdapter {
        private Context context;
        private ArrayList<Group> groups,childgroup,childgroup2,a1;

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            ArrayList<Group> chList = groups.get(groupPosition).getItems();
            return chList.get(childPosition);
        }

        public ParentLevel(Context context, ArrayList<Group> groups,ArrayList<Group> groups2,ArrayList<Group> groups3) {
            this.context = context;
            this.groups = groups;
            this.childgroup=groups2;
            this.childgroup2=groups3;
        }

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

        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
          Group child = (Group) getChild(groupPosition, childPosition);
            if (convertView == null) {
               LayoutInflater infalInflater = (LayoutInflater) context
                        .getSystemService(context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.group_item, null);
            }
            //child.setItems(childgroup);
            //requestQueue1 = Volley.newRequestQueue(getApplicationContext());


           SecondLevelexplv = new CustExpListview(MainActivity.this);
            SecondLevelexplv.setAdapter(new SecondLevelAdapter(context, childgroup));
            SecondLevelexplv.setGroupIndicator(null);




            return SecondLevelexplv;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            ArrayList<Group> chList = groups.get(groupPosition).getItems();
            if(!(chList==null))
                return chList.size();
            else return 0;
        }

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

        @Override
        public int getGroupCount() {
            return groups.size();
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent){

        Group group = (Group) getGroup(groupPosition);
        if(convertView==null)

        {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item, null);
        }

        TextView tv = (TextView) convertView.findViewById(R.id.group_name);
        tv.setPadding(10, 7, 7, 7);

            tv.setText(group.getName());
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
        if(!(group.getItems()==null))

        {
            if (!group.getItems().isEmpty()) {
                if (isExpanded) {
                    tv.setTypeface(null, Typeface.BOLD);
                    imageView.setBackgroundResource(R.drawable.ic_action_minus);
                } else {
                    imageView.setBackgroundResource(R.drawable.ic_action_add);
                    tv.setTypeface(null, Typeface.NORMAL);
                }
            } else imageView.setVisibility(View.INVISIBLE);
        }

        return convertView;


    }

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

        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 i = 0;
        private Context context;
        private ArrayList<Group> groups;
        private int gr;

        public SecondLevelAdapter(Context context, ArrayList<Group> groups) {
            this.context = context;
            this.groups = groups;


        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            ArrayList<Group> chList = groups.get(groupPosition).getItems();
            return chList.get(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) {

            Group child = (Group) getChild(groupPosition, childPosition);

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context
                        .getSystemService(context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.group_item, null);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.group_name);

            tv.setText(child.getName().toString());

            //Toast.makeText(context,child.getName().toString()+"hi",Toast.LENGTH_SHORT).show();
            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            ArrayList<Group> chList = groups.get(groupPosition).getItems();
            if (!(chList == null))
                return chList.size();
            else return 0;
        }

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

        @Override
        public int getGroupCount() {
            return groups.size();
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            Group group = (Group) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater inf = (LayoutInflater) context
                        .getSystemService(context.LAYOUT_INFLATER_SERVICE);
                convertView = inf.inflate(R.layout.group_item, null);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.group_name);

            tv.setText(group.getName());
            ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
            if (!(group.getItems() == null)) {
                if (!group.getItems().isEmpty()) {
                    if (isExpanded) {
                        tv.setTypeface(null, Typeface.BOLD);
                        imageView.setBackgroundResource(R.drawable.ic_action_minus);
                    } else {
                        imageView.setBackgroundResource(R.drawable.ic_action_add);
                        tv.setTypeface(null, Typeface.NORMAL);
                    }
                } else imageView.setVisibility(View.INVISIBLE);
            }

            return convertView;
        }

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

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

What is I got is this enter image description here enter image description here

Kiran Benny Joseph
  • 6,271
  • 3
  • 34
  • 55

1 Answers1

0

finally I got my answer in parentlevel adapter change

 public ArrayList<Group> getChild(int groupPosition, int childPosition) {
            ArrayList<Group> chList = groups.get(groupPosition).getItemz();
            return chList;
        }

and in the inner most for loop change the if condition to

secondcatid[j]==jsonObject3.getInt("second_category_id")
Kiran Benny Joseph
  • 6,271
  • 3
  • 34
  • 55