Android ExpandableListView does not expand/not clickable
Asked Answered
A

2

14

i'm pretty new to android programming and tried to implement a basic expandable listview showing a list of Courses which should show some additional details when clicked. i feel like the problem is very basic as i don't include anything fancy in any of the views, but i still could not find a solution online, most of the other questions are a lot more specific.

The first part works fine(displaying the list of courses), the problem is that the list is not clickable, meaning it does not expand.

what i have determined right now is the following:
1. the getChildView is never called(debugger never stops at breakpoint)
2. there is data for the children as it's the same data that's used for the groupheading which works fine.

I have also tried to set an onGroupClickListener which was never called, so i removed it again.

my code:

Group heading layout:

    <TextView
        android:id="@+id/CourseName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:text="CourseName"
        android:layout_weight="7"
        android:layout_marginRight="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:id="@+id/CourseDate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_weight="3"
        android:text="CourseDate" />


    <CheckBox
        android:id="@+id/checkBox"
        style="?android:attr/starStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

child_row layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/button1"
        android:text="Button" />

    <TextView
        android:id="@+id/phases"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:layout_below="@+id/button1"
        android:text="phases:" />

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/phases"
        android:gravity="center"
        android:text="information:" />

</RelativeLayout>

activity layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/sportName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="Sport Name"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="@dimen/profile_title" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:numStars="5"
        android:stepSize="1"
        android:layout_centerInParent="true" />

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/sportName" >
    </ExpandableListView>

</RelativeLayout>

Adapter java:

package ch.unibe.unisportbern.views.details;

import java.util.ArrayList;

import com.example.unisportbern.R;

import ch.unibe.unisportbern.support.Course;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class SportsAdapter extends BaseExpandableListAdapter  {
    private Context context;
    private ArrayList<Course> courseList;



    public SportsAdapter(Context context, ArrayList<Course> courseList) {
        this.context = context;
        this.courseList = courseList;
    }



    @Override
    public Object getChild(int index, int stub) {
        return courseList.get(index);
    }

    @Override
    public long getChildId(int index, int stub) {
        return stub;
    }

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

        if (convertView == null){
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               convertView = inflater.inflate(R.layout.child_row, null);
        }

        TextView phases = (TextView) convertView.findViewById(R.id.phases);
        TextView info = (TextView) convertView.findViewById(R.id.info);

        phases.setText("phases:\n" + courseList.get(groupPosition).getPhases());
        info.setText(courseList.get(groupPosition).getInformation());

        return convertView;
    }

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

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

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

    @Override
    public long getGroupId(int groupPosition) {
        return courseList.get(groupPosition).getId();
    }

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

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

        TextView courseName = (TextView) convertView.findViewById(R.id.CourseName);
        TextView courseDate = (TextView) convertView.findViewById(R.id.CourseDate);

        courseName.setText(courseList.get(groupPosition).getName());
        courseDate.setText(courseList.get(groupPosition).getDay() + courseList.get(groupPosition).getTime());

        return convertView;
    }

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

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

}

activity java:

package ch.unibe.unisportbern.views.details;

import java.util.ArrayList;

import com.example.unisportbern.R;

import ch.unibe.unisportbern.support.Course;
import ch.unibe.unisportbern.support.DBMethodes;
import ch.unibe.unisportbern.support.Sport;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class DActivity extends Activity{


    public final static String NAME = "SportName";
    public final static String ID = "SportID";

    private Sport sport;
    private ArrayList<Course> courses;
    private SportsAdapter sportsadapter;

    private ExpandableListView myList;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        getSport();
        getCourses();

        setContentView(R.layout.details_layout);

        myList = (ExpandableListView) findViewById(R.id.expandableListView);
        sportsadapter = new SportsAdapter(this, courses);
        myList.setAdapter(sportsadapter);
    }

    private void getCourses() {
        DBMethodes dbMethodes = new DBMethodes(this);
        try {
            courses = dbMethodes.getAllCourses(sport);
        } catch (Exception e) {
        }
    }

    private void getSport() {
        Intent intent = this.getIntent();
        int id = intent.getIntExtra(ID, 0);
        String name = intent.getStringExtra(NAME);
        this.sport = new Sport(id, name);
    }

}

thanks a lot in advance!

EDIT: i found it by chance. the checkbox inside the groupheader was focusable, stealing all the clicks away from the list. to solve this problem simply set the focusable attribute of the checkbox/button in your list to false.

Athematic answered 1/11, 2013 at 18:15 Comment(0)
A
44

I found it by chance. the checkbox inside the groupheader was focusable, stealing all the clicks away from the list. to solve this problem simply set the focusable attribute of the checkbox/button in your list to false with either:

in Java:

checkbox.setFocusable(false);

OR, in xml:

android:focusable="false"
Athematic answered 10/11, 2013 at 15:17 Comment(2)
you are a real life saver man. I wasted about 5 hours and re implemented listview three timesFelicefelicia
For some reason android:focusable="false" had no effect but setFocusable(false); did the trick. Thanks.Bookbindery
T
1

i was facing the same issue but what i added the below line to my Parent view

android:descendantFocusability="blocksDescendants"

and it worked for me.

Tamalatamale answered 9/2, 2017 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.