Attemp to re-open an already closed object android.database.sqlite

456 views Asked by At

i have a Main activity which has a list view. the listview items are loaded with LoaderManager.

when i click an item in the listview i open another activity that shows more information (with "startActivityForResult")

the problem is :

when i go back(using the return key on) from the activity that shows information to the main activity and then click again i get an exception - Attempt to re-open an already closed object.

but if i go back from that activity(that shows more information) with a Button that i made there(which is actually "finish()") to the main activity and then cllick again then i get no exception

anyone knows what is the problem?

Thanks !

private void display_listview()
    {
        // create an adapter from the SimpleCursorAdapter
        dataAdapter = new SimpleCursorAdapter(
                this,
                R.layout.row_invite_layout,
                null,
                columns,
                to,
                0);

        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);

        //Ensures a loader is initialized and active.
        getSupportLoaderManager().initLoader(0, null, this);


        //add implementation to listview item click
        listView.setOnItemClickListener(new OnItemClickListener() 
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {

                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                IInvite invite = LoadFromCursor(cursor);

                cursor.close();

                mUpdateFlag = true;

                if(!mTrashFlag) // Trash Can is turned off
                {
                    Intent intent = new Intent(getApplicationContext(), InviteViewActivity.class);
                    intent.putExtra("invite",(Invite)invite);
                    startActivityForResult(intent, INVITE_REQUEST_ID);
                }
                else // Trash Can is turned on
                {
                    getContentResolver().delete(InviteContentProvider.CONTENT_URI, SQLdataHelper.KEY_ROWID+"="+invite.getID(), null);
                    ExtraDelete(invite);
                    getSupportLoaderManager().getLoader(0).forceLoad();
                }


            }
        });
    }


    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data)
    {
        // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        dataAdapter.swapCursor(data);

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) 
    {
        dataAdapter.swapCursor(null);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args)
    {
        String[] projection = {
                SQLdataHelper.KEY_ROWID,
                SQLdataHelper.INVITE_CLIENT_ID,
                SQLdataHelper.INVITE_CREATION_DATE,
                SQLdataHelper.INVITE_NAME,
                SQLdataHelper.INVITE_NOTE,
                SQLdataHelper.INVITE_REQUESTED_DATE,
                SQLdataHelper.INVITE_TOTAL_PRICE,
                SQLdataHelper.INVITE_STATUS
        };
        CursorLoader cursorLoader = new CursorLoader(this,
                InviteContentProvider.CONTENT_URI, projection, null, null, null);
        return cursorLoader;
    }

UPDATE: FIXED IT. i added this method to my main activity..

@Override
public void onResume()
{
    super.onResume();

    getSupportLoaderManager().getLoader(0).forceLoad();
}
1

There are 1 answers

2
lugonja On

You can do it by creating public static fields, but I wouldn't recomend it. You can store data in shared preferences and then retrieve it whenever you want.