我有一个活动,其中有ListFragment
你的朋友的“组”。这些组保存在 sqlite 数据库中。
当我们单击一个组时,它会将我们带到另一个活动页面。此页面显示该特定组的参与者。
在参与者页面上,有一个操作栏菜单,其中包含“删除和删除组”选项,然后您将返回“组”页面,因为您不再是刚刚离开的组的一部分。
我的问题是,在参与者页面上,当我对删除选项进行编码时,我在组页面中没有对适配器的引用。所以我不能notifyDataSetChanged
在“组”页面上。因此回到组页面,即使我刚刚删除了 1 个组,列表也是一样的。(或者我的sqlite删除查询不起作用)
另外,我不确定是否应该notifyDataSetChanged
在菜单选项中使用。onCreate
在或其他生命周期方法上调用某些东西会更有意义吗?
任何帮助是极大的赞赏。谢谢!
伪代码:
组列表片段
public class GroupListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
// custom CursorLoader which is based on the SimpleCursorLoader found at the below link
// http://stackoverflow.com/questions/7182485/usage-cursorloader-without-contentprovider
// Basically, the CursorLoader was subclassed to replace references of the content provider
// with the sqlite database
public static final class CustomCursorLoader extends SimpleCursorLoader{
//relevent thing here is that my custom cursor loader queries my database
// in the loadInBackground() method, returning a cursor.
}
// Standard onCreate, onActivityCreated and onLoadFinished methods
// onCreateLoader is the only somewhat different method in that it uses my custom
// cursorloader class
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_1,null,FROM,TO,0 );
setListAdapter(mAdapter);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0,null,this);
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CustomCursorLoader(getActivity(),getHelper());
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
mAdapter.swapCursor(cursor);
}
参与者活动页面
//relevant section is menu
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.deleteGroupMenuSelection:
//my db helper class
ContractDBHelpers mHelper = new ContractDBHelpers(getApplicationContext());
SQLiteDatabase db = mHelper.getWritableDatabase();
db.delete(GroupContract.GroupDetails.TABLE_NAME, GroupContract.GroupDetails._ID+"=?",new String[] {Integer.toString(position)});
db.close();
/***********************************************************************
* here is where I think I'm supposed to notifyDataSetChanged
* but since I'm in an Activity, I don't know how to reference the adapter
************************************************************************/
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}