vendredi 15 mai 2015

Pass string from main activity to DB Helper class

I am trying to pass a string 'answerSetId' to my DB Helper class - I have tried using intent but this doesn't work because the db helper is not declared on the android manifest file.

     public void showNextRandomQuestion3() {


            SQLDatabaseHelper db = new SQLDatabaseHelper(this);

            //get the data from the database
            List<List<String>> listList = db.getAllAnswersByQuestion1();

            //Get the question/text/answer Strings
            List<String> questionStrings = listList.get(0); //question Strings
            List<String> answerSetIds = listList.get(4);

            //Generate random index
            Random r = new Random();
            int rand = Math.abs((r.nextInt() % questionStrings.size()));

            //get answer description for randomly selected question
            String questionString = questionStrings.get(rand);  
            String answerSetId = answerSetIds.get(rand);

            questionView.setText(questionString);

            Intent intent = new Intent(this, SQLDatabaseHelper.class);
            intent.putExtra("answerSetId", answerSetId);
            startActivity(intent);

            }

What is the best way to pass this string to my db helper. This is the db helper method I want to pass the string to, and the getIntent method:

public List<List<String>> getAnswers(String answerSetId) {

    List<String> array1 = new ArrayList<String>();    
    List<String> array2 = new ArrayList<String>();  

    SQLiteDatabase db = this.getReadableDatabase();

    Intent startingIntent = getIntent();
    String answerSetIdentifier = startingIntent.getStringExtra("answerSetId");

    String[] columns = new String[]{TDESCR, ADESCR};
    String selection = ASID+"=?";
    String[] selectionArgs = new String[]{String.valueOf(answerSetIdentifier)};
    Cursor c = db.query(TABLE_ANSWERS, columns, selection, selectionArgs, null, null, null);  

   if (c.moveToFirst()) {
    do {

     String textdescr = c.getString(c.getColumnIndex(TDESCR));
     String answersdescr = c.getString(c.getColumnIndex(ADESCR));
     array1.add(textdescr);
     array2.add(answersdescr);;

    } while (c.moveToNext());

 }
   List< List<String> > listArray2 = new ArrayList< List<String> >();
   listArray2.add(array1);
   listArray2.add(array2);

   return listArray2;
}
private Intent getIntent() {
    // TODO Auto-generated method stub
    return null;
}

Error I get basically is 'ActivityNotFoundException'- so I assume I cannot use intent to send to db helper. Cheers

Aucun commentaire:

Enregistrer un commentaire