在调用超类型构造函数之前,不能引用MySQLiteOpenHelper.context

时间:2015-06-28 15:54:13

标签: android constructor sqliteopenhelper

构造函数中的上下文中有一行。我不确定为什么。而且我不确定为什么我被告知公开MySQLiteHelper(DisplayResult displayResult)。 DisplayResult是程序中的另一个类。我在底部包含了该代码。提前谢谢。

以下是代码:

public class MySQLiteHelper extends SQLiteOpenHelper {

private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "promisesdatabase.sqlite";
private String DB_PATH = "/data/data/com.blogspot.joyouslybeingjoy.cybpromises/";

public static final String TABLE_NAME = "PromisesCYB";

public static final String KEY_ROWID = "id";
public static final String KEY_CATEGORY = "category";
public static final String KEY_BOOK = "book";
public static final String KEY_CHAPTER = "chapter";
public static final String KEY_VERSE = "verse";
public static final String KEY_WORD = "word";

private SQLiteDatabase myDataBase;
private final Context myContext;
private Context context;

public MySQLiteHelper(DisplayResult displayResult) throws IOException {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

    if(android.os.Build.VERSION.SDK_INT >= 17){
        DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
    }
    else
    {
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }

    this.myContext = context;
}

public void createDataBase() throws IOException
{
    //If database not exists copy it from the assets

    boolean mDataBaseExist = checkDataBase();
    if(!mDataBaseExist)
    {
        this.getReadableDatabase();
        this.close();
        try
        {
            //Copy the database from assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        }
        catch (IOException mIOException)
        {
            throw new Error("ErrorCopyingDataBase");
        }
    }
}
//Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase()
{
    File dbFile = new File(DB_PATH + DATABASE_NAME);
    //Log.v("dbFile", dbFile + "   "+ dbFile.exists());
    return dbFile.exists();
}

//Copy the database from assets
private void copyDataBase() throws IOException
{
    InputStream mInput = myContext.getAssets().open(DATABASE_NAME);
    String outFileName = DB_PATH + DATABASE_NAME;
    OutputStream mOutput = new FileOutputStream(outFileName);
    byte[] mBuffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(mBuffer))>0)
    {
        mOutput.write(mBuffer, 0, mLength);
    }
    mOutput.flush();
    mOutput.close();
    mInput.close();
}

//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
    String mPath = DB_PATH + DATABASE_NAME;
    //Log.v("mPath", mPath);
    myDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
    //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    return myDataBase != null;
}

@Override
public synchronized void close()
{
    if(myDataBase != null)
        myDataBase.close();
    super.close();
}



@Override
public void onCreate(SQLiteDatabase promisesDB) {
    String DATABASE_CREATE =
            "CREATE TABLE if not exists " + TABLE_NAME + " ( " +
                    KEY_ROWID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
                    KEY_CATEGORY + " TEXT, " +
                    KEY_BOOK + " TEXT, " +
                    KEY_CHAPTER + " INTEGER, " +
                    KEY_VERSE + " INTEGER, " +
                    KEY_WORD + " TEXT " + " )";
    promisesDB.execSQL(DATABASE_CREATE);

}

@Override
public void onUpgrade(SQLiteDatabase promisesDB, int oldVersion, int newVersion) {

}


public void selectQuery(String query) {
    String query1 = ("SELECT * FROM " + MySQLiteHelper.TABLE_NAME +
            " WHERE KEY_CATEGORY = ?" + new String[]{DisplayResult.selectedCategory});

}

}

这是DisplayResult代码:

 public class DisplayResult extends Activity {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "promisesdatabase.sqlite";

private Context context;

static String selectedCategory;
MySQLiteHelper sqlHandler;
ListView lvCustomList;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verses);

    Bundle b = getIntent().getExtras();
    String selectedCategory = b.getString("ITEMVALUE");

    //Toast.makeText(DisplayResult.this, "Category passed: " + selectedCategory,
    // Toast.LENGTH_LONG).show();

    lvCustomList = (ListView) findViewById(R.id.listViewItems);

    try {
        sqlHandler = new MySQLiteHelper(this);
    } catch (IOException e) {
        e.printStackTrace();
    }

    showList();

}

private void showList() {

    ArrayList<Promise> verses = new ArrayList<Promise>();
    verses.clear();

    String query = ("SELECT * FROM " + MySQLiteHelper.TABLE_NAME +
            " WHERE KEY_CATEGORY = ?" + new String[]{selectedCategory});

    Cursor c1 = MySQLiteHelper.selectQuery(query);
        if (c1 != null && c1.getCount() != 0) {
            do {
                Promise verse = new Promise();
                verse.setCategory(c1.getString(c1.getColumnIndex("category")));
                verse.setBook(c1.getString(c1.getColumnIndex("book")));
                verse.setChapter(Integer.parseInt(String.valueOf(c1.getColumnIndex("chapter"))));
                verse.setVerse(Integer.parseInt(String.valueOf(c1.getColumnIndex("verse"))));
                verse.setWord(c1.getString(c1.getColumnIndex("word")));
                verses.add(verse);
            } while (c1.moveToNext());
        }
    c1.close();

    PromiseListAdapter promiseListAdapter = new PromiseListAdapter (DisplayResult.this, verses);
    lvCustomList.setAdapter(promiseListAdapter);

}

1 个答案:

答案 0 :(得分:0)

MySQLiteHelper构造函数中将参数名称更改为上下文(以及类型)并删除private Context context行;

你的构造函数应该是:

public MySQLiteHelper(Context context) throws IOException {...