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 3442334
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:39:21+00:00 2026-05-18T08:39:21+00:00

I am trying to create an ExpandableListView with selectable elements(children) so that the selections

  • 0

I am trying to create an ExpandableListView with selectable elements(children) so that the selections don’t disappear after a group is closed or the selected elements are scrolled away.
I haven’t found any native solution for this so I’ve tryed to save the selected state of the elements in a seperate list and change the state and color of the elements in an onChildClickListner. The problem is – I can’t get the Listner to work. It just won’t go inside. However I can set a Listner on each child seperately. This does work but doesn’t solve my problem because in this case I don’t get any information about the position of a child in group.
It’s probably just a small mistake.. here is the code:

p.s. some variable names are in German, sorry for that =)
p.p.s. I’d be grateful for any hints!

package com.example.android.apis;

import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;

public class ApiDemos extends ExpandableListActivity {

    // This one works just fine if i set it for each child
    private View.OnClickListener cl1 = new View.OnClickListener() {
        public void onClick(View v) {
            if (((TextView) v).isSelected() == false) {

                ((TextView) v).setSelected(true);
                ((TextView) v).setTextColor(RED);
            } else {
                ((TextView) v).setSelected(false);
                ((TextView) v).setTextColor(WHITE);
            }
        }
    };

    // each child has a relation with one of these:
    class veranstaltungView {
        public String text;
        public boolean marked;

        public veranstaltungView(String txt) {
            text = txt;
            marked = false;
        }
    }

    // This one is set on the whole ExpandableListView. It just doesnt get
    // inside!
    private OnChildClickListener cl = new OnChildClickListener() {

        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

            if (((TextView) v).isSelected() == false) {

                veranstaltungenList.get(groupPosition).get(childPosition).marked = true;
                ((TextView) v).setSelected(true);
                ((TextView) v).setTextColor(RED);
            } else {
                veranstaltungenList.get(groupPosition).get(childPosition).marked = false;
                ((TextView) v).setSelected(false);
                ((TextView) v).setTextColor(WHITE);
            }
            return false;
        }
    };

    private static final int WHITE = 0xffffffff;
    private static final int RED = 0xffbc0000;

    ExpandableListAdapter mAdapter;
    private ArrayList<ArrayList<veranstaltungView>> veranstaltungenList;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // generate some elements
        veranstaltungenList = new ArrayList<ArrayList<veranstaltungView>>();
        for (int i = 0; i < 6; i++) {
            ArrayList<veranstaltungView> semester = new ArrayList<veranstaltungView>();
            for (int j = 0; j < 3; j++) {
                semester.add(new veranstaltungView("Veranstaltung " + i + "."
                        + j));
            }
            veranstaltungenList.add(semester);
        }

        // Create and set the adapter
        ExpandableListView elv = getExpandableListView();
        mAdapter = new MyExpandableListAdapter();
        setListAdapter(mAdapter);

        elv.setItemsCanFocus(false);
        elv.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);

        elv.setOnChildClickListener(cl);
    }

    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
        // The groups
        private String[] semesterArray = { "SEMESTER1", "SEMESTER2",
                "SEMESTER3", "SEMESTER4", "SEMESTER5", "SEMESTER6" };

        public Object getChild(int groupPosition, int childPosition) {
            return veranstaltungenList.get(groupPosition).get(childPosition).text;
        }

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

        public int getChildrenCount(int groupPosition) {
            return veranstaltungenList.get(groupPosition).size();
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, 64);
            TextView textView = new TextView(ApiDemos.this);

            textView.setTextColor(WHITE);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }

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

            TextView textView = null;
            textView = getGenericView();
            textView.setText((String) getChild(groupPosition, childPosition));
            textView.setClickable(true);
            textView.setSelected(veranstaltungenList.get(groupPosition).get(
                    childPosition).marked);

            if (veranstaltungenList.get(groupPosition).get(childPosition).marked == true)
                textView.setTextColor(RED);
            else
                textView.setTextColor(WHITE);

            // textView.setOnClickListener(cl1);

            return textView;
        }

        public Object getGroup(int groupPosition) {
            return semesterArray[groupPosition];
        }

        public int getGroupCount() {
            return veranstaltungenList.size();
        }

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

        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }

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

        public boolean hasStableIds() {
            return true;
        }
    }
}
  • 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-18T08:39:22+00:00Added an answer on May 18, 2026 at 8:39 am

    The problem was “textView.setClickable(true);” in the getChildView method that prevented the OnClickListener to be executed. Also the line “if (((TextView) v).isSelected() == false)” should be changed to “veranstaltungenList.get(groupPosition).get(childPosition).marked“.
    After those changes worked like charm! It maybe not the best solution but it works. Hope this helps somebody!

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

Sidebar

Related Questions

I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
I am trying to create custom background selector for my ExpandableListView. It works fine
I am trying create a new frame in wxPython that is a child of
Trying to create an app that does some socket communication (Writing only). I can
Trying to create a grails ant task that has other environments besides prod for
Trying to create a simple plugin that simply connects to an ftp site, looks
Im trying to create LinqDataSource that will represent data from dynamically created String list
I am trying to create a list similar to that in the call log

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.