我正在尝试在一些代码中使用SimpleCursorTreeAdapter,我正在编写这些代码用于学习目的。发生的事情是,当我点击父母时,它会展开并显示孩子,但是我不能再做任何事了。我使用调试器走了代码,并且我被困在looper.loop里面的一个永无止境的循环中。我已经在下面的代码片段中注释了所有实际工作的东西,但它仍然挂起。使用下面的代码段,我希望点击第二个父代会使其展开(如箭头所示)。 我在代码中留下了旧的东西(但注释掉了)以显示我想要做的事情。
我真的很感激任何建议或建议。
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;
}
我试着实际运行查询并返回光标,但它没有什么区别所以我试着回到基础。 如果有人对正在发生的事情有任何想法,我将不胜感激。
答案 0 :(得分:3)
我相信你的问题是因为你压倒SimpleCursorTreeAdapter
而不是延伸它。当你这样做时,你需要重新创建它用来处理列表的所有方法(你还没有完成)。
您有两种选择:添加所有缺少的方法或创建一个扩展SimpleCursorTreeAdapter
的新类,并将其用作适配器。最后一个(IMO)比第一个(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));
}
}