首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SimpleCursorTreeAdapter不刷新的安卓ExpandableListView

SimpleCursorTreeAdapter不刷新的安卓ExpandableListView
EN

Stack Overflow用户
提问于 2011-12-12 05:54:08
回答 1查看 3K关注 0票数 0

我是这个ExpandableListView的新手,我非常需要你的帮助。

我使用下面的代码通过SimpleCursorTreeAdapter创建一个ExpandableListView,因为我从SQLite数据库中获取数据。这一切都很好,但我找不到一个解决方案,如何将一些项目添加到父ListView (目前)。我想通过点击一个TextView来添加一个类别。

代码语言:javascript
复制
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覆盖,还是应该做完全不同的事情?提前感谢你一百万!诚挚的问候!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-12 17:13:54

对不起。奇怪的是,我已经在这里问过答案了,现在我找到了答案。这不是我的本意。但也许有人会用它。

我已经重新初始化了我的游标,并为适配器更改了它

代码语言:javascript
复制
getAllCat = db.getAllCategories(1);
cursorTreeAdapter.changeCursor(getAllCat);

这是onClick块现在的样子:

代码语言:javascript
复制
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();
            }

我们仍然欢迎任何更好的建议。谢谢

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8467807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档