Reputation: 45
I have to admit I'm a newbie in Android and my ExpandableListView is causing me a lot of trouble. I've figured it's the adapter which isn't working properly. I'm using a class which extends BaseExpandableListAdapter.
From logging I know this much: The list I hand over to the constructor of the adapter is filled correctly. getGroupCount returns 1 (yes, there's only 1 group) and getChildrenCount returns 18 (as expected). The method getGroupView is called (Log.d...) but getChildView isn't - not when logging at the very start of this method and not while debugging (didn't reach the breaking point). There are no error messages - it's just the ExpandableListView quietly not expanding.
Any idea what went wrong? Do I need a ViewHolder or is it not inflating the group properly? I'm totally lost...
Part of my code:
package de.cimitery.android.cimitery;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class ExpandableAdapter extends BaseExpandableListAdapter {
private List<GroupCat> catList;
private Context ctx;
NewGraveActivity app;
public ExpandableAdapter(List<GroupCat> catList, Context ctx, NewGraveActivity app) {
this.catList = catList;
this.ctx = ctx;
this.app = app;
}
@Override
public Object getGroup(int groupPosition) {
return catList.get(groupPosition);
}
@Override
public int getGroupCount() {
Log.d("ExAdapter getGroupCount", "" + catList.size());
return catList.size();
}
@Override
public long getGroupId(int groupPosition) {
return catList.get(groupPosition).hashCode();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Log.d("ExAdapter getGroupView", "Start");
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.group_layout, parent, false);
}
TextView groupName = (TextView) v.findViewById(R.id.groupName);
GroupCat cat = catList.get(groupPosition);
groupName.setText(cat.getGroupName());
return v;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return catList.get(groupPosition).getItemList().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return catList.get(groupPosition).getItemList().get(childPosition).hashCode();
}
@Override
public View getChildView(final int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Log.d("ExAdapter getChildView", "Start");
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.item_layout, parent, false);
}
CheckBox itemCheck = (CheckBox) v.findViewById(R.id.itemCheck);
TextView itemName = (TextView) v.findViewById(R.id.itemName);
if (app.selected.containsKey((groupPosition))==true)
itemCheck.setChecked(true);
else
itemCheck.setChecked(false);
Category child = catList.get(groupPosition).getItemList().get(childPosition);
Log.d("ExAdapter getChildView", child.getName());
itemName.setText(child.getName());
itemCheck.setOnCheckedChangeListener(new CheckListener(childPosition, app));
return v;
}
@Override
public int getChildrenCount(int groupPosition) {
int size = catList.get(groupPosition).getItemList().size();
System.out.println("number of children for group ["+groupPosition+"] is ["+size+"]");
return size;
}
}
group-layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/groupName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dip" />
<TextView
android:id="@+id/groupDescr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="8dip" />
</LinearLayout>
item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/itemCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/itemName"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
layout with exp. listview:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp" >
</ExpandableListView>
<Button
android:id="@+id/buttonNewGrave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonNewGrave" />
</LinearLayout>
</ScrollView>
Upvotes: 3
Views: 2600
Reputation: 264
First thing: You should not use any scrolling component like expandable list, inside a scroll view here's why Listview inside ScrollView is not scrolling on Android.
Second thing: Height of ExpandableListView should be match_parent. In your case you have taken Expandable ListView within Linear Layout whose height is wrap_content, change it to match_parent.
I had faced the same problem and I resolved that problem by following the same.
Upvotes: 1