首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓SimpleCursorTreeAdapter挂起

安卓SimpleCursorTreeAdapter挂起
EN

Stack Overflow用户
提问于 2012-06-23 09:20:52
回答 1查看 919关注 0票数 0

我正试着在我编写的一些代码中使用SimpleCursorTreeAdapter,以便于学习。发生的情况是,当我单击一个父对象时,它会展开并显示子对象,但之后我就不能再做任何事情了。我使用调试器遍历了代码,发现自己陷入了looper.loop中的一个永无止境的循环中。我已经在下面的代码片段中注释掉了所有实际工作的东西,但它仍然挂起。使用下面的代码片段,我预计单击第二个父对象会导致它展开(如箭头变化所示)。我在代码中留下了旧的东西(但注释掉了),以显示我想要做的事情。

如果有任何建议或建议,我将非常感谢。

代码语言:javascript
复制
    private void showEvents(final Cursor cursor) {

    ExpandableListView lv1 = this.getExpandableListView();



    SimpleCursorTreeAdapter  adapter = new SimpleCursorTreeAdapter (this, cursor, R.layout.simplerow, FROM, TO, 
            R.layout.childrow, FROMCHILD, TOCHILD) {
        //@Override
        //public void setViewText(TextView v, String text) {
        //  super.setViewText(v, convText(v, text, cursor));
        //}

        @Override
        protected Cursor getChildrenCursor(Cursor parentCursor) {

            int groupPos = parentCursor.getPosition(); 
              Log.d("TEST", "getChildrenCursor() for groupPos " + groupPos); 

            String sql = "select f._id, f.NAME as ASSETCLASS, "
                    + "f.value as AMOUNT, "
                    + "0 as PERCENTAGE, "
                    + "0 as TARGET "
                    + "from funds f "
                    + "where f.asset_class = " + parentCursor.getInt(0) + ";";

            sql = "select f._id, f.NAME as FUNDNAME, "
                    + "f.value as AMOUNT_OWNED "
                    + "from funds f "
                    + "where 1  = 1;";



            Log.d("TEST", sql);

            //SQLiteDatabase db = pfdata.getReadableDatabase();

            //Cursor cursor = db.rawQuery(sql, null);
            //startManagingCursor(cursor);
            //return cursor;

            //Log.d("TEST", Integer.toString(cursor.getCount()));
            //return cursor;
            return null;
        }

    };

    // Log.d("TEST", Integer.toString(adapter.getCount()));

    lv1.setAdapter(adapter);

}

我尝试了这个建议,但我没有看到任何行为上的变化。这是我目前所拥有的:

代码语言:javascript
复制
    private void showEvents(final Cursor cursor) {

    ExpandableListView lv1 = this.getExpandableListView();


    MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(cursor, this, 
            R.layout.simplerow, R.layout.childrow, 
               FROM, TO, 
               FROMCHILD, TOCHILD); 
        lv1.setAdapter(mAdapter); 
       }

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 parentCursor) { 

    String sql = "select f._id, f.NAME as ASSETCLASS, "
            + "f.value as AMOUNT, "
            + "0 as PERCENTAGE, "
            + "0 as TARGET "
            + "from funds f "
            + "where f.asset_class = " + parentCursor.getInt(0) + ";";

    Log.d("TEST", sql);

    return null;        
} 

我试着实际运行查询并返回游标,但没有什么不同,所以我试着回到最基础的部分。如果任何人对正在发生的事情有任何想法,我将非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-23 12:25:55

我相信你的问题是因为你重写了SimpleCursorTreeAdapter而不是扩展它。当您这样做时,您需要重新创建它用来处理列表的所有方法(您还没有这样做)。

您有两个选择:添加所有缺少的方法,或者创建一个扩展SimpleCursorTreeAdapter的新类并将其用作适配器。最后一个(IMO)比第一个简单得多。您只需覆盖所需的方法。

下面是我的一个项目的示例。

代码语言:javascript
复制
mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
       R.layout.rowlayout_expgroup, R.layout.rowlayout_itemlist_exp,
       new String[] { "_id" }, new int[] { android.R.id.text1 },
       new String[] { GroceryDB.ITEM_NAME, GroceryDB.LISTITEM_QTY,
               GroceryDB.ITEM_UNIT }, new int[] { R.id.ListItem1,
               R.id.ListItem3, R.id.ListItem2 });
lv.setAdapter(mAdapter);

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(GroceryListMain.group,
                groupCursor.getString(groupCursor.getColumnIndex("_id")),
                ListFragment_ShopList.listId);
        getActivity().startManagingCursor(childCursor);
        childCursor.moveToFirst();
        return childCursor;
    }

    protected void bindChildView(View view, Context context, Cursor cursor,
            boolean isLastChild) {
        TextView name = (TextView) view.findViewById(R.id.ListItem1);
        TextView qty = (TextView) view.findViewById(R.id.ListItem3);
        TextView unit = (TextView) view.findViewById(R.id.ListItem2);
        TextView cost = (TextView) view.findViewById(R.id.ListItem4);

        name.setText(cursor.getString(2));
        qty.setText(cursor.getString(1));
        unit.setText(cursor.getString(4));
        DecimalFormat df = new DecimalFormat("0.00");
        Double hold = Double.valueOf(cursor.getString(3));
        Double qtyhold = Double.valueOf(cursor.getString(1));
        Double total = hold * qtyhold;
        cost.setText("$" + df.format(total));
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11166055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档