无法在Android Studio中解析方法getWritableDatabase()

时间:2015-10-28 20:02:42

标签: android sqlite android-studio android-sqlite anonymous-inner-class

我第一次在Android中使用SQLite。我想补充新的危机可以在后​​台完成。但是当我调用写入数据库的引用时遇到错误。我尝试使用asyncTasck方法在数据库中添加新的危机。不起作用的代码在方法 void addCrisi(Crisi crisi)中。这是它:

// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();

完整代码是:

 public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "crisisManager";

// Crisis table name
private static final String TABLE_CRISIS = "crisis";
//Singleton
private static DatabaseHandler instance;
public static DatabaseHandler getInstance(Context context) {
    if (instance == null) {
        instance = new DatabaseHandler(context.getApplicationContext());
    }
    return instance;
}

// Crisis Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_START_DATE = "start_date";
private static final String KEY_END_DATE = "end_date";
private static final String KEY_LATITUDE = "latitude";
private static final String KEY_LONGITUDE = "longitude";

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

// Creating Tables
public void onCreate(SQLiteDatabase db) {
    String CREATE_CRISIS_TABLE = "CREATE TABLE " + TABLE_CRISIS + "("
            + KEY_ID + " INTEGER PRIMARY KEY,"
            + KEY_START_DATE + " TEXT,"
            + KEY_END_DATE + " TEXT,"
            + KEY_LATITUDE + " DOUBLE,"
            + KEY_LONGITUDE + " DOUBLE" + ")";
    db.execSQL(CREATE_CRISIS_TABLE);
}

// Upgrading database
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CRISIS);
    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */
// Adding new crisi
void addCrisi(Crisi crisi) {
    Log.d("Add Crisi", crisi.toString());
    //SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_START_DATE, crisi.getStartDate()); // Start Date
    values.put(KEY_END_DATE, crisi.getEndDate()); // End Date
    values.put(KEY_LATITUDE, crisi.getLatitude()); // Latitude
    values.put(KEY_LONGITUDE, crisi.getLongitude()); // Longitude

    // Inserting Row Async Inserting
    //AsyncTask is anonymous class
    new AsyncTask<ContentValues, Void, Void>() {
        @Override
        protected Void doInBackground(ContentValues... params) {

            // 1. get reference to writable DB
            SQLiteDatabase db = this.getWritableDatabase();

            // 3. insert
            db.insert(TABLE_CRISIS, null, params[0]);

            // 4. close
            db.close();
            return null;
        }
    }.execute(values);
    //db.insert(TABLE_CRISIS, null, values);
    //db.close(); // Closing database connection
}
// Getting All Crisis
public List<Crisi> getAllCrisis() {
    List<Crisi> crisiList = new ArrayList<Crisi>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CRISIS;

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Crisi crisi = new Crisi();
            crisi.setId(Integer.parseInt(cursor.getString(0)));
            //crisi.setName(cursor.getString(1));
            //crisi.setPhoneNumber(cursor.getString(2));
            crisi.setStartDate(cursor.getString(1));
            crisi.setEndDate(cursor.getString(2));
            crisi.setLatitude(Double.parseDouble(cursor.getString(3)));
            crisi.setLongitude(Double.parseDouble(cursor.getString(4)));
            // Adding crisi to list
            crisiList.add(crisi);
        } while (cursor.moveToNext());
    }

    // return crisi list
    return crisiList;
}

// Updating single crisi
public int updateCrisi(Crisi crisi) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_START_DATE, crisi.getStartDate());
    values.put(KEY_END_DATE, crisi.getEndDate());
    values.put(KEY_LATITUDE, crisi.getLatitude()); // Latitude
    values.put(KEY_LONGITUDE, crisi.getLongitude()); // Longitude
    values.put(KEY_LOCALITY, crisi.getLatitude()); // Locality
    values.put(KEY_COUNTRY, crisi.getLongitude()); // Country

    // updating row
    return db.update(TABLE_CRISIS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(crisi.getId()) });
}

// Deleting single crisi
public void deleteCrisi(Crisi crisi) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CRISIS, KEY_ID + " = ?",
            new String[] { String.valueOf(crisi.getId()) });
    db.close();
}

// Getting crisis Count
public int getCrisisCount() {
    String countQuery = "SELECT  * FROM " + TABLE_CRISIS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}
}

有人可以帮忙吗?谢谢! Ps:我提前为我糟糕的英语道歉。

1 个答案:

答案 0 :(得分:4)

this指的是内部Asynctask而不是DatabaseHandler。使用DatabaseHandler.this(一般为NameOuter.this)来引用内部类

中的外部类