-1

I have an SQLite database with multiple columns, and one of these columns is a date. I want to populate the data from the database to an expandable listview, with the dates as the headings. So for all the rows that share the same date value, I want to display them as children to that specific date.

I did it using arraylist to store the data from the database. I got the group headings showing up but I get null pointer exception when I try to expand. The error comes from the getChildView method.

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

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

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

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        RelativeLayout infoLayout = (RelativeLayout) findViewById(R.id.infoLayout);
        String exercise = ((ArrayList<Object>) getChild(groupPosition, childPosition)).get(0).toString();
        String weight = ((ArrayList<Object>) getChild(groupPosition, childPosition)).get(1).toString();
        String reps = ((ArrayList<Object>) getChild(groupPosition, childPosition)).get(2).toString();
        ((TextView) infoLayout.findViewById(R.id.exercise)).setText(exercise);
        ((TextView) infoLayout.findViewById(R.id.weight)).setText(weight);
        ((TextView) infoLayout.findViewById(R.id.reps)).setText(reps);


        return infoLayout;
    }

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

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

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

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

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView dateView = new TextView(getApplicationContext());
        dateView.setText(getGroup(groupPosition).toString());
        return dateView;
    }

    public boolean hasStableIds() {
        return false;
    }

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

And this is how I populated the arraylists. The arraylist containing the children are nested twice: the innermost contains 3 objects to be displayed on the same row, and the "middle" one groups the innermost arraylists together so that they appear under the correct groupings for the expandable listview.

private DatabaseMethods dbm;
private ArrayList<String> dateList = new ArrayList<String>();
private ArrayList<ArrayList<ArrayList<Object>>> infoList = new ArrayList<ArrayList<ArrayList<Object>>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stats);
    dbm = new DatabaseMethods(this);
    dbm.read();
    Cursor dates = dbm.fetchGroup();
    int DATES_INDEX = dates.getColumnIndexOrThrow("date");
    while (dates.moveToNext()) {
        String date = dates.getString(DATES_INDEX);
        if (!dateList.contains(date)) {
            dateList.add(date);
        }
    }
    for (String date : dateList) {
        Cursor info = dbm.fetchChildren(date);
        int EXERCISE_INDEX = info.getColumnIndexOrThrow("exercise");
        int WEIGHT_INDEX = info.getColumnIndexOrThrow("weight");
        int REPS_INDEX = info.getColumnIndexOrThrow("reps");
        ArrayList<ArrayList<Object>> secondInnerInfoList = new ArrayList<ArrayList<Object>>();
        while (info.moveToNext()) {
            String exercise = info.getString(EXERCISE_INDEX);
            int weight = info.getInt(WEIGHT_INDEX);
            int reps = info.getInt(REPS_INDEX);
            ArrayList<Object> thirdInnerInfoList = new ArrayList<Object>();
            thirdInnerInfoList.add(exercise);
            thirdInnerInfoList.add(weight);
            thirdInnerInfoList.add(reps);
            secondInnerInfoList.add(thirdInnerInfoList);
        infoList.add(secondInnerInfoList);
        }

    }
CoffeeCrisp
  • 253
  • 3
  • 6
  • 19
  • Have you tried something? Show us a code you have tried and you will receive a help in case if you have any issues. – Marcin S. Dec 14 '12 at 00:55
  • Elaborate your problem. If your code has error, post it here. Otherwise you can search [google](http://goo.gl/uZ1D1) for sqlite tutorial or SO [question](http://stackoverflow.com/q/6760948/1050058). – Trung Nguyen Dec 14 '12 at 03:59
  • http://stackoverflow.com/questions/17132559/java-lang-nullpointerexception-displaying-records-from-database-in-an-expandab help needed .check out link ^ – user2468835 Jun 16 '13 at 10:57

3 Answers3

0
  1. Write a method to get and convert your data to an ArrayList or an array string.
  2. In your Activity declare and init data for ArrayAdapter like: ArrayAdapter mAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, yourArrayListOrArray);
  3. ListView.setAdapter(mAdapter);
VAdaihiep
  • 521
  • 9
  • 19
0

For expandable listview with sqlite database: Instead of using BaseExpandablelistAdapter you can use CursorTreeAdapter. This will be easy for you to achieve the task. Please refer the below link:

http://developer.android.com/reference/android/widget/CursorTreeAdapter.html

Dasari
  • 438
  • 4
  • 11
0
public View getChildView(int parentPosition, int childPosition, boolean isLastChild, View convertView,
        ViewGroup parent) {
    // TODO Auto-generated method stub


    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_view_list, null);
    }
    final String abc = (String) getChild(parentPosition, childPosition);

    TextView status_tv = (TextView) convertView
            .findViewById(R.id.child_tv);
    status_tv.setText(abc);

    //ImageView imgListChild=(ImageView) convertView.findViewById(R.id.child_iv);


    //imgListChild.set
    return convertView;
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211