Android ExpandableListView
Recently I'm trying to use android's expandable list view, so I googled around and stumbled upon this
http://about-android.blogspot.com/2010/04/steps-to-implement-expandablelistview.html
I followed the steps almost exactly, but I created a file just to contain implementation of my own adapter. Basically on my onCreate method of the main activity I call:
mEntries = findViewById(R.id.entries);
ExpandableListAdapter adapter = new MyExpandableListAdapter(this);
mEntries.setAdapter(adapter);
Here's the code (Taken from above URL and modified):
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private String[] groups = {"Vehicle", "Baj"};
private String[][] children = {
{"Mol", "Mor"},
{"In", "Ruh"}
};
private Context cxt;
public MyExpandableListAdapter(Context cxt) {
this.cxt = cxt;
}
@Override
public Object getChild(int groupPos, int childPos) {
return children[groupPos][childPos];
}
@Override
public long getChildId(int groupPos, int childPos) {
return childPos;
}
@Override
public View getChildView(int groupPos, int childPos,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView tv = getGenericView();
tv.setText(getChild(groupPos, childPos).toString());
return tv;
}
@Override
public int getChildrenCount(int groupPos) {
return children[groupPos].length;
}
@Override
public Object getGroup(int groupPos) {
return groups[groupPos];
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int groupPos) {
return groupPos;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView tv = new TextView(this.cxt);
tv.setLayoutParams(lp);
// Center the text vertically
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
tv.setPadding(36, 0, 0, 0);
return tv;
}
@Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView tv = getGenericView();
tv.setText(getGroup(groupPos).toString());
return null;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPos, int childPos) {
return true;
}
}
The logcat spit these:
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): java.lang.NullPointerException
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.AbsListView.obtainView(AbsListView.java:1276)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.ListView.makeAndAddView(ListView.java:1668)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.ListView.fillDown(ListView.java:637)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.ListView.fillFromTop(ListView.java:694)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.ListView.layoutChildren(ListView.java:1521)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.view.View.layout(View.java:6830)
12-01 22:24:54.883: ERROR/An开发者_运维问答droidRuntime(14424): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:900)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.view.View.layout(View.java:6830)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.view.View.layout(View.java:6830)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.view.View.layout(View.java:6830)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424): at android.view.View.layout(View.java:6830)
snip...
Anyone care to shed a light on this? =/
EDIT: I've already implemented the methods required in the Base class.
EDIT2: Code added
Finally got it... it's at:
@Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView tv = getGenericView();
tv.setText(getGroup(groupPos).toString());
return null;
}
It's returning null... I seriously shouldn't code when I'm sleepy =/
When you extend BaseExpandableListAdapter, you still need to give an implementation on how to show each row. Without it, I think the current impl is to return null which is what you are seeing. So I would also implement:
public int getGroupCount()
public int getChildrenCount(int groupPosition)
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
I implemented other functions too but I didn't show them. Hope this helps.
精彩评论