小编Evg*_*kov的帖子

如何在适配器数据更改时更新可扩展列表视图

我有一个扩展ExpandableListActivity的活动.我使用SimpleCursorTreeAdapter来填充ExpandableListView.我的布局包含列表视图和空视图.在应用程序启动时,ExpandableListActivity会自动选择要显示的右视图.

我的步骤:

  1. 应用程序启动,没有数据.(屏幕上的空视图)
  2. 将一些数据插入db.
  3. 调用adapter.notifyDataSetChanged(); 但空视图仍然在屏幕上,我的列表视图中没有任何项目.

然后我重启app:

  1. 列表视图出现.我展开所有组并滚动到底部.
  2. 我点击列表中的项目.出现新活动.
  3. 单击后退按钮.所有组都已折叠,我们位于屏幕顶部.滚动位置和扩展组不会被记住.
  4. 从db中删除所有数据并调用adapter.notifyDataSetChanged();
  5. 子视图已消失,但顶级组仍然可见.

问题:

  1. 如何用列表视图替换空视图?
  2. 我该怎么做才能保存组的状态并滚动列表视图的位置?

在SDK上测试:1.5r3,1.6r1

码:

public class MainActivity extends ExpandableListActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        dbHelper = new DBOpenHelper(this);

        rubricsDbAdapter = new RubricsDBAdapter(dbHelper);
        rubricsDbAdapter.open();

        itemsDbAdapter = new ItemsDBAdapter(dbHelper);
        itemsDbAdapter.open();

        rubricsCursor = rubricsDbAdapter.getAllItemsCursor();
        startManagingCursor(rubricsCursor);

        // Cache the ID column index
        rubricIdColumnIndex = rubricsCursor.getColumnIndexOrThrow(RubricsDBAdapter.KEY_ID);

        // Set up our adapter
        mAdapter = new MyExpandableListAdapter(rubricsCursor,
                this,
                android.R.layout.simple_expandable_list_item_1,
                android.R.layout.simple_expandable_list_item_1,
                new String[] {RubricsDBAdapter.KEY_NAME},
                new int[] {android.R.id.text1},
                new String[] {ItemsDBAdapter.KEY_NAME}, 
                new int[] …
Run Code Online (Sandbox Code Playgroud)

java android

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

java ×1