我是这个ExpandableListView的新手,非常需要您的帮助。
我正在使用以下代码通过SimpleCursorTreeAdapter创建ExpandableListView,因为我是从SQLite数据库获取数据的。 一切正常,但是我找不到如何向父级ListView添加某些项目的解决方案(目前)。 我想通过单击TextView添加一个类别。
public class ListViewExp extends ExpandableListActivity {
private DBAdapter db;
private Cursor getMatchigItems;
private Cursor getAllCat;
private ExpandableListView expListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testexpview);
TextView tv1 = (TextView) findViewById(R.id.testtext);
db = new DBAdapter(this);
db.open();
getAllCat = db.getAllCategories(1);
startManagingCursor(getAllCat);
final SimpleCursorTreeAdapter cursorTreeAdapter = new SimpleCursorTreeAdapter(
getApplicationContext(),
getAllCat,
R.layout.group_row,
R.layout.group_row,
new String[]{db.KEYCAT_TXTCATEGORYNAME, db.KEYCAT_ROWID},
new int[]{R.id.groupname},
R.layout.child_row,
R.layout.child_row,
new String[]{db.KEYITEMS_TXTITEMNAME, db.KEYITEMS_ROWID, db.KEYITEMS_ROWID},
new int[]{R.id.childname,R.id.rgb}) {
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
db.open();
getMatchigItems = db.getMatchingItems(groupCursor.getLong(0), 1);
startManagingCursor(getMatchigItems);
return getMatchigItems;
}
};
expListView = getExpandableListView();
setListAdapter(cursorTreeAdapter);
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
db.open();
db.addMyCategory("TestCat1", "");
// getAllCat.requery();
cursorTreeAdapter.notifyDataSetChanged();
}
});
db.close();
}
}
添加到数据库工作正常,但仅在重新启动应用程序时才能看到新项目。 我试图重新查询游标getAllCat.requery(); ,但是当我这样做时,它会刷新列表,但它是空的(甚至不会显示较旧的项目),就像没有数据一样。
谁能给我一些解决方法的指导? 我想念什么吗?
我应该执行onResume替代还是完全不同? 预先感谢一百万! 最好的祝福!