0

I created database at stClass, and I want to use the same database in other class( ndClass). Android studio wanted to make all static so I did it, but i cannot resolve that cannot be referenced from a static field

stClass:

 public static Base db= new Base(this,null,null,0);// error in "this"
public static Base getDb() {
    return db;
}

ndClass:

Base db = AddDate.getDb();

I know that problem is probably piss-easy...

Xalion
  • 623
  • 9
  • 27

1 Answers1

0
public class Base extends SQLiteOpenHelper {



public static final String DB_NAME ="db_workout2";//name of base
public static  final String DB_TABLE ="dateOfWorkout2";//name of table
public static final int DB_VER =1;
private static Base sInstance;


public static synchronized Base getInstance(Context applicationContext) {


    if (sInstance == null) {
        sInstance = new Base(applicationContext);
    }
    return sInstance;
}
private   Base(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DB_NAME, null, DB_VER);

}





@Override


public void onCreate(SQLiteDatabase db){
    db.execSQL(
            "create table "+ DB_TABLE +"("
            + "nr integer primary key autoincrement,"
            + "name text,"
            + "s1 integer,"
            + "s2 integer,"
            + "s3 integer,"
            + "s4 integer,"
            + "s5 integer,"
            + "weight integer,"
            +"ddate text);"
            +"");

}

public void addWorkoutPlan(String i, int q, int w, int e, int r, int t, int wt,String d) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues wartosci = new ContentValues();

    wartosci.put("name", i);
    wartosci.put("s1", q);
    wartosci.put("s2", w);
    wartosci.put("s3", e);
    wartosci.put("s4", r);
    wartosci.put("s5",t);
    wartosci.put("weight",wt);
    wartosci.put("ddate",d);



    db.insertOrThrow(DB_TABLE, null, wartosci);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
    onCreate(db);

}
public List<String> getDateLabels(){
    List<String> labels = new ArrayList<String>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + DB_TABLE;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning lables
    return labels;
}

}

Xalion
  • 623
  • 9
  • 27