Clavijo
Clavijo

Reputation: 64

Android SimpleCursorTreeAdapter Hanging

I'm trying to use a SimpleCursorTreeAdapter in some code I'm writing for learning purposes. What is happening is that when I click on a parent it expands and shows the children, but then I can't do anything more. I walked the code using the debugger and fould that I am stuck in a never ending loop inside looper.loop. I have commented out all of the stuff that does actual working in the snippet below but it still hangs. With the snippet below, I would expect that clicking on a second parent would cause it to expand (as indicated by the arrow changing). I left the old stuff in the code (but commented out) to show what I was trying to do.

I would really appreciate any advice or suggestions.

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

}

I tried the suggestion but I didn't see any change in the behavior. Here is what I have currently:

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

I tried with actually running the query and returning the cursor, but it didn't make a difference so I tried to go back to the basics. If anyone has any ideas on what is going on, I would greatly appreciate it.

Upvotes: 0

Views: 927

Answers (1)

Barak
Barak

Reputation: 16393

I believe your issue is becasue you are overriding SimpleCursorTreeAdapter instead of extending it. When you do that, you need to re-create all the methods it uses to work the list (which you haven't done).

You have two choices: add in all the missing methods or create a new class that extends SimpleCursorTreeAdapter and use that as your adapter. The last (IMO) is far easier than the first. You simply override the methods you need.

An example from one of my projects follows.

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

Upvotes: 3

Related Questions