0

I'm having some real problems passing a single string from my main activity to my db helper class. Here is my main activity method which I am returning a string 'answerSetId':

 public String 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);

            return answerSetId;
            }

I specifically want to use this 'answerSetId' in one method in my db helper class (as part of a query). Here is my current code - but it is capturing an empty string '[]':

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

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

    System.out.println(answerSetId);

    SQLiteDatabase db = this.getReadableDatabase();

    String[] columns = new String[]{TDESCR, ADESCR};
    String selection = ASID+"=?";
    String[] selectionArgs = new String[]{String.valueOf(answerSetId)};
    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;
}

You can see that I want to use this method to retrun some more array data based on the query using the 'answerSetId' string - you'll also see where I have added a system print out which gives the empty value '[]'. But am only bothered about actually capturing the 'answerSetId' value from my main activity at this point.

How best to achieve this?

the arrays of data I will then pass onto my 'showNextAnswer()' method:

        public String showNextAnswers() {

            SQLDatabaseHelper db = new SQLDatabaseHelper(this);

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

            //Get the question/text/answer Strings
            List<String> textDescrs = listList.get(0); //question Strings
//          List<String> answerDescrs = listList.get(1); //text strings

//          System.out.println(answerSetId);
            answerView3.setText(textDescrs.toString());

            String textDescriptions = textDescrs.toString();
//          String regex = "\\[|\\]";
//          textDescriptions = textDescriptions.replaceAll(regex, "");

            //Separate out the question/answer of text description associated with the randomly selected question
            StringTokenizer textDescr = new StringTokenizer(textDescriptions, ",");

            String first = textDescr.nextToken();
//              String second = textDescr.nextToken();
//              String third = textDescr.nextToken();
//              String fourth = textDescr.nextToken();

            subQuestionView1.setText(first);
//              subQuestionView2.setText(second);
//              subQuestionView3.setText(third);
//              answerView3.setText(fourth);
//              System.out.println(textDescr);
            return null;
            }

FYI - I call 'showNextRandomQuestion3()' from an onClick method when user clicks to go to a next question - a new random question and the related answers to that new question will appear each click:

        NextQuestionButton.setOnClickListener(new OnClickListener(){;
                @Override
        public void onClick(View v) {
                    showNextRandomQuestion3();
                    showNextAnswers();
                    countQuestions();

                }});
  • Just to note, you're calling `getAllAnswersByQuestion1()`, does that method call `getAnswers()`? – Daniel Nugent May 16 '15 at 21:00
  • Nope - I have a 'getAllAnswersByQuestion1() method in db helper - this passes on several arrays of question related data. Then I have the 'showRandomNextQuestion3()' method in main activity which takes these arrays, assigns some to textviews but also returns a new string 'answerSetId'. This string is taken by 'getAnswers()' method in my db helper - and this method passes on a couple different arrays back to my main activity where another method, 'showNextAnswers()' which I hadn't mentioned yet, calls 'getAnswers()' and assigns remaining strings to remaining textviews. –  May 16 '15 at 21:09
  • 1
    Can you show the code where you call `getAnswers()`? – Daniel Nugent May 16 '15 at 21:23
  • Have added it to the question above now. It's not finished of course with plenty of it commented out whilst I am still working things out - at the moment it is only getting blank arrays from 'showNextAnswer()' –  May 17 '15 at 05:37
  • Any thoughts Daniel? –  May 17 '15 at 18:18
  • I'll take a look now. – Daniel Nugent May 17 '15 at 18:19
  • Where does `answerSetId` come from in the `showNextAnswers()` method? Is it an instance variable? – Daniel Nugent May 17 '15 at 18:34
  • Ah - so not sure on that one. I have it in there, but really I am just trying to get the listarray2 that was returned in 'getAnswers()'. But when I leave the brackets blank on line 'List> listList = db.getAnswers(answerSetId);' it wants me to add astring argument - I basically haven't tackled it again yet. Just trying to get a non blank 'answerSetId' value from the db helper method first - but would welcome any thoughts on what I may need to change on 'showNextAnswers()' of course. –  May 17 '15 at 19:00
  • Question, why are you dong `listList.get(0)` and `listList.get(4)`? Are the other lists not used? – Daniel Nugent May 17 '15 at 20:42
  • That's right - I happened to return 5 arrays originally from db helper method. But I decided to only use 2 of them (I have since removed them from my code and replaced 'listlist.get(4)', with 'listlist.get(1)'. –  May 17 '15 at 20:50
  • Can you show where you call `showNextRandomQuestion3()`? – Daniel Nugent May 17 '15 at 21:45

1 Answers1

0

It looks like the solution is very simple. You just capture the return value of showNextRandomQuestion3(), since it returns answerSetId.

You will need to add a String parameter to the showNextAnswers() method to take the String that was returned from showNextRandomQuestion3().

So in your click event, you would do this:

 NextQuestionButton.setOnClickListener(new OnClickListener(){;
        @Override
        public void onClick(View v) {
                    String answerSetId = showNextRandomQuestion3(); //get return value
                    showNextAnswers(answerSetId); //give the ID here
                    countQuestions();

                }});

Then, just add the parameter to showNextAnswers(), and you're done!

public String showNextAnswers(String answerSetId) {

            SQLDatabaseHelper db = new SQLDatabaseHelper(this);

            //get the data from the database
            List<List<String>> listList = db.getAnswers(answerSetId); //This will work now!

            //.................
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • This is great - and apologies for not getting back and setting this as resolved. I just happen to get an cursor window full error with this - 05-20 19:49:46.269: W/CursorWindow(10993): Window is full: requested allocation 132 bytes, free space 21 bytes, window size 2097152 bytes 05-20 19:49:48.859: D/dalvikvm(10993): GC_FOR_ALLOC freed 835K, 6% free 24962K/26480K, paused 22ms, total 22ms - any thoughts? Have been reading up a lot on the error and made sure to close my cursors - shall I raise a new question on the forum you think? –  May 20 '15 at 18:57
  • @jrgart No problem! I'm not familiar with that specific error, but it seems there are lots of threads about it. I would say read up on it as much as you can, and if you can't figure it out from the answers already posted, then post a new question! Here is one post I found: http://stackoverflow.com/questions/11863024/ And here is another one, which might be more specific to your case: http://stackoverflow.com/questions/20094421/ – Daniel Nugent May 20 '15 at 19:05