I have an app that needs to access a static database and run queries based on user input. I am using
jgilfelt/android-sqlite-asset-helper
as my Database helper class. My main activity has 2 spinners that the user can use to narrow down a search. The spinners return the a string variable named mGenre and mDecade respectively. at the bottom of the activity is a button to proceed. When the button is pushed an intent fires to bring up a new activity that displays a song title at random. The variables are passed to the new activity vi the intent with the putExtra(). The new activity recieves the variable and uses them in if/else if statements to decide which query to run from the DB Helper.
In the DB helper class, I create an instance of the new class and us it to set the mGenre and mDecade variables so that the proper query is running, using the variables inside of the String[] args section of rawQuery().
Here is the songActivity class that is opened when the user hits the button on the Main Activity:
public class SongActivity extends ActionBarActivity {
private MyDatabase db;
private static final String TAG = "We got ";
String mGenre;
String mDecade;
public String getGenre() {
return mGenre;
}
public String getDecade() {
return mDecade;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song);
//when activity launches query database using user params
mGenre = getIntent().getExtras().getString("Genre");
mDecade = getIntent().getExtras().getString("Decade");
Log.v(TAG, mGenre);
Log.v(TAG, mDecade);
String songTitle = "";
String songArtist = "";
String songYear = "";
TextView mSongTitle = (TextView)findViewById(R.id.songTitle);
TextView mSongArtist = (TextView)findViewById(R.id.songArtist);
TextView mSongYear = (TextView)findViewById(R.id.songYear);
db= new MyDatabase(this);
if (mGenre.equals("*") && mDecade.equals("*")) {
Cursor cursor = db.kamikazeSelected();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
songTitle = cursor.getString(1);
songArtist = cursor.getString(2);
songYear = cursor.getString(4);
Log.i(TAG, String.valueOf(songTitle));
cursor.moveToNext();
}
cursor.close();
db.close();
}
else if (!mGenre.equals("*") && mDecade.equals("*")) {
Cursor cursor = db.genreSelected();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
songTitle = cursor.getString(1);
songArtist = cursor.getString(2);
songYear = cursor.getString(4);
// Log.i("cursor", cursor.getString(1));
Log.i(TAG, String.valueOf(songTitle));
cursor.moveToNext();
}
cursor.close();
db.close();
}
else if (mGenre.equals("*") && !mDecade.equals("*")) {
Cursor cursor = db.decadeSelected();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
songTitle = cursor.getString(1);
songArtist = cursor.getString(2);
songYear = cursor.getString(4);
Log.i(TAG, String.valueOf(songTitle));
cursor.moveToNext();
}
cursor.close();
db.close();
}
else if (!mGenre.equals("*") && !mDecade.equals("*")) {
Cursor cursor = db.bothSelected();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
songTitle = cursor.getString(1);
songArtist = cursor.getString(2);
songYear = cursor.getString(4);
Log.i(TAG, String.valueOf(songTitle));
cursor.moveToNext();
}
cursor.close();
db.close();
}
mSongTitle.setText(songTitle);
mSongArtist.setText(songArtist);
mSongYear.setText(songYear);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here is the database helper class:
public class MyDatabase extends SQLiteAssetHelper {
private static final String TAG = "Variable for query is ";
private static final String DATABASE_NAME = "mankiniDB.sqlite";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
SongActivity mSongActivity = new SongActivity();
String mGenre = mSongActivity.getGenre();
String mDecade = mSongActivity.getDecade();
public Cursor kamikazeSelected() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM songList ORDER BY random() limit 1", null);
return cursor;
}
public Cursor genreSelected() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM songList WHERE Genre = ? ORDER BY random() limit 1", new String[] {mGenre});
return cursor;
}
public Cursor decadeSelected() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM songList WHERE Decade = ? ORDER BY random() limit 1", new String[] {mDecade});
return cursor;
}
public Cursor bothSelected() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM songList WHERE Decade = ? AND Genre = ? ORDER BY random() limit 1", new String[] {mDecade, mGenre});
return cursor;
}
}
The problem is that no matter what I do, or where I set the variables, The database helper class recives and empty variable that results in a null argument. I need the user's input complete the query. But it is always null. Please help me see what I am missing.
Aucun commentaire:
Enregistrer un commentaire