Help me to solve expandable list view problem..

Jay Park's picture

Recently I'm studying about multi level(4-level) expandable list view using ArrayList.
But there's some problem related getGroupCount function.
Parent Level's getGroupCount function works well, but two child level's getGroupCount function works bad.

I wanna implement listview like below.

. a
. a1
. a11
. a111
. a112
. a12
. a2
. b
. b1
. c

But the rusult of my source program is below. (a112 not shown)

. a
. a1
. a11
. a111
. a12
. a2
. b
. b1
. c

Is there anyone who cah help me to solve this problem?

my main.xml code is like below. (No other layout file exists.)

<?xml version="1.0" encoding="utf-8"?>

And my java code is like below.

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class Studying_Sfonge extends Activity {
ExpandableListView explvlist;

private ArrayList oneLevel = new ArrayList();
private ArrayList twoLevel = new ArrayList();
private ArrayList threeLevel = new ArrayList();
private ArrayList fourLevel = new ArrayList();

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

oneLevel.add(0, new Place2("a"));
oneLevel.add(1, new Place2("b"));
oneLevel.add(2, new Place2("c"));

twoLevel.add(0, new Place2("a1"));
twoLevel.add(1, new Place2("b1"));
twoLevel.add(2, new Place2("a2"));

threeLevel.add(0, new Place2("a11"));
threeLevel.add(1, new Place2("a12"));

fourLevel.add(0, new Place2("a111"));
fourLevel.add(1, new Place2("a112"));

oneLevel.get(0).GroupPlace.add(0, twoLevel.get(0));
oneLevel.get(0).GroupPlace.add(1, twoLevel.get(2));
oneLevel.get(1).GroupPlace.add(0, twoLevel.get(1));
twoLevel.get(0).GroupPlace.add(0, threeLevel.get(0));
twoLevel.get(0).GroupPlace.add(1, threeLevel.get(1));
threeLevel.get(0).GroupPlace.add(0, fourLevel.get(0));
threeLevel.get(0).GroupPlace.add(1, fourLevel.get(1));

explvlist = (ExpandableListView) findViewById(R.id.ParentLevel);
explvlist.setAdapter(new ParentLevel());
}

public class ParentLevel extends BaseExpandableListAdapter {

public String getChild(int arg0, int arg1) {
return oneLevel.get(arg0).GroupPlace.get(arg1).getPlacename();
}

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

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

CustExpListview SecondLevelexplv = new CustExpListview(Studying_Sfonge.this);
SecondLevelexplv.setAdapter(new SecondLevelAdapter(groupPosition,childPosition));

return SecondLevelexplv;
}

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

public Object getGroup(int groupPosition) {
return oneLevel.get(groupPosition);
}

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

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

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

TextView tv = new TextView(Studying_Sfonge.this);
tv.setText(oneLevel.get(groupPosition).getPlacename());
tv.setTextColor(Color.WHITE);
tv.setTextSize(20);
tv.setBackgroundColor(Color.BLACK);
tv.setPadding(60, 7, 7, 7);

return tv;
}

public boolean hasStableIds() {
return true;
}

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

public class CustExpListview extends ExpandableListView {

int intGroupPosition, intChildPosition, intGroupid;

public CustExpListview(Context context) {
super(context);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960,
MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,
MeasureSpec.AT_MOST);

super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

public class SecondLevelAdapter extends BaseExpandableListAdapter {
private int childidx;
private int groupidx;

public SecondLevelAdapter(int groupPosition, int childPosition) {
// TODO Auto-generated constructor stub

this.groupidx = groupPosition;
this.childidx = childPosition;
}

public Object getChild(int groupPosition, int childPosition) {
return oneLevel.get(groupPosition).GroupPlace.get(childPosition);
}

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

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

CustExpListview ThirdLevelAdapter = new CustExpListview(Studying_Sfonge.this);
ThirdLevelAdapter.setAdapter(new ThirdLevelAdapter(groupPosition,childPosition));

return ThirdLevelAdapter;
}

public int getChildrenCount(int groupPosition) {
return oneLevel.get(groupidx).GroupPlace.get(childidx).GroupPlace.size();
}

public Object getGroup(int groupPosition) {
return groupPosition;
}

public int getGroupCount() {
if (oneLevel.get(groupidx).GroupPlace.size()==0 )
return 0;
else
return 1;
}

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

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

TextView tv = new TextView(Studying_Sfonge.this);

tv.setText(oneLevel.get(groupidx).GroupPlace.get(childidx).getPlacename());
tv.setTextColor(Color.WHITE);
tv.setTextSize(20);
tv.setPadding(90, 7, 7, 7);
tv.setBackgroundColor(Color.BLACK);

return tv;

}

public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}

public class ThirdLevelAdapter extends BaseExpandableListAdapter {
private int childidx;
private int groupidx;

public ThirdLevelAdapter(int groupPosition, int childPosition) {
// TODO Auto-generated constructor stub

this.childidx = childPosition;
}

public Object getChild(int groupPosition, int childPosition) {
return twoLevel.get(groupidx).GroupPlace.get(childidx);
}

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

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

TextView tv = new TextView(Studying_Sfonge.this);
tv.setText( twoLevel.get(groupidx).GroupPlace.get(childidx).GroupPlace.get(childPosition).getPlacename() );
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.GREEN);
tv.setLayoutParams(new ListView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return tv;
}

public int getChildrenCount(int groupPosition) {
return twoLevel.get(groupidx).GroupPlace.get(childidx).GroupPlace.size();
}

public Object getGroup(int groupPosition) {
return twoLevel.get(groupPosition);
}

public int getGroupCount() {
if (twoLevel.get(groupidx).GroupPlace.size()==0 )
return 0;
else
return 1;
}

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

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

TextView tv = new TextView(Studying_Sfonge.this);

tv.setText(twoLevel.get(groupidx).GroupPlace.get(childidx).getPlacename());
tv.setTextColor(Color.WHITE);
tv.setTextSize(20);
tv.setPadding(100, 7, 7, 7);
tv.setBackgroundColor(Color.BLACK);

return tv;
}

public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}

}

Taxonomy upgrade extras: 
Gabor Paller's picture

Your example is not complete, e.g. the Place2 class declaration is missing.
Could you export your project as an Eclipse archive and attach it to the post?