2011-02-24 34 views
1

我正在使用CursorTreeAdapter将Curser映射到Android中的ExpandableListView。CursorTreeAdapter - 为Childview数据返回Groupcursor的一部分

除了从ListView中处理数据的方式以外,一切正常。一般来说,所有的数据已经在Cursor里面了,我给了CursorTreeAdater的构造函数 - 甚至是Childview的daa。问题是,Android的预期由getChildrenCursor功能接收到ChildView数据:

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

在这里,你已经发现问题。我必须返回一个游标,但我不能仅仅“切出”负责特定Childview的游标中的一个条目。相反,我想出了类似查询数据库的每个ChildView的东西。这不仅是愚蠢的,因为数据已经存在(groupcursor内),但它也很慢。

我的问题是,如果有某种功能只克隆游标的特定条目或只返回一个条目而不是经常查询数据库。

也许我也通过使用CursorTreeAdapter并使用更通用的AdapterClass将是有益的。

谢谢大家, 约翰内斯

+0

我在做类似的位置http://stackoverflow.com/questions/10611927/simplecursortreeadapter-and-cursorloader – toobsco42 2012-05-16 07:29:47

回答

1

我现在想通了自己。

答案是从CursorTreeAdapter切换到BaseExpandableListAdapter

我不知是(说不上来为什么),但现在它的工作就像一个魅力之前害怕。

如果有人对代码感兴趣,我可以在这里发布。发表评论

+0

当然东西,我想看到的代码。 – anakin78z 2011-08-26 19:36:28

+0

是的,我对你如何能够继承BaseExpandableListAdapter进行了分析。我曾尝试类似的东西,但我看到一些奇怪的结果: http://stackoverflow.com/questions/10611927/simplecursortreeadapter-and-cursorloader-for-expandablelistview – toobsco42 2012-05-23 00:48:02

+0

对不起,迟到的答案。我将发布我的源代码作为附加答案。 – Johnnycube 2012-06-01 09:47:09

0

按照我的当前源代码。它可能需要一些cleenup - 但我希望你明白这一点。请询问您是否有其他问题。

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

}