我需要使用getIntent,但是不能这样做,因为根据我的承诺,它不是SQLiteOpenHelper的构造函数,并且需要在onCreate中使用getIntent。
在这种情况下,我如何从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);
}
}
}
答案 0 :(得分:0)
在这种情况下,我如何从SQLiteOpenHelper中访问它?
理想情况下,您无法从getIntent()
访问SQLiteOpenHelper
。您在SQLiteOpenHelper
上实现了一个构造函数(提示:您已经有一个),并传入它需要的任何数据。理想情况下,不涉及传入活动,而是从任何地方获取活动的值(例如Intent
额外内容)。