onCreate 不是 SQLiteOpenHelper 的构造函数,但我需要它用于 getIntet

发布于 2024-12-10 05:49:55 字数 6697 浏览 0 评论 0原文

我需要使用 getIntent,但不能这样做,因为据我了解,它不是 SQLiteOpenHelper 的构造函数,并且 getIntent 需要在 onCreate 中使用。

在这种情况下,我如何从 SQLiteOpenHelper 中访问它?

基本上,我试图让以下代码在 SQLiteOpenHelper 中工作,但遇到了麻烦。

        public void onCreate(Bundle savedInstanceState) {
        onCreate(savedInstanceState);
        sortTypePassed=getIntent().getStringExtra(SmithRockScreenActivity.SORT_TYPE_VAR);
    }

我正在尝试根据 String 的值对数据库进行查询排序。就像这样:

  public Cursor getCursor() {



    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    queryBuilder.setTables(TABLE_NAME);

    String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE, COLUMN_YDSRATING, COLUMN_ROUTETYPE, COLUMN_AREA, COLUMN_ROUTEVIDEO, COLUMN_ROUTEDESCRIPTION};

    if (sortTypePassed.equals("nameSort")) { 

        Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                null, null, null, "route_name ASC");

        return mCursor;

    } else {

        Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                null, null, null, "yds_rating ASC");

        return mCursor;

    }

这是完整的代码:

package com.pocketbeta;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.os.Bundle;
 import android.util.Log;


class SmithRockDatabase extends SQLiteOpenHelper {

public String sortTypePassed = "null";

private static final String DATABASE_PATH = "/data/data/com.pocketbeta/databases/";
private static final String DATABASE_NAME="smithrockdb.db";
private static final int SCHEMA_VERSION=1;
public static final String TABLE_NAME = "SmithRockRoutes";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "route_name";
public static final String COLUMN_YDSRATING = "yds_rating";
public static final String COLUMN_QUALITY = "quality";
public static final String COLUMN_IMAGE = "route_photo";
public static final String COLUMN_ROUTEVIDEO = "route_video";
public static final String COLUMN_TICKLIST = "ticklist";
public static final String COLUMN_AREA = "area";
public static final String COLUMN_ROUTETYPE = "route_type";
public static final String COLUMN_ROUTEDESCRIPTION = "route_description";

public SQLiteDatabase dbSqlite;

private final Context myContext;

public SmithRockDatabase(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    this.myContext = context;
    // check if exists and copy database from resource
    //createDB();
}


@Override
public void onCreate(SQLiteDatabase db) {
    // check if exists and copy database from resource

}

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

}
public void createDatabase() {
    createDB();
}

private void createDB() {

    boolean dbExist = DBExists();

    if (!dbExist) {

        //By calling this method we create an empty database into the default system location
        //We need this so we can overwrite that database with our database.
        this.getReadableDatabase();
        //now we copy the database we included!
        copyDBFromResource();


    }



}    

private boolean DBExists() {

    SQLiteDatabase db = null;

    try {
        String databasePath = DATABASE_PATH + DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(databasePath, null,
                SQLiteDatabase.OPEN_READWRITE);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        db.setVersion(1);



    } catch (SQLiteException e) {

        Log.e("SqlHelper", "database not found");

    }

    if (db != null) {


        db.close();

    }


    return db != null ? true : false;
}

private void copyDBFromResource() {

    InputStream inputStream = null;
    OutputStream outStream = null;
    String dbFilePath = DATABASE_PATH + DATABASE_NAME;

    try {

        inputStream = myContext.getAssets().open(DATABASE_NAME);

        outStream = new FileOutputStream(dbFilePath);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }

        outStream.flush();
        outStream.close();
        inputStream.close();

    } catch (IOException e) {

        throw new Error("Problem copying database from resource file.");

    }

}

public void openDataBase() throws SQLException {

    String myPath = DATABASE_PATH + DATABASE_NAME;
    dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

}

@Override
public synchronized void close() {

    if (dbSqlite != null)
    {
        dbSqlite.close();
    }
    super.close();

}

// NEED TO GET THIS CODE TO WORK TO BRING IN VALUE FROM OTHER INTENT!!!!!!!!!!!!!!!!!!

//  public void onCreate(Bundle savedInstanceState) {
//  onCreate(savedInstanceState);
//  sortTypePassed=getIntent().getStringExtra(SmithRockScreenActivity.SORT_TYPE_VAR);
//}
// }


public Cursor getCursor() {



    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    queryBuilder.setTables(TABLE_NAME);

    String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE, COLUMN_YDSRATING, COLUMN_ROUTETYPE, COLUMN_AREA, COLUMN_ROUTEVIDEO, COLUMN_ROUTEDESCRIPTION};

    if (sortTypePassed.equals("nameSort")) { 

        Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                null, null, null, "route_name ASC");

        return mCursor;

    } else {

        Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                null, null, null, "yds_rating ASC");

        return mCursor;

    }

}


public String getName(Cursor c) {


    return(c.getString(1));

}

public String getRating(Cursor c) {
    return(c.getString(2));
}
public String getArea(Cursor c) {
    return(c.getString(4));
}
public String getRouteVideo(Cursor c) {
    return(c.getString(5));
}
public String getRouteDescription(Cursor c) {
    return(c.getString(6));
}

public String getRouteType(Cursor c) {

    String routeValue="";
    routeValue=(c.getString(3));
    String noSelection = "";

    if (routeValue != null) {
        return c.getString(3);

    } else {
        return(noSelection);

    }


}

}

I need to use the getIntent, but can't do so because from what I undertand, it is not a constructor of SQLiteOpenHelper, and getIntent needs to be used from within an onCreate.

This being the case,how would I get access to it from within the SQLiteOpenHelper?

Basically, I am trying to get the following code to work from within the SQLiteOpenHelper, but am having troubles.

        public void onCreate(Bundle savedInstanceState) {
        onCreate(savedInstanceState);
        sortTypePassed=getIntent().getStringExtra(SmithRockScreenActivity.SORT_TYPE_VAR);
    }

I'm trying to do a query sort of the database, based on the value of String. Like so:

  public Cursor getCursor() {



    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    queryBuilder.setTables(TABLE_NAME);

    String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE, COLUMN_YDSRATING, COLUMN_ROUTETYPE, COLUMN_AREA, COLUMN_ROUTEVIDEO, COLUMN_ROUTEDESCRIPTION};

    if (sortTypePassed.equals("nameSort")) { 

        Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                null, null, null, "route_name ASC");

        return mCursor;

    } else {

        Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                null, null, null, "yds_rating ASC");

        return mCursor;

    }

Here is the code in its entire length:

package com.pocketbeta;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.os.Bundle;
 import android.util.Log;


class SmithRockDatabase extends SQLiteOpenHelper {

public String sortTypePassed = "null";

private static final String DATABASE_PATH = "/data/data/com.pocketbeta/databases/";
private static final String DATABASE_NAME="smithrockdb.db";
private static final int SCHEMA_VERSION=1;
public static final String TABLE_NAME = "SmithRockRoutes";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "route_name";
public static final String COLUMN_YDSRATING = "yds_rating";
public static final String COLUMN_QUALITY = "quality";
public static final String COLUMN_IMAGE = "route_photo";
public static final String COLUMN_ROUTEVIDEO = "route_video";
public static final String COLUMN_TICKLIST = "ticklist";
public static final String COLUMN_AREA = "area";
public static final String COLUMN_ROUTETYPE = "route_type";
public static final String COLUMN_ROUTEDESCRIPTION = "route_description";

public SQLiteDatabase dbSqlite;

private final Context myContext;

public SmithRockDatabase(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    this.myContext = context;
    // check if exists and copy database from resource
    //createDB();
}


@Override
public void onCreate(SQLiteDatabase db) {
    // check if exists and copy database from resource

}

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

}
public void createDatabase() {
    createDB();
}

private void createDB() {

    boolean dbExist = DBExists();

    if (!dbExist) {

        //By calling this method we create an empty database into the default system location
        //We need this so we can overwrite that database with our database.
        this.getReadableDatabase();
        //now we copy the database we included!
        copyDBFromResource();


    }



}    

private boolean DBExists() {

    SQLiteDatabase db = null;

    try {
        String databasePath = DATABASE_PATH + DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(databasePath, null,
                SQLiteDatabase.OPEN_READWRITE);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        db.setVersion(1);



    } catch (SQLiteException e) {

        Log.e("SqlHelper", "database not found");

    }

    if (db != null) {


        db.close();

    }


    return db != null ? true : false;
}

private void copyDBFromResource() {

    InputStream inputStream = null;
    OutputStream outStream = null;
    String dbFilePath = DATABASE_PATH + DATABASE_NAME;

    try {

        inputStream = myContext.getAssets().open(DATABASE_NAME);

        outStream = new FileOutputStream(dbFilePath);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }

        outStream.flush();
        outStream.close();
        inputStream.close();

    } catch (IOException e) {

        throw new Error("Problem copying database from resource file.");

    }

}

public void openDataBase() throws SQLException {

    String myPath = DATABASE_PATH + DATABASE_NAME;
    dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

}

@Override
public synchronized void close() {

    if (dbSqlite != null)
    {
        dbSqlite.close();
    }
    super.close();

}

// NEED TO GET THIS CODE TO WORK TO BRING IN VALUE FROM OTHER INTENT!!!!!!!!!!!!!!!!!!

//  public void onCreate(Bundle savedInstanceState) {
//  onCreate(savedInstanceState);
//  sortTypePassed=getIntent().getStringExtra(SmithRockScreenActivity.SORT_TYPE_VAR);
//}
// }


public Cursor getCursor() {



    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    queryBuilder.setTables(TABLE_NAME);

    String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE, COLUMN_YDSRATING, COLUMN_ROUTETYPE, COLUMN_AREA, COLUMN_ROUTEVIDEO, COLUMN_ROUTEDESCRIPTION};

    if (sortTypePassed.equals("nameSort")) { 

        Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                null, null, null, "route_name ASC");

        return mCursor;

    } else {

        Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                null, null, null, "yds_rating ASC");

        return mCursor;

    }

}


public String getName(Cursor c) {


    return(c.getString(1));

}

public String getRating(Cursor c) {
    return(c.getString(2));
}
public String getArea(Cursor c) {
    return(c.getString(4));
}
public String getRouteVideo(Cursor c) {
    return(c.getString(5));
}
public String getRouteDescription(Cursor c) {
    return(c.getString(6));
}

public String getRouteType(Cursor c) {

    String routeValue="";
    routeValue=(c.getString(3));
    String noSelection = "";

    if (routeValue != null) {
        return c.getString(3);

    } else {
        return(noSelection);

    }


}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不服输 2024-12-17 05:49:56

既然如此,我如何从 SQLiteOpenHelper 中访问它?

理想情况下,您无法从 SQLiteOpenHelper 访问 getIntent()。您在 SQLiteOpenHelper 上实现一个构造函数(提示:您已经有了一个),并传入它需要的任何数据。理想情况下,这涉及传递活动,而是涉及活动从任何地方获取的值(例如,Intent extras)。

This being the case,how would I get access to it from within the SQLiteOpenHelper?

Ideally, you don't get access to getIntent() from SQLiteOpenHelper. You implement a constructor on SQLiteOpenHelper (hint: you already have one) and you pass in whatever data it needs. Ideally, this does not involve passing in the activity, but rather values the activity obtains from wherever (e.g., Intent extras).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文