由于无法将上下文传递给DBManager,因此无法获得数据库连接

时间:2019-03-01 11:51:13

标签: java android android-sqlite

我有一个虚拟数据管理器,它将使用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” 不能从静态上下文中引用。 有人可以解释一下上下文参数,因为我不太了解。

1 个答案:

答案 0 :(得分:1)

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

如下更改您的方法

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

并在调用它时使用活动或应用程序上下文中合适的一个。