gpt4 book ai didi

java - 如何在适配器的数据更改时更新可扩展 ListView

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:44 24 4
gpt4 key购买 nike

我有一个扩展 ExpandableListActivity 的 Activity 。我使用 SimpleCursorTreeAdapter 来填充 ExpandableListView。我的布局包含 ListView 和空 View 。在应用程序启动时,ExpandableListActivity 会自动选择要显示的正确 View 。

我的步骤:

  1. 应用启动,没有数据。 (屏幕上的空白 View )
  2. 将一些数据插入数据库。
  3. 调用适配器.notifyDataSetChanged();但是屏幕上仍然是空 View ,我的 ListView 中没有任何项目。

然后我重新启动应用程序:

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

问题:

  1. 如何用 ListView 替换空 View ?
  2. 我该怎么做才能保存组的状态和 ListView 的滚动位置?

在 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[] {android.R.id.text1});

setListAdapter(mAdapter);
}

public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Intent i = new Intent(this, ItemViewActivity.class);
i.putExtra(ItemsDBAdapter.KEY_ID, id);
startActivity(i);

return super.onChildClick(parent, v, groupPosition, childPosition, id);
}

private void updateMyData() {
int i;
int k;
long rubricId;

for (i = 1; i <= 5; i++) {
rubricId = rubricsDbAdapter.insert("rubric " + i);
for (k = 1; k <= 5; k++) {
itemsDbAdapter.insert("item " + i + "-" + k, rubricId);
}
}

mAdapter.notifyDataSetChanged();
}

private void deleteMyData() {
rubricsDbAdapter.deleteAll();
itemsDbAdapter.deleteAll();

mAdapter.notifyDataSetChanged();
}

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 notebookCursor) {
// Given the group, we return a cursor for all the children within that group
long id = notebookCursor.getLong(rubricIdColumnIndex);

Cursor itemsCursor = itemsDbAdapter.getRubricItemsCursor(id);
startManagingCursor(itemsCursor);
return itemsCursor;
}

}
}

最佳答案

所以,答案是:

  1. 在调用适配器的 notifyDataSetChanged() 方法之前调用 cursor.requery()。
  2. 在 Activity 的 onPause() 方法中保存组状态并在 onResume() 方法中恢复。

关于java - 如何在适配器的数据更改时更新可扩展 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1463906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com