• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps cannot read value from a custom expandable list view

mwhubham

Lurker
Jun 18, 2015
1
0
I have a arraylist for generic multilevel expandable view.
Java:
package com.example.generic;

import java.util.ArrayList;

public class Category {
        ArrayList<Category> childs;
        private String name;
     
        /*Constructor 1*/
        public Category(String name) {
                this.name = name;
                childs = new ArrayList<Category>(0);
        }
       
        /*Constructor 2*/
        public Category(String name, ArrayList<Category> childs) {
                this.name = name;
                this.childs = childs;
        }
      
        public String getName() {
              return "";
        }
}

and a custom adapter with overrided method
Code:
package com.example.generic;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class CatAdapter extends BaseExpandableListAdapter {
        private Context context;
        private LayoutInflater inflater;
        private ArrayList<Category> categories;
      
        public CatAdapter(Context context, ArrayList<Category> categories) {
                this.context = context;
                inflater = LayoutInflater.from(context);
                this.categories = categories;
        }
      
        @Override
        public Object getChild(int groupPosition, int childPosition) {
                return categories.get(groupPosition).childs.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) {
              
                convertView = inflater.inflate(R.layout.catitem, parent, false);
                TextView textView_catName = (TextView)convertView.findViewById(R.id.textView_catName);
                Category current = categories.get(groupPosition).childs.get(childPosition);
               
                textView_catName.setText(groupPosition + " , " + childPosition);
               
                String value="";
               
//                textView_catName.setText(value);
               
               
                if(current.childs.size() > 0 ) {
                        ExpandableListView elv = new ExpandableListView(context);
                        elv.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,  AbsListView.LayoutParams.WRAP_CONTENT));
                        elv.setAdapter(new CatAdapter(context, current.childs));
                        ((ViewGroup)convertView).addView(elv);
                }
              
                return convertView;
        }
        @Override
        public int getChildrenCount(int groupPosition) {
                return categories.get(groupPosition).childs.size();
        }
        @Override
        public Object getGroup(int groupPosition) {
                return categories.get(groupPosition);
        }
        @Override
        public int getGroupCount() {
                return categories.size();
        }
        @Override
        public long getGroupId(int groupPosition) {
                return groupPosition;
        }
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                convertView = inflater.inflate(R.layout.catitem, parent, false);
                TextView textView_catName = (TextView)convertView.findViewById(R.id.textView_catName);
                Category current = categories.get(groupPosition);
//               
//                categories.get(groupPosition, parent);
//                textView_catName.setText(grpValue);
//               
               
                textView_catName.setText("" + groupPosition);
                return convertView;
        }
        @Override
        public boolean hasStableIds() {
                return false;
        }
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
                return false;
        }
}


but the problem is that it does not read properly. how to override them for n level expandable list view.
another problem is how to access value from arraylist on the basis of child and group.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones