Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3960750
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:53:28+00:00 2026-05-20T02:53:28+00:00

I have been working on a way to pass a reference between Activities, to

  • 0

I have been working on a way to pass a reference between Activities, to my DataBaseHelper class. I don’t want to recreate new instances of DataBaseHelper in each new activity.
From what I have seen, the best way to do this is to implement android.os.parcelable, which is fine.
However, when I try to override the DataBaseHelper constructor with:

public DataBaseHelper(Parcel source)

I get an error telling me that the constructor is undefined.
I kind of understand what this means, but am not sure how to get around this issue and so implement Parcelable in this class.
Below is the DatabaseHelper class, the parcelable code is towards the bottom.

package com.drager;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper implements Parcelable{
    //private static String DB_PATH = "/data/data/com.drager/databases/";
    private static String DB_PATH = Environment.getDataDirectory()+"/data/com.drager/databases/";
    final static String DB_NAME = "myDBName";
    private SQLiteDatabase myDataBase=null;
    private final Context myContext;
    private DataBaseHelper myDbHelper;
    private static String TAG ="MyActivity";

    public DataBaseHelper(Context context){
        super(context, DB_NAME, null, 1);
        this.myContext = context;

    }


    public DataBaseHelper(Parcel source) {
        super(source);
        // TODO Auto-generated constructor stub
    }


    public DataBaseHelper createDataBase() throws IOException{
        boolean dbExist =checkDataBase();
        //SQLiteDatabase db_read =null;
        Log.i(TAG,"############value of dbExist"+dbExist+"##########");
        if (dbExist){
            copyDataBase();
            //db must exist
        }
        else{
        myDbHelper = new DataBaseHelper(myContext);
        myDataBase = myDbHelper.getReadableDatabase();
        myDataBase.close();
        //this.getReadableDatabase();
            //db_read.close();

            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("error copying database");
            }
        }
        return this;

    }

    public Cursor executeStatement(){
        Log.i(TAG,"in execute statement");
        Cursor cursor=null;

        cursor=myDataBase.rawQuery("SELECT _ID, title, value "+
                    "FROM constants ORDER BY title",
                     null);
        return cursor;
    }

    public String[] getTextViewItem(){

        Cursor cursor=null;

        String str="";
        String[] resultsString;
        //store query results in cursor
        cursor=myDataBase.rawQuery("SELECT shrt_description FROM description",
                     null);

        ArrayList strings = new ArrayList();
        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            str =cursor.getString(cursor.getColumnIndex("shrt_description"));
            strings.add(str);
        }
         resultsString =(String[])strings.toArray(new String[strings.size()]);

         close();//close database after use
        return resultsString;
    }

public String[] getDetailedDescription(){

        Cursor cursor=null;

        String str="";
        String[] resultsString;
        //store query results in cursor
        cursor=myDataBase.rawQuery("SELECT detailed_description FROM description",
                     null);

        ArrayList strings = new ArrayList();
        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            str =cursor.getString(cursor.getColumnIndex("detailed_description"));
            strings.add(str);
        }
         resultsString =(String[])strings.toArray(new String[strings.size()]);

         close();//close database after use
        return resultsString;
    }

    public void copyDataBase() throws IOException{
        // open db as input stream
        InputStream myInput;
        //open empty db as output stream
        OutputStream myOutPut;
        try {
            myInput = myContext.getAssets().open(DB_NAME);

            //path to newly created db
            String outFileName =DB_PATH + DB_NAME;

            myOutPut = new FileOutputStream(outFileName);

            //transfer bytes from the inputFile to the outPutFile
            byte[] buffer = new byte[1024];
            int length;
            while((length = myInput.read(buffer))>0){
                myOutPut.write(buffer, 0, length);
            }
            myOutPut.flush();
            myOutPut.close();
            myInput.close();
            }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;

        String myPath = DB_PATH + DB_NAME;

        try {
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLException e) {

            e.printStackTrace();
            return false;
        }

        if (checkDB != null){
            checkDB.close();
        }
        return true;
        //return checkDB !=null ? true : false;
    }

    public void openDataBase()throws SQLException{
        //open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }

    @Override
    public synchronized void close(){
        if(myDataBase != null){
            myDataBase.close();
        }
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        dest.writeParcelable((Parcelable) myDataBase, 0);
        dest.writeString(DB_PATH);
        dest.writeString(DB_NAME);
        dest.writeString(TAG);
        dest.writeParcelable((Parcelable) myContext, 0);
        dest.writeParcelable(myDbHelper, 0);

    }

    public static final Parcelable.Creator<DataBaseHelper> CREATOR = new Parcelable.Creator<DataBaseHelper>() {

        @Override
        public DataBaseHelper createFromParcel(Parcel source) {
            // TODO Auto-generated method stub

            return new DataBaseHelper(source);
        }

        @Override
        public DataBaseHelper[] newArray(int size) {
            // TODO Auto-generated method stub
            return null;
        }

    };

}

Any help greatly appreciated.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:53:29+00:00Added an answer on May 20, 2026 at 2:53 am

    You seem to be unnecessarily complicating things. You say:

    I don’t want to recreate new instances
    of DataBaseHelper in each new
    activity. From what I have seen, the
    best way to do this is to implement
    android.os.parcelable, which is fine.

    But restoring a DataBaseHelper object from a Parcel does create a new instance of the DataBaseHelper class; it’s just a harder way of doing it than creating a fresh DataBaseHelper.

    In any case, although you didn’t copy and paste the error message here, I think I know what error you received: It’s not that your DataBaseHelper class doesn’t contain the right constructor; it does. It’s that the super class, SQLiteOpenHelper, doesn’t implement Parcelable. The only way around this problem would be to manage all of the SQLiteOpenHelper‘s state yourself. That is, include the SQLiteOpenHelper’s state as part of your implementation of writeToParcel, and restore that state as part of DatabaseHelper(Parcel). The constructor then would call the default constructor for SQLiteOpenHelper. The resulting constructor would look something like this:

    public DataBaseHelper(Parcel source, Context context, String name,
                          SQLiteDatabase.CursorFactory factory, int version) {
        // NOTE: You've got to pass in an appropriate Context; I'm sure it would not work
        // to try to include the original Activity in the Parcel! That means that your 
        // constructor **must** include the Context as one of its arguments. Conceivably,
        // the name, factory, and version could be taken from the Parcel rather than being
        // passed in the constructor arguments. Again, I ask: Is it worth the hassle?
        super(context, name, factory, version);
    
        // First, restore any relevant `SQLiteOpenHelper` state...
               //...
        // Now restore any relevant DataBaseHelper state...
               //...
    }
    

    I don’t think it’s likely to be worth the effort, but that’s up to you to decide.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working my way through Scott Guthrie's excellent post on ASP.NET MVC
I'm starting out with wxPython and have been working my way through every tutorial
I have been working on a new project for a little while and already
I have been working on a web services related project for about the last
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
I have been working with a string[] array in C# that gets returned from
We have been working with CVS for years, and frequently find it useful to
I have been working on some legacy C++ code that uses variable length structures
I have been working with relational databases for sometime, but it only recently occurred
I have been working as a native C++ programmer for last few years. Now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.