我使用内容提供程序来显示数据库中的数据。内容显示在 ListActivity 中。当我改变手机的方向时,有时它不会重新加载数据并显示一个空的 ListView。可能是什么原因?它大约每 4-5 次发生...有一个操作栏微调器,它具有三个项目:显示全部、显示日期、显示位置,它将查询数据库以仅显示一个子集
public class MainActivity extends ListActivity {
private Cursor mCursor = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSimpleCursorAdapter = new SpecialAdapter(this,
R.layout.row,
null,
PROJECTION,
new int[] { R.id.titleID, R.id.dateTimeOrLocationID1 , R.id.dateTimeOrLocationID2 },
CursorAdapter.NO_SELECTION);
//mListView = (ListView) findViewById(android.R.id.list);
//mListView.setAdapter(mSimpleCursorAdapter);
setListAdapter(mSimpleCursorAdapter);
mOnNavigationListener = new OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int position, long itemId) {
switch(position) {
case 0:
mCursor = getContentResolver().query(ReminderContentProvider.CONTENT_URI, PROJECTION, null, null, null);
break;
case 1:
mCursor = getContentResolver().query(ReminderContentProvider.CONTENT_URI, PROJECTION, " Date NOT NULL", null, null);
break;
case 2:
mCursor = getContentResolver().query(ReminderContentProvider.CONTENT_URI, PROJECTION, " Address NOT NULL", null, null);
break;
default:
break;
}
getLoaderManager().restartLoader(0, null, MainActivity.this);
return true;
}
};
}
@Override
public void onResume() {
super.onResume();
getLoaderManager().restartLoader(0, null, this);
}
@Override
public void onStart() {
int showWhat = prefs.getInt(Utils.PREF_SHOW_ALL_DATE_OR_ADDRESS, 1);
VisibilitySelection enumValue = VisibilitySelection.values()[showWhat-1];
switch (enumValue) {
case SHOW_ALL:
mShowProperty = VisibilitySelection.SHOW_ALL;
break;
case SHOW_DATE_TIME:
mShowProperty = VisibilitySelection.SHOW_DATE_TIME;
break;
case SHOW_ADDRESS:
mShowProperty = VisibilitySelection.SHOW_ADDRESS;
break;
default:
mShowProperty = VisibilitySelection.SHOW_ALL;
break;
}
getLoaderManager().restartLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader loader = null;
switch(mShowProperty) {
case SHOW_ALL:
loader = new CursorLoader(this, ReminderContentProvider.CONTENT_URI, PROJECTION, null, null, null);
break;
case SHOW_DATE_TIME:
loader = new CursorLoader(this, ReminderContentProvider.CONTENT_URI, PROJECTION, " Date NOT NULL", null, null);
break;
case SHOW_ADDRESS:
loader = new CursorLoader(this, ReminderContentProvider.CONTENT_URI, PROJECTION, " Address NOT NULL", null, null);
break;
}
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mSimpleCursorAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mSimpleCursorAdapter.swapCursor(null);
}
}