将空字符串传递给db helper

时间:2015-05-16 20:34:44

标签: java android arrays string return

我遇到一些实际问题,从我的主要活动传递一个字符串到我的db helper类。这是我的主要活动方法,我返回一个字符串'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;
            }

我特别想在我的db helper类中的一个方法中使用这个'answerSetId'(作为查询的一部分)。这是我当前的代码 - 但它捕获一个空字符串'[]':

   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;
}

你可以看到我想使用这个方法根据查询使用'answerSetId'字符串重新获取更多的数组数据 - 你还会看到我添加系统打印输出的位置,它给出了空值'[ ]”。但是我只是在实际上从我的主要活动中捕获'answerSetId'值时感到困扰。

如何最好地实现这一目标?

我将把数据数组传递给我的'showNextAnswer()'方法:

        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;
            }

仅供参考 - 当用户点击进入下一个问题时,我会从onClick方法调用'showNextRandomQuestion3()' - 一个新的随机问题,每次点击都会显示该新问题的相关答案:

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

                }});

1 个答案:

答案 0 :(得分:0)

看起来解决方案非常简单。您只需捕获showNextRandomQuestion3()的返回值,因为它返回answerSetId

您需要在showNextAnswers()方法中添加一个String参数,以获取从showNextRandomQuestion3()返回的字符串。

所以在你的点击事件中,你会这样做:

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

                }});

然后,只需将参数添加到showNextAnswers(),就完成了!

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!

            //.................