1
I’m starting in java android, I’m quite layy yet, I’m picking up the concept of POO gradually.
My problem is this, I have a Criabanco class and another Bancocontroller where I have some methods to manipulate the bank, this in an Activity with menu name, up to here everything working.
The doubt and if I need to manipulate the database data in another Activity, what is the correct way to do this.
At first I thought about repeating the class in the other Activity but I was afraid of doing this: the bank was going to be rewritten and I also found it inelegant to repeat all the code.
So I was wondering if there’s a way to instantiate this class in the other Activity.
Below the code I used to create and manipulate the bank, only the basic, I removed the methods not to be extended.
public class BancoController {
private SQLiteDatabase db;
private CriaBanco banco;
public BancoController(Context context) {
banco = new CriaBanco(context);
}
}
public class CriaBanco extends SQLiteOpenHelper {
private static final int VERSION = 1;
private static final String DB_NAME = "dbcoletor.db";
public CriaBanco(Context context) {
super(context, DB_NAME, null, VERSION);
}
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i2) {
//--nothing for now--
}
}
To solve I created a class in the other Activity and used this form:
private SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("data/data/com.coletorrc/databases/dbcoletor.db", null);
So I could manipulate the bank, although it worked I was wondering if there would be a more elegant way to do it.
Marcio, just that I need this for the other Activity, the Create Bank class in my case is in an Activity, that when I click a button passes to another, in this other one that I need to manipulate the bank, taking advantage...if I instantiate this class Create bank more than once...or then in another Activity, it overwrites the bank created?
– Dunga Cardoso
You can create the Create Database class separately (create a file of your own, to be more organized) and instantiate it where necessary. The seat shall not be overwritten.
– Márcio Oliveira
I managed to do as you instructed.
– Dunga Cardoso