I'm having issues trying to get the query method to run in the DatabaseHelper class. I passed data to a second activity which is used in the query. But how do I call the query method from the second activity? When I try to, it comes up with a "can't call a static from non-static" message (or vice versa?). I'm having trouble coming with the correct way to call the print method I'm using in the second activity too. Thanks in advance.
Here's the code for the second activity: DisplayResult.java
public class DisplayResult extends Activity {
TextView tv;
List<PromisesModel> list = new ArrayList<PromisesModel>();
DatabaseHelper db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Intent intent = getIntent();
String selectedCategory = intent.getStringExtra("data");
Toast.makeText(DisplayResult.this, "Category passed: " + selectedCategory,
Toast.LENGTH_LONG).show();
try {
db = new DatabaseHelper(getApplicationContext());
} catch (IOException e) {
e.printStackTrace();
}
tv = (TextView)findViewById(R.id.tv);
print(List<PromisesModel> list);
}
private void print(List<PromisesModel> list) {
String value = "";
for (PromisesModel sm : list) {
value = value + sm.book + " " + sm.chapter + ":" + sm.verse + " " + sm.word + "\n" + "\n";
}
tv.setText(value);
}
}
Here's the code for the DatabaseHelper class:
public class DatabaseHelper extends SQLiteOpenHelper {
public String selectedCategory;
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.promisescyb/";
public static final String TABLE_NAME = "cybpromises";
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 DatabaseHelper(Context context) 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 db) {
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 " + " )";
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public List<PromisesModel> getPromises() {
List<PromisesModel> promisesArrayList = new ArrayList<PromisesModel>();
String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + KEY_CATEGORY + " = " + selectedCategory;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
if(c != null) {
c.moveToFirst();
do {
PromisesModel promises = new PromisesModel();
promises.book = c.getString(c.getColumnIndex(KEY_BOOK));
promises.chapter = c.getInt(c.getColumnIndex(KEY_CHAPTER));
promises.verse = c.getInt(c.getColumnIndex(KEY_VERSE));
promises.word = c.getString(c.getColumnIndex(KEY_WORD));
promisesArrayList.add(promises);
} while (c.moveToNext());
}
return promisesArrayList;
}
I have a PromisesModel class as well as the MainActivity. Those seem to be okay. The toast messages show that the proper value is getting passed to the second activity. I hope I have the scope defined correctly on that so that it's also used in the query in the DatabaseHelper class. Thanks again.
Aucun commentaire:
Enregistrer un commentaire