我有一个使用ListView作为主屏幕的应用程序.每行显示一些文本,一个复选框和根据数据的图像,因为你不能使用标准的ListAdapter,我自己制作.
每次发生更改(添加/删除行,检查提示,导航等)到列表数据时,我都会使用以下方法刷新列表显示:
public void refreshList()
{
// fetch the new items
ItemDbHelper items = new ItemDbHelper(this);
Cursor item_cursor = items.fetchItemForCurrentView(this.current_context_filter);
// do not use "start managing cursor" here or resume() won't work
// anymore since the cursor will be closed despite we still need it
// set the welcome message if no items to display
if (item_cursor.getCount() == 0)
{
TextView message = (TextView) this.findViewById(R.id.home_message);
message.setVisibility(View.VISIBLE);
}
ListView list = this.task_list;
ItemListAdapter item_cursor_adater = (ItemListAdapter) list.getAdapter();
// close the old cursor manually and replace it with the new one
item_cursor_adater.getCursor().close();
item_cursor_adater.changeCursor(item_cursor);
// reset some cache data in the adapter
item_cursor_adater.reset();
// tell the list to refresh
item_cursor_adater.notifyDataSetChanged();
// to easy navigation, we set the focus the last selected item
// set the last modified item as selected
int selected_index = this.getItemPositionFromId(list,
this.getCurrentContextFilter()
.getSelectedTaskId());
list.setSelection(selected_index);
}
Run Code Online (Sandbox Code Playgroud)
一些事实:
items.fetchItemForCurrentView() 触发一个非常繁重的SQL查询
this.getItemPositionFromId() 循环遍历整个ListView,以查找具有给定id的行的索引.
在item_cursor_adapter扩展SimpleCursorAdapter和覆盖
public View getView(int position,View convertView,ViewGroup parent)
这是一个非常沉重的方法.
在典型的应用程序用例中,这种方法在用户需求中经常被调用,并且等待一秒钟以使屏幕刷新以降低用户体验.
你对如何改进这个有什么建议吗?
一些想法:
| 归档时间: |
|
| 查看次数: |
899 次 |
| 最近记录: |