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/AndroidRuntime(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

Answers


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 =/


Need Your Help

How to put the retrieved database value inside the javascript

javascript php jquery ajax

I am retrieved value from the database,i want to put my retrieved data in the JavaScript,i am storing my retrieved value like this $data1 =25 , in my JavaScript code instead of data: 50 i want to...

Capture input from barcode scanner, write to a browser window

osx applescript barcode-scanner

Our reception has a barcode scanner plugged into a Macbook Mini, which is used by the receptionist. When people 'check in', they use the barcode scanner to scan their membership card, which then wr...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.