简体   繁体   中英

Android Expandable List from Sqlite

Are there any simple tutorials to help create an expandable list from sqlite data?

I have a table called categories, then a table called questions. If a category is clicked then the questions for that category need to be shown.

How would I pass the category id to the second cursor?

Any help appreciated.

I know this is a bit old, but since it's unanswered and I just figured this out for something I was doing, I'll take a whirl at giving you an answer.

The answer you are looking for lies in the adapter (specifically the overridden getChildrenCursor method). Basically your group cursor is getting passed in and then you extract the data from that to create the child cursor for each parent.

Some example code from my current project follows.

set the adapter

mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
        R.layout.explistlayout,
        R.layout.explistlayout,
        new String[] { "_id" },
        new int[] { R.id.text1 },
        new String[] { AttendanceDB.EVENT_NAME },
        new int[] { R.id.text1 });
lv.setAdapter(mAdapter);

adapter class

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

    public MyExpandableListAdapter(Cursor cursor, Context context,
            int groupLayout, int childLayout, String[] groupFrom,
            int[] groupTo, String[] childrenFrom, int[] childrenTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo,
                childLayout, childrenFrom, childrenTo);
    }
    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        Cursor childCursor = mDbHelper.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("_id")));
        getActivity().startManagingCursor(childCursor);
        childCursor.moveToFirst();
        return childCursor;
    }
}

Database methods

public Cursor fetchGroup() {
    String query ="SELECT DISTINCT " + EVENT_DATE + " AS _id FROM " + EVENT_TABLE; 
      return mDb.rawQuery(query,null); 
}

public Cursor fetchChildren(String dateToken) {
    return mDb.query(EVENT_TABLE, new String[] { EVENT_ROWID,
            EVENT_NAME, EVENT_DATE }, EVENT_DATE + "='" + dateToken + "'" ,
            null, null, null, null);
}

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM