gpt4 book ai didi

java - 无法获得数据库连接,因为我无法将上下文传递给 DBManager

转载 作者:行者123 更新时间:2023-12-02 10:12:05 24 4
gpt4 key购买 nike

我有一个虚拟数据管理器,它将用 SQLite 数据库的内容填充 ArrayList。当我尝试创建数据库对象时,出现错误,指出无法从静态上下文引用“com.example.listview.manager.ActivityDummyDataManager.this”。

package com.example.listview.manager;

import android.database.Cursor;
import com.example.listview.DBManager;
import com.example.listview.model.ActivityItem;
import java.util.ArrayList;


public class ActivityDummyDataManager {


public static ArrayList<ActivityItem> getActivityItemList() {
DBManager db = new DBManager(this);
Cursor cursor = db.fetch();
ArrayList<com.example.listview.model.ActivityItem> list = new ArrayList<>();
while (cursor.moveToNext()) {
int index;
index = cursor.getColumnIndexOrThrow("id");
Long id = cursor.getLong(index);

index = cursor.getColumnIndexOrThrow("activity");
String activity = cursor.getString(index);

index = cursor.getColumnIndexOrThrow("description");
String description = cursor.getString(index);

index = cursor.getColumnIndexOrThrow("description");
String date = cursor.getString(index);
ActivityItem item = new ActivityItem();
item.setId(id);
item.setActivity(activity);
item.setDescription(description);
item.setDate(date);
list.add(item);
}
return list;
}
}

我有一个 DBManager 类,通常使用 DBManager db = new DBManager(this) 访问,但由于某种原因,这在此类中不起作用。

package com.example.listview;

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

public class DBManager {
private DatabaseHelper dbHelper;
private Context context;
private SQLiteDatabase database;

public DBManager(Context c) {
context = c;
}

public DBManager open() throws SQLException {
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}

public void close() {

dbHelper.close();
}

public void insert(String activity, String description, String date) {
ContentValues contentValue = new ContentValues();
contentValue.put(DatabaseHelper.ACTIVITY, activity);
contentValue.put(DatabaseHelper.DESCRIPTION, description);
contentValue.put(DatabaseHelper.DATE, date);
database.insert(DatabaseHelper.TABLE_NAME, null, contentValue);
}

public Cursor fetch() {
String[] columns = new String[] {
DatabaseHelper._ID,
DatabaseHelper.ACTIVITY,
DatabaseHelper.DESCRIPTION,
DatabaseHelper.DATE
};
Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, columns, null,
null, null, null, "DATE DESC");
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}

public Cursor fetchForEdit(String id) {
String[] columns = new String[] {
DatabaseHelper._ID,
DatabaseHelper.ACTIVITY,
DatabaseHelper.DESCRIPTION,
DatabaseHelper.DATE
};

String [] args = new String [] {id};
Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, columns, "_ID=?",
args, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
Log.d("DATABASE", "cursor= "+cursor.getColumnName(1));
return cursor;
}


public int update(long _id, String activity, String description, String date) {
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.ACTIVITY, activity);
contentValues.put(DatabaseHelper.DESCRIPTION, description);
contentValues.put(DatabaseHelper.DATE, date);
int i = database.update(
DatabaseHelper.TABLE_NAME, contentValues,
DatabaseHelper._ID + " = " + _id,
null);
return i;
}

public void delete(long _id) {
database.delete(DatabaseHelper.TABLE_NAME,
DatabaseHelper._ID + "=" + _id,
null);
}
}

为什么我不能设置DBManager db = new DBManager(this);获得数据库连接。说“com.example.listview.manager.ActivityDummyDataManager.this”无法从静态上下文中引用。有人还可以解释一下上下文参数吗,因为我不太明白。

最佳答案

您的构造函数 DBManager(Context c) 需要一个上下文作为参数,如果您从 Activity 或 Service 类调用此构造函数,它将起作用,因为 Activity 和 Service 都是 Context 的子类,因此传递这个应该足够了。由于 ActivityDummyDataManager 不处理上下文,因此您需要从 Activity 传递 Context,或者可以传递应用程序上下文 getApplicationContext()

按如下方式更改您的方法

public static ArrayList<ActivityItem> getActivityItemList() {
DBManager db = new DBManager(mContext);
//rest of your code.
}

并且在调用它时使用合适的 Activity 或应用程序上下文。

关于java - 无法获得数据库连接,因为我无法将上下文传递给 DBManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54944103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com