使用来自两个单独数据库文件的数据填充列表视图

时间:2017-02-05 00:32:54

标签: android database

我有一个listview我填充了数据库文件中的数据,我使用' Selected_Database_File_Name'传递了文件的名称。哪个有效,但是当我关闭数据库并使用' Selected_Database_File_Name'传递新的数据库文件名之后并填充列表视图,它仍然填充以前的数据库文件值。在我尝试使用新值填充listview之前,我将数组适配器设置为null。知道为什么它不会用新文件值重新加载吗?

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import static com.example.cryptonote.MainActivity.Selected_Database_File_Name;

public class NotesDbAdapter {

public static final String KEY_TITLE = "title";
public static final String KEY_DATE = "date";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "_id";
private static final String TAG = "NotesDbAdapter";
public DatabaseHelper mDbHelper;
public SQLiteDatabase mDb;

private static final String DATABASE_CREATE =
    "create table notes (_id integer primary key autoincrement, "
    + "title text not null, body text not null, date text not null);";

private static final String DATABASE_NAME = Selected_Database_File_Name;
private static final String DATABASE_TABLE = "notes";
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS notes");
        onCreate(db);
    }
}

public NotesDbAdapter(Context ctx) {
    this.mCtx = ctx;
}

public NotesDbAdapter open() throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

public void close() {
    mDbHelper.close();
}

public long createNote(String title, String body, String date) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_BODY, body);
    initialValues.put(KEY_DATE, date);
    return mDb.insert(DATABASE_TABLE, null, initialValues);
}

public boolean deleteNote(long rowId) {
    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

public Cursor fetchAllNotes() {
    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
            KEY_BODY,KEY_DATE}, null, null, null, null, null);
}

public Cursor fetchNote(long rowId) throws SQLException {
    Cursor mCursor =
        mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                KEY_TITLE, KEY_BODY,KEY_DATE}, KEY_ROWID + "=" + rowId, null,
                null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

public boolean updateNote(long rowId, String title, String body,String date) {
    ContentValues args = new ContentValues();
    args.put(KEY_TITLE, title);
    args.put(KEY_BODY, body);
    args.put(KEY_DATE, date);
    return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) >        0;
}
public boolean isOpen() {
    return         mDb.isOpen();
}
}

1 个答案:

答案 0 :(得分:0)

如果您没有重新创建DATABASE_NAME为private static final的对象,那么它就不会更改该值。