如何更新扩展列表视图时,适配器的数据变化 [英] How to update expandable list view when adapter's data changes
本文介绍了如何更新扩展列表视图时,适配器的数据变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个扩展ExpandableListActivity活动。我用SimpleCursorTreeAdapter填写ExpandableListView。 我的布局包含列表视图和空视图。 在应用程序启动ExpandableListActivity自动选择合适的视图来显示。
我的步骤:
- 在应用程序启动时,没有数据。 (屏幕上的空白视图)
- 在一些数据插入到数据库中。
- 呼叫adapter.notifyDataSetChanged();但空观点依然在屏幕上,并没有在我的列表视图中的任何项目。
然后我重新启动应用程序:
- 在显示列表视图。我展开所有团体和滚动到底部。
- 在我点击列表中的项目。出现新的活动。
- 单击后退按钮。所有组都倒塌,我们是在屏幕的顶部。滚动位置和扩大群体不记得。
- 从数据库中删除所有数据,并调用adapter.notifyDataSetChanged();
- 在孩子的意见已经消失,但顶级组仍清晰可见。
问题:
- 在我能做些什么,以取代列表视图空的观点?
- 在我能做些什么做保存群体的状态,滚动列表视图中的位置?
测试在软件开发工具包:1.5r3,1.6r1
code:
公共类MainActivity扩展ExpandableListActivity {
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
dbHelper =新DBOpenHelper(本);
rubricsDbAdapter =新RubricsDBAdapter(dbHelper);
rubricsDbAdapter.open();
itemsDbAdapter =新ItemsDBAdapter(dbHelper);
itemsDbAdapter.open();
rubricsCursor = rubricsDbAdapter.getAllItemsCursor();
startManagingCursor(rubricsCursor);
//缓存中的ID列索引
rubricIdColumnIndex = rubricsCursor.getColumnIndexOrThrow(RubricsDBAdapter.KEY_ID);
//设置我们的适配器
mAdapter =新MyExpandableListAdapter(rubricsCursor,
本,
android.R.layout.simple_expandable_list_item_1,
android.R.layout.simple_expandable_list_item_1,
新的String [] {} RubricsDBAdapter.KEY_NAME,
新的INT [] {} android.R.id.text1,
新的String [] {} ItemsDBAdapter.KEY_NAME,
新的INT [] {android.R.id.text1});
setListAdapter(mAdapter);
}
公共布尔onChildClick(ExpandableListView父,视图V,INT groupPosition,诠释childPosition,长ID){
意图I =新的意图(这一点,ItemViewActivity.class);
i.putExtra(ItemsDBAdapter.KEY_ID,ID);
startActivity(ⅰ);
返回super.onChildClick(父母,V,groupPosition,childPosition,身份证);
}
私人无效updateMyData(){
INT I;
INT K表;
长rubricId;
对于(i = 1; I< = 5;我++){
rubricId = rubricsDbAdapter.insert(专栏+ I);
为(K = 1; K&其中; = 5; k ++){
itemsDbAdapter.insert(项目+ I + - + K,rubricId);
}
}
mAdapter.notifyDataSetChanged();
}
私人无效deleteMyData(){
rubricsDbAdapter.deleteAll();
itemsDbAdapter.deleteAll();
mAdapter.notifyDataSetChanged();
}
公共类MyExpandableListAdapter扩展SimpleCursorTreeAdapter
{
公共MyExpandableListAdapter(光标指针,上下文语境,诠释的GroupLayout,
INT childLayout,字符串[] groupFrom,INT [] groupTo,字符串[] childrenFrom,
INT [] childrenTo){
超(背景下,光标的GroupLayout,groupFrom,groupTo,childLayout,childrenFrom,
childrenTo);
}
@覆盖
受保护的光标getChildrenCursor(光标notebookCursor){
//由于组,我们返回游标该组内的所有儿童
长ID = notebookCursor.getLong(rubricIdColumnIndex);
光标itemsCursor = itemsDbAdapter.getRubricItemsCursor(ID);
startManagingCursor(itemsCursor);
返回itemsCursor;
}
}
}
解决方案
那么,答案是:
- 在调用适配器的notifyDataSetChanged()方法之前调用cursor.requery()。
- 保存组状态活动的的onPause()方法,并在onResume)恢复(方法。
I have an activity that extends ExpandableListActivity. I use SimpleCursorTreeAdapter to fill ExpandableListView. My layout contains list view and empty view. On app start ExpandableListActivity automatically chooses the right view to display.
My steps:
- App starts, there is no data. (empty view on the screen)
- Insert some data into db.
- Call adapter.notifyDataSetChanged(); But empty view is still on the screen and there is no any item in my list view.
Then I restart app:
- List view appears. I expand all groups and scroll to the bottom.
- I click on the item in the list. New activity appears.
- Click back button. All groups are collapsed and we are at the top of the screen. Scroll position and expanded groups are not remembered.
- Delete all data from db and call adapter.notifyDataSetChanged();
- Child views have disappeared, but top-level groups are still visible.
Questions:
- What can I do to replace empty view with list view?
- What can I do to do to save state of groups and scroll position of the list view?
Tested on SDKs: 1.5r3, 1.6r1
Code:
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;
}
}
}
解决方案
So, the answer is:
- Call cursor.requery() before calling adapter's notifyDataSetChanged() method.
- Save groups state in activity's onPause() method and restore in onResume() method.
这篇关于如何更新扩展列表视图时,适配器的数据变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文