Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:31:06+00:00 2026-05-30T04:31:06+00:00

I am using an ExpandableListView in which each group is a TextView and each

  • 0

I am using an ExpandableListView in which each group is a TextView and each child is a CheckBox. I was trying to implement something similar to this link :

http://www.vogella.de/articles/AndroidListView/article.html#listadvanced_interactive

but with a ExpandableListView instead of ListView.
At runtime, it is giving a strange ClassCastException while setting the Tag for the checkbox using setTag(). Here is the code :

public class ExpandableActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ArrayList<String> groups = new ArrayList<String>();
    List<List<OptionItem>> children = new ArrayList<List<OptionItem>>();

    for (int i = 0; i < 10; i++) {
        String groupName = "Group " + i;
        groups.add(groupName);
        List<OptionItem> temp = new ArrayList<OptionItem>();
        for (int j = 0; j < 5; j++) {
            temp.add(new OptionItem(groupName, "Child " + j));
        }
        children.add(temp);
    }

    children.get(0).get(3).setSelected(true);
    children.get(5).get(2).setSelected(true);
    children.get(8).get(1).setSelected(true);
    children.get(3).get(4).setSelected(true);

    ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView1);
    elv.setAdapter(new ExpandableAdapter(groups, children));

}

public static class ViewHolder {
    protected CheckBox cb;
}

public class ExpandableAdapter extends BaseExpandableListAdapter {

    private List<List<OptionItem>> list;
    private ArrayList<String> groups;

    public ExpandableAdapter(ArrayList<String> groups,
            List<List<OptionItem>> children) {
        this.groups = groups;
        this.list = children;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (list != null && list.size() > groupPosition
                && list.get(groupPosition) != null) {
            if (list.get(groupPosition).size() > childPosition)
                return list.get(groupPosition).get(childPosition);
        }

        return null;
    }

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

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

        View view = null;
        if (convertView == null) {
            view = getLayoutInflater().inflate(R.layout.row, null);
            final ViewHolder childHolder = new ViewHolder();
            childHolder.cb = (CheckBox) view.findViewById(R.id.checkBox1);              
            childHolder.cb
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton button,
                                boolean isChecked) {
                            OptionItem item = (OptionItem) childHolder.cb
                                    .getTag();
                            item.setSelected(button.isChecked());
                        }
                    });

            view.setTag(childHolder);
            childHolder.cb.setTag(list.get(groupPosition).get(childPosition));
        } else {
            view = convertView;         
            ((ViewHolder) view.getTag()).cb.setTag(list.get(groupPosition).get(childPosition));
        }

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.cb.setChecked(list.get(groupPosition).get(childPosition)
                .isSelected());
        holder.cb.setText(list.get(groupPosition).get(childPosition)
                .getChildName());

        return view;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (list != null && list.size() > groupPosition
                && list.get(groupPosition) != null)
            return list.get(groupPosition).size();

        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (groups != null && groups.size() > groupPosition)
            return groups.get(groupPosition);

        return null;
    }

    @Override
    public int getGroupCount() {
        if (groups != null)
            return groups.size();

        return 0;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(
                    android.R.layout.simple_expandable_list_item_1, null);
        }

        TextView tv = (TextView) convertView
                .findViewById(android.R.id.text1);
        tv.setText(groups.get(groupPosition));

        return convertView;
    }

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

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

and the data model class :

public class OptionItem {

private String groupName;
private String childName;
private boolean selected;

public OptionItem(String group, String child) {

    this.groupName = group;
    this.childName = child;
    selected = false;
}

public void setGroupName(String name) {
    this.groupName = name;
}

public void setChildName(String name) {
    this.childName = name;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

public String getGroupName() {
    return groupName;
}

public String getChildName() {
    return childName;
}

public boolean isSelected() {
    return selected;
}

Please help. Thanks in advance.

EDIT : The log is as follows:

  02-15 12:40:03.224: E/AndroidRuntime(423): FATAL EXCEPTION: main
02-15 12:40:03.224: E/AndroidRuntime(423): java.lang.ClassCastException: com.test.OptionItem
02-15 12:40:03.224: E/AndroidRuntime(423):  at com.test.ExpandableActivity$ExpandableAdapter.getChildView(ExpandableActivity.java:106)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:450)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.ListView.makeAndAddView(ListView.java:1727)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.ListView.fillDown(ListView.java:652)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.ListView.fillSpecific(ListView.java:1284)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.ListView.layoutChildren(ListView.java:1558)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.AbsListView.onLayout(AbsListView.java:1147)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.view.View.layout(View.java:7035)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.view.View.layout(View.java:7035)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.view.View.layout(View.java:7035)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.view.View.layout(View.java:7035)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.view.View.layout(View.java:7035)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.os.Looper.loop(Looper.java:123)
02-15 12:40:03.224: E/AndroidRuntime(423):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-15 12:40:03.224: E/AndroidRuntime(423):  at java.lang.reflect.Method.invokeNative(Native Method)
02-15 12:40:03.224: E/AndroidRuntime(423):  at java.lang.reflect.Method.invoke(Method.java:521)
02-15 12:40:03.224: E/AndroidRuntime(423):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-15 12:40:03.224: E/AndroidRuntime(423):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-15 12:40:03.224: E/AndroidRuntime(423):  at dalvik.system.NativeStart.main(Native Method)

EDIT: I have tried it again, it is still giving the same exception. Here are the imports I have added:

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.TextView;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:31:08+00:00Added an answer on May 30, 2026 at 4:31 am

    Thanks to Natali, I have figured out the problem. The problem was in the layout file “row.xml”. I was using :

    <?xml version="1.0" encoding="utf-8"?>
    <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
    

    as the layout. But when I changed it to :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CheckBox" />
    
    </LinearLayout>
    

    It worked like a charm. I guess this is a bug.
    Thanks to Natali for helping me in figuring this out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get something like this: http://img202.imageshack.us/img202/552/layoutoy.png . I'm using this as a
I am using an ExpandableListView, same way they do in this sample code: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html
I'm using ExpandableListView with a SimpleCursorTreeAdapter. Is it possible to get the group number
I'm new to this ExpandableListView and I desperately need your help please. I'm using
I've got this RelativeLayout I am using: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=match_parent
I am trying to get the primary key _id from the selected group using
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
I need to get item's index(position) knowing its ID while using ExpandableListView. Is there
I try to swap expandablelistview group background image when it is collapse or expand.
I have a simple ExpandalbeList. For the group header view, I'm using a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.