2012-06-23 61 views
0

我正在嘗試在我爲學習目的而編寫的某些代碼中使用SimpleCursorTreeAdapter。正在發生的事情是,當我點擊父母時,它會展開並展示孩子,但是我再也無法做任何事了。我使用調試器來執行代碼,並發現我被困在looper.loop中的一個永無止境的循環中。我已經評論了所有在下面的代碼段中實際工作的東西,但它仍然掛起。通過下面的代碼片斷,我預計點擊第二個父代會導致它擴大(如箭頭更改所示)。 我在代碼中留下了舊的東西(但註釋掉了)以顯示我正在嘗試做的事情。Android SimpleCursorTreeAdapter Hanging

我真的很感激任何意見或建議。

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

} 

我試過這個建議,但是我沒有看到行爲有任何變化。以下是我目前有:

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

我試圖與實際運行查詢並返回遊標,但它並沒有區別,所以我想回去的基本知識。 如果有人對發生了什麼有什麼想法,我將不勝感激。

回答

3

我相信你的問題是因爲你重寫SimpleCursorTreeAdapter而不是擴展它。當你這樣做時,你需要重新創建它用來處理列表的所有方法(你還沒有完成)。

您有兩種選擇:添加所有缺少的方法或創建一個擴展SimpleCursorTreeAdapter並將其用作適配器的新類。最後一個(IMO)比第一個更容易。你只需重寫你需要的方法。

我的一個項目的例子如下。

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)); 
    } 
} 
相關問題