开发者

Help with SimpleCursorTreeAdapter and getChildrenCursor()

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 ha开发者_如何学编程ve 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.


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);
    }
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜