3

I have an sqlite database with three columns: id, a date and a string. For a single date there can be multiple strings associated with it, so I have multiple rows with the same date just with different strings.

I want to use an ExpandableListView to show this data. I need to implement getChildrenCursor() in SimpleCursorTreeAdapter in order to use it for this purpose, but I am not sure how to do so. I have looked at this and I see that it uses managedQuery, but I do not have a content provider so I cannot use it. From what I understand, the purpose of getChildrenCursor() is to get a cursor with only the data that can be put in a child, but I can't see how this method can separate the entries according to their dates, since it's only passed a Cursor as a parameter.

Community
  • 1
  • 1
colig
  • 405
  • 5
  • 11
  • An update: after much wrangling with the code and not going anywhere with it, I gave up and just used a ListView. – colig Mar 08 '11 at 08:27
  • I have also came across the exactly same problem as you have mentioned here. Could you solve the same? – Shaista Naaz Jul 16 '11 at 23:37
  • I am doing something similar HERE http://stackoverflow.com/questions/10611927/simplecursortreeadapter-and-cursorloader – Etienne Lawlor May 16 '12 at 04:51

3 Answers3

0

If you do not want to use a ContentProvider try running your query in an AsyncTask. Then use the changeCursor method inside onPostExecute to swap out the Cursor.

managedQuery should not be used as it has been depreciated in API 11.

The groupCursor object can be used to say get an "_id" for use in querying for its child data. like SELECT * FROM 'TABLE' WHERE ID = ?. the "?" being an ID column from the group cursor, which is most likely going to be used as a foreign key on another table. If you're still confused try searching for "Database Normalization" on google.

Nick H
  • 8,897
  • 9
  • 41
  • 64
0
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
    @SuppressWarnings("deprecation")
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        // Given the group, we return a cursor for all the children within that group 
        // Return a cursor that points to this contact's phone numbers
        Uri.Builder builder = People.CONTENT_URI.buildUpon();
        ContentUris.appendId(builder, groupCursor.getLong(mGroupIdColumnIndex));
        builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY);
        Uri phoneNumbersUri = builder.build();
        // The returned Cursor MUST be managed by us, so we use Activity's helper
        // functionality to manage it for us.
        return managedQuery(phoneNumbersUri, mPhoneNumberProjection, null, null, null);
    }
}
Tim Stone
  • 19,119
  • 6
  • 56
  • 66
Bhavin2887
  • 170
  • 2
  • 3
  • 10
0

I know it's 8 months too late, but still.

You can create cursor without a content provider. Open the SQLite database and do db.query(...) - this will create a cursor for you. It's the same way content providers create cursors.

zmbq
  • 38,013
  • 14
  • 101
  • 171