1

I have a DataBaseAdapter class, which basically creates the database, table and some methods to manipulate the data in it.

It looks like this-

import ...

public class DataBaseAdapter  {

    DataBaseForApp dataBaseForApp;
    //Constructor for DataBaseAdapter
    public DataBaseAdapter(Context context) {

    }
    //insert data
    public long insertData(String player_name,int enrollmentno)
    {
        SQLiteDatabase SQLiteDB=dataBaseForApp.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put(dataBaseForApp.NAME,player_name);
        contentValues.put(dataBaseForApp.ENROL,enrollmentno);
        long id=SQLiteDB.insert(dataBaseForApp.DATABASE_TABLE,null,contentValues);
        SQLiteDB.close();
        return id;
    }
    //search for a player name and the square grid size
    public boolean search(String player_name, int enrollmentno)
    {
        SQLiteDatabase SQLiteDB=dataBaseForApp.getWritableDatabase();
        String[] columns={dataBaseForApp.NAME,dataBaseForApp.ENROL};
        String[] arguments={player_name,Integer.toString(enrollmentno)};
        Cursor cursor=SQLiteDB.query(dataBaseForApp.DATABASE_TABLE,columns,null,null,null,null,null);
        while(cursor.moveToNext())
        {
            int index1=cursor.getColumnIndex(dataBaseForApp.NAME);
            int index2=cursor.getColumnIndex(dataBaseForApp.ENROL);
            String name=cursor.getString(index1);
            int gsize=cursor.getInt(index2);
            if(name.equals(player_name)&&gsize==enrollmentno)
            {
                return true;
            }
        }
        SQLiteDB.close();
        return false;

    }
    // main class
    static class DataBaseForApp extends SQLiteOpenHelper{
        private static final String DATABASE_NAME="SQLiteDB_FOR_APP";
        private static final String DATABASE_TABLE="INFO";
        private static final int DATABASE_VERSION=1;
        private static final String NAME="NAME";
        private static final String ENROL="ENROL";
        private static final String CREATE_TABLE="CREATE TABLE "+DATABASE_TABLE+"("+NAME+" VARCHAR(100), "+ENROL+" INT)";
        private static final String DROP_TABLE="DROP TABLE IF EXISTS "+DATABASE_TABLE;


        public DataBaseForApp(Context context) {
            super(context,DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase SQLiteDB) {
            try {
                SQLiteDB.execSQL(CREATE_TABLE);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onUpgrade(SQLiteDatabase SQLiteDB, int oldVersion, int newVersion) {
            try {
                SQLiteDB.execSQL(DROP_TABLE);
                onCreate(SQLiteDB);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

Then I did this in my First Activity-

DataBaseAdapter dataBaseAdapter=new DataBaseAdapter(ActivityOne.this);

After this, A database has been created and the methods in DataBaseAdapter are available to me for manipulation.

So far, It is cool, but problem arises when I want to get the same database for my second activity to insert the data from there.

If I use this-

 DataBaseAdapter dataBaseAdapter=new DataBaseAdapter(ActivityTwo.this);

then, it means I am trying to create the database once again.

Please suggest me how to share the database between two activities.

Thanks in Advance!

ks_mani
  • 169
  • 1
  • 5
  • 18

2 Answers2

1

First, your DataBaseForApp class is never instantiated. I Suppose you want this to be done in the DataBaseAdapter constructor.

Second, what you want to do is have multiple instances of the DataBaseForApp (extends SQLiteOpenHelper) class. IMHO you don't need this. You could subclass the Application class like this:

public class MyApplication extends Application {
    private static DataBaseAdapter dbAdapter;

    public static SQLiteDatabase getDbAdapter() {
        return dbAdapter;
    } 

    @Override 
    public void onCreate() { 
        super.onCreate(); 
        dbAdapter = new DataBaseAdapter(getApplicationContext());
    } 
} 

And in your manifest.xml file:

<application
    android:name=".MyApplication">
Quanturium
  • 5,698
  • 2
  • 30
  • 36
0

You can make an abstract BaseActivity which all other activities can extend. Inside the BaseActivity create an instance of the DataBaseAdapter. You'll be able to use this in all your activities. Here is an example : android how to create my own Activity and extend it?

You can use BaseActivity as a single place for the thing you need in all the activities. Eg: ProgressDialog or AlertDialog etc.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124