我使用CursorLoader來查詢使用內容提供程序的數據庫以從名稱列中獲取值。然後在我的活動的onCreate()方法中使用SimpleCursorAdapter將其填充到ListView中。我想知道是否應該在Activity的onStart()方法中再次實現適配器,以便在導航到另一個活動並返回到列表視圖時更新列表。但我認爲這不是必要的,因爲CursorLoader會自動偵聽數據庫中的更新。但是當我返回到活動時,我的listview沒有被填充。Android CursorLoader Listview
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Show the Up button in the action bar.
setupActionBar();
mAdapter = new SimpleCursorAdapter(this,com.example.test.R.layout.list_view , null,
fromColumns, toViews,0);
setListAdapter(mAdapter);
listview = getListView();
listview.setAdapter(mAdapter);
View addButton=findViewById(R.id.add);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(), Enter.class);
startActivity(i);
}
}
);
getLoaderManager().initLoader(LIST_ID, null, this);
}
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
// TODO Auto-generated method stub
Uri CONTENT_URI=Uri.parse("content://" + "com.example.test" + "/University");
return new CursorLoader(this,CONTENT_URI, PROJECTION, SELECTION, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
// TODO Auto-generated method stub
mAdapter.swapCursor(arg1);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
mAdapter.swapCursor(null);
你還實現了'onCreateLoader'和'onLoadFinished'嗎? – Szymon
是的,我做到了。我已經包含代碼 – Meenakshi