- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在研究一种在 Activity 之间传递引用到我的 DataBaseHelper 类的方法。我不想在每个新 Activity 中重新创建 DataBaseHelper 的新实例。据我所知,最好的方法是实现 android.os.parcelable,这很好。但是,当我尝试使用以下方法重写 DataBaseHelper 构造函数时:
public DataBaseHelper(Parcel source)
我收到一条错误消息,告诉我构造函数未定义。我有点明白这意味着什么,但不确定如何解决这个问题,因此在此类中实现 Parcelable。下面是DatabaseHelper类,可分割的代码在底部。
package com.drager;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper implements Parcelable{
//private static String DB_PATH = "/data/data/com.drager/databases/";
private static String DB_PATH = Environment.getDataDirectory()+"/data/com.drager/databases/";
final static String DB_NAME = "myDBName";
private SQLiteDatabase myDataBase=null;
private final Context myContext;
private DataBaseHelper myDbHelper;
private static String TAG ="MyActivity";
public DataBaseHelper(Context context){
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public DataBaseHelper(Parcel source) {
super(source);
// TODO Auto-generated constructor stub
}
public DataBaseHelper createDataBase() throws IOException{
boolean dbExist =checkDataBase();
//SQLiteDatabase db_read =null;
Log.i(TAG,"############value of dbExist"+dbExist+"##########");
if (dbExist){
copyDataBase();
//db must exist
}
else{
myDbHelper = new DataBaseHelper(myContext);
myDataBase = myDbHelper.getReadableDatabase();
myDataBase.close();
//this.getReadableDatabase();
//db_read.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("error copying database");
}
}
return this;
}
public Cursor executeStatement(){
Log.i(TAG,"in execute statement");
Cursor cursor=null;
cursor=myDataBase.rawQuery("SELECT _ID, title, value "+
"FROM constants ORDER BY title",
null);
return cursor;
}
public String[] getTextViewItem(){
Cursor cursor=null;
String str="";
String[] resultsString;
//store query results in cursor
cursor=myDataBase.rawQuery("SELECT shrt_description FROM description",
null);
ArrayList strings = new ArrayList();
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
str =cursor.getString(cursor.getColumnIndex("shrt_description"));
strings.add(str);
}
resultsString =(String[])strings.toArray(new String[strings.size()]);
close();//close database after use
return resultsString;
}
public String[] getDetailedDescription(){
Cursor cursor=null;
String str="";
String[] resultsString;
//store query results in cursor
cursor=myDataBase.rawQuery("SELECT detailed_description FROM description",
null);
ArrayList strings = new ArrayList();
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
str =cursor.getString(cursor.getColumnIndex("detailed_description"));
strings.add(str);
}
resultsString =(String[])strings.toArray(new String[strings.size()]);
close();//close database after use
return resultsString;
}
public void copyDataBase() throws IOException{
// open db as input stream
InputStream myInput;
//open empty db as output stream
OutputStream myOutPut;
try {
myInput = myContext.getAssets().open(DB_NAME);
//path to newly created db
String outFileName =DB_PATH + DB_NAME;
myOutPut = new FileOutputStream(outFileName);
//transfer bytes from the inputFile to the outPutFile
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer))>0){
myOutPut.write(buffer, 0, length);
}
myOutPut.flush();
myOutPut.close();
myInput.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
String myPath = DB_PATH + DB_NAME;
try {
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLException e) {
e.printStackTrace();
return false;
}
if (checkDB != null){
checkDB.close();
}
return true;
//return checkDB !=null ? true : false;
}
public void openDataBase()throws SQLException{
//open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close(){
if(myDataBase != null){
myDataBase.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeParcelable((Parcelable) myDataBase, 0);
dest.writeString(DB_PATH);
dest.writeString(DB_NAME);
dest.writeString(TAG);
dest.writeParcelable((Parcelable) myContext, 0);
dest.writeParcelable(myDbHelper, 0);
}
public static final Parcelable.Creator<DataBaseHelper> CREATOR = new Parcelable.Creator<DataBaseHelper>() {
@Override
public DataBaseHelper createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new DataBaseHelper(source);
}
@Override
public DataBaseHelper[] newArray(int size) {
// TODO Auto-generated method stub
return null;
}
};
}
非常感谢任何帮助。提前致谢。
最佳答案
你似乎让事情变得不必要地复杂化了。你说:
I don't want to recreate new instances of DataBaseHelper in each new activity. From what I have seen, the best way to do this is to implement android.os.parcelable, which is fine.
但是从 Parcel 恢复 DataBaseHelper 对象确实会创建 DataBaseHelper 类的新实例;这只是比创建新的 DataBaseHelper 更困难的方法。
无论如何,虽然您没有在此处复制并粘贴错误消息,但我想我知道您收到了什么错误:这并不是说您的 DataBaseHelper 类不包含正确的构造函数;而是您的 DataBaseHelper 类不包含正确的构造函数。确实如此。这是因为父类(super class) SQLiteOpenHelper
没有实现 Parcelable
。解决此问题的唯一方法是自己管理 SQLiteOpenHelper 的所有状态。也就是说,将 SQLiteOpenHelper 的状态作为 writeToParcel
实现的一部分包含在内,并将该状态作为 DatabaseHelper(Parcel)
的一部分恢复。然后,构造函数将调用 SQLiteOpenHelper 的默认构造函数。生成的构造函数看起来像这样:
public DataBaseHelper(Parcel source, Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
// NOTE: You've got to pass in an appropriate Context; I'm sure it would not work
// to try to include the original Activity in the Parcel! That means that your
// constructor **must** include the Context as one of its arguments. Conceivably,
// the name, factory, and version could be taken from the Parcel rather than being
// passed in the constructor arguments. Again, I ask: Is it worth the hassle?
super(context, name, factory, version);
// First, restore any relevant `SQLiteOpenHelper` state...
//...
// Now restore any relevant DataBaseHelper state...
//...
}
我认为这可能不值得付出努力,但这取决于您的决定。
关于java - 在 DataBaseHelper 类中实现 Parcelable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4993667/
引用this链接,如果只有一个连接,什么时候关闭连接比较合适?我有一个服务一直在后台运行。什么时候调用 close() 函数比较合适?有什么建议吗? 最佳答案 您实际上不必调用 close()。例如,
关于这个主题有几个问题,但没有一个给出明确的理由说明为什么应该或不应该有一个或多个 databaseHelper 实例。什么时候拥有多个 DatabaseHelper 实例是个好主意,什么时候不是。复
也许我遗漏了一些东西,但我的问题是:我可以从代码的不同点访问数据库,每个代码点都使用自己的 DatabaseHelper 实例吗?还是拥有一个可从任何地方(线程、 Activity 等)访问的独特的全
大家好,我一直在开发一个简单的 Chronometer 应用程序,该应用程序将单圈时间存储在数据库中。我创建了一个数据库助手类。当我尝试从我的 Activity 中插入数据时,我收到一条错误消息,指出
我需要将一些默认数据存入数据库。我在我的数据库助手中制作了方法: private void putDefaultList(SQLiteDatabase db){ String nazwaa =
大家好我被困在一个点上,问题是我有下面显示的三个类,我想在 AsyncTask 类中实例化我的 DatabaseHelper 类。你能帮忙吗,我怎样才能在 AsyncTask 类中获取上下文? 问题已
我有一个安卓应用程序,通过按下按钮从服务器同步数据。但是我需要按钮出现在应用程序的所有 Activity 中,所以我决定将 updateTask 作为独立类。 在 updateTask 中,我下载了一
我一直在研究一种在 Activity 之间传递引用到我的 DataBaseHelper 类的方法。我不想在每个新 Activity 中重新创建 DataBaseHelper 的新实例。据我所知,最好的
我正在主 Activity 的 onCreate() 中创建 DBHelper 类的实例。 databaseHelper = new DatabaseHelper(getBaseContext())
我查看了上下文信息,它在 Activity 组的已知间接子类中说,“此类已弃用。请改用新的 Fragment 和 FragmentManager API” 那么为什么下面的代码在我的类中不起作用扩展
我正在尝试使用 OrmLite 保存我的数据。在这个例子中有这个类: package com.example.helloandroid; import java.sql.SQLException; i
我想在我的 Activity 中对 Order 对象调用 CRUD 操作。我想知道以下“服务”类的实现是否是执行此操作的好方法?我不希望在我的 Activity 代码中引用 DatabaseHelpe
我有一个 Activity 使用 AsyncTask 下载一些数据并将其保存到数据库中。要打开数据库,我们需要将 Context 参数传递给 SQLiteOpenHelper 的 onCreate 方
数据库帮助.java package com.example.abc2; import java.io.FileOutputStream; import java.io.IOException; im
根据 ORMLite 文档,所有创建的 Dao 对象都缓存在 DaoManager 中。但是在 ORMLite 中 examples ,我看到 Dao 类再次缓存在 DatabaseHelper 类中
我有一个数据库助手类(下面的代码)。这个助手的类任务是将数据库从我的应用程序附带的 Assets 文件夹复制到我的应用程序的 data\data... 中,以便我可以使用它。 一旦我将数据库放入数据\
您好,我在运行时使用 Android Studio 时遇到此错误,但仅限于 SDK 版本 1.3.0 sdk版本:compileSdkVersion 19 > 23 应用程序兼容性:com.andr
DatabaeHelper.class 这是一个普通类。但我无法插入值并从数据库中取回数据。 public class DatabaseHelper extends SQLiteOp
我在阅读Android 2.2默认启动器的源代码时遇到了一个问题。 LauncherProvider.DatabaseHelper源码段: @Override public void onCreate
我是一名优秀的程序员,十分优秀!