Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:SQLite中数据库的获取者_Android_Sqlite_Getter - Fatal编程技术网

Android:SQLite中数据库的获取者

Android:SQLite中数据库的获取者,android,sqlite,getter,Android,Sqlite,Getter,我在stClass创建了数据库,我想在其他类(ndClass)中使用相同的数据库。Android studio希望使所有内容都是静态的,所以我这样做了,但我无法解决不能从静态字段引用 stClass: public static Base db= new Base(this,null,null,0);// error in "this" public static Base getDb() { return db; } Base db = AddDate.getDb(); ndCl

我在stClass创建了数据库,我想在其他类(ndClass)中使用相同的数据库。Android studio希望使所有内容都是静态的,所以我这样做了,但我无法解决不能从静态字段引用

stClass:

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

 public static Base db= new Base(this,null,null,0);// error in "this"
public static Base getDb() {
    return db;
}
Base db = AddDate.getDb();
我知道这个问题可能很简单…

公共类基类扩展了SQLiteOpenHelper{
public class Base extends SQLiteOpenHelper {

    private static Base sInstance;

    private static final String DATABASE_NAME = "database name";
    private static final String DATABASE_TABLE = "table";
    private static final int DATABASE_VERSION = 1;

    public static synchronized Base getInstance(Context applicationContext) {

        // Make sure it is application context, which will ensure that you
        // don't leak an Activity's context.
        if (sInstance == null) {
            sInstance = new Base(applicationContext);
        }
        return sInstance;
    }

    /**
     * private constructor prevent direct instantiation.
     */
    private Base(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

}
公共静态最终字符串DB\u NAME=“DB\u workout2”;//基的名称 公共静态最终字符串DB_TABLE=“dateOfWorkout2”//表名 公共静态最终整数DB_VER=1; 私人静态基地站; 公共静态同步基本getInstance(上下文应用程序上下文){ if(sInstance==null){ sInstance=newbase(applicationContext); } 回归承诺; } 私有基(上下文上下文、字符串名称、SQLiteDatabase.CursorFactory工厂、int版本){ super(上下文、数据库名称、null、数据库版本); } @凌驾 public void onCreate(SQLiteDatabase db){ db.execSQL( “创建表格”+DB_表格+”(“ +nr整数主键自动递增 +“名称文本,” +“s1整数,” +s2整数 +s3整数 +“s4整数,” +s5整数 +“权重整数,” +“ddate文本);” +""); } 公共void addWorkoutPlan(字符串i、整数q、整数w、整数e、整数r、整数t、整数wt、字符串d){ SQLiteDatabase db=getWritableDatabase(); ContentValues wartosci=新ContentValues(); 瓦托西。put(“名称”,i); 瓦托西.普特(s1),q); 瓦托西。普特(“s2”,w); 瓦托西.普特(“s3”,e); 瓦托西.普特(“s4”,r); 瓦托西.普特(s5),t); 瓦托西重量(重量); 瓦托西.普特(“ddate”,d); db.insertOrThrow(db_表,null,wartosci); } public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){ db.execSQL(“如果存在删除表”+db_表); onCreate(db); } 公共列表getDateLabels(){ 列表标签=新的ArrayList(); //选择所有查询 String selectQuery=“SELECT*FROM”+DB\u表; SQLiteDatabase db=this.getReadableDatabase(); Cursor Cursor=db.rawQuery(selectQuery,null); //循环遍历所有行并添加到列表 if(cursor.moveToFirst()){ 做{ labels.add(cursor.getString(1)); }while(cursor.moveToNext()); } //闭合连接 cursor.close(); db.close(); //退回标签 退货标签; }

}

[link]现在看起来是这样的。你在使用哪个IDE?Android Studio 2.1.2mean更新了类代码并修复了未实现的问题,现在它应该可以工作了,试着构建你的项目。看看我的答案,尽管构建了项目,但仍然有错误