开发者

CursorTreeAdapter - Return part of Groupcursor for Childview Data

I'm currently using a CursorTreeAdapter to map a Curser to an ExpandableListView in Android.

Everything works fine except from the way data is handled inside the ListView. Generally all the data is already inside the Cursor I give the Constructor of the CursorTreeAdater - even the daa for the Childview. The Problem is Android expects the data for the ChildView to be received by the getChildrenCursor function:

@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
    db.open();
    return db.getEpisode(groupCursor.getString(0));
}

Here you already see the Problem. I have to return a cursor but I'm not able to just "Cut out" the one entry in the Cursor that is responsible for the specific Childview. Instead I came up with something like querying the database for every single ChildView there is. This is not only dumb since the data is already there (inside the groupcursor) but it also ist pretty slow.

My Question would be if there is some kind of functionality of cloning only specific entries of cursors or returning only one entry instead of constantly querying the database.

Maybe I'm also开发者_如何学运维 off by using the CursorTreeAdapter and using a more general AdapterClass would be beneficial.

Thank you all, Johannes


I currently figured it our myself.

The answer was to switch from a CursorTreeAdapter to a BaseExpandableListAdapter

I somehow was scared before (dunno why) but now its working like a charm.

If someone is interested in the code I can post it here. Just leave a comment


As requested my current sourcecode. It may need some cleenup - but i hope you get the point. Please ask if you have additional questions.

public class SeriesSeasonEpisodesAdapter extends BaseExpandableListAdapter {

    LayoutInflater mInflater;
    OnClickListener cbListener;
    long date;
    Cursor c;

    public SeriesSeasonEpisodesAdapter(Cursor cursor, Context context,
                    boolean autoRequery, OnClickListener cbListener) {

            this.cbListener = cbListener;

            mInflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public boolean hasStableIds() {
            return true;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
            return false;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
            return 1;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                    boolean isLastChild, View convertView, ViewGroup parent) {

            c.moveToPosition(groupPosition);

            % FILL convertView %

            return convertView;
    }


    @Override
    public Object getChild(int groupPosition, int childPosition) {
            return getGroup(groupPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
            c.moveToPosition(groupPosition);
            return c;
    }

    @Override
    public int getGroupCount() {
            if (c == null)
                    return 0;
            return c.getCount();
    }

    @Override
    public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                    View convertView, ViewGroup parent) {

            c.moveToPosition(groupPosition);

            % FILL convertView %

            return convertView;
    }

    public void swapCursor(Cursor c) {
            this.c = c;
            notifyDataSetChanged();
    }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜