1

I have an Activity which has a ListFragment of "Groups" of your friends. The groups are saved in the sqlite database.

When we click a Group it brings us to another activity page. This page shows the participants of that specific group.

On the participants page, there is an action bar menu which has the option to 'delete and remove group', which will then bring you back to the "Groups" page because you are no longer a part of the group you just left.

My problem is that on the Participants page, when I code the delete option, I don't have a reference to the adapter in the Groups page. So I can't notifyDataSetChanged on the Groups page. Thus going back to the Groups page, the list is the same even though I just deleted 1 of the groups. (Either that or maybe my sqlite delete query isn't working)

Also, I'm not sure if I am supposed to use notifyDataSetChanged in the menu option. Would it make more sense to call something on the onCreate or possibly some other lifecycle method?

Any help is greatly appreciated. Thanks!

Pseudo code:

GroupListFragment

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);
}

ParticipantsActivityPage

//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);
}
Terence Chow
  • 10,755
  • 24
  • 78
  • 141

1 Answers1

0

I would suggest using the primary means of communicating from one Activity to the previous, which is through startActivityForResult(), setResult(), and onActivityResult(). It seems pretty appropriate to me, unless I'm missing something, that your use case would want to leverage this mechanism.

If for some reason you want to notify the previous activity while remaining on the topmost activity (participants), you could use a LocalBroadcastReceiver instead.

Eric Schlenz
  • 2,551
  • 20
  • 19