我是这个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覆盖,还是应该做完全不同的事情?提前感谢你一百万!诚挚的问候!
发布于 2011-12-12 17:13:54
对不起。奇怪的是,我已经在这里问过答案了,现在我找到了答案。这不是我的本意。但也许有人会用它。
我已经重新初始化了我的游标,并为适配器更改了它
getAllCat = db.getAllCategories(1);
cursorTreeAdapter.changeCursor(getAllCat);这是onClick块现在的样子:
public void onClick(View v) {
db.open();
db.addMyCategory("TestCat7", "");
getAllCat = db.getAllCategories(1);
cursorTreeAdapter.changeCursor(getAllCat);
cursorTreeAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "KLIK", Toast.LENGTH_SHORT).show();
}我们仍然欢迎任何更好的建议。谢谢
https://stackoverflow.com/questions/8467807
复制相似问题