【问题标题】:Passing empty string to db helper将空字符串传递给数据库助手
【发布时间】:2015-07-28 14:47:36
【问题描述】:

我在将一个字符串从我的主要活动传递到我的数据库助手类时遇到了一些实际问题。这是我返回字符串“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;
            }

我特别想在我的数据库助手类(作为查询的一部分)的一种方法中使用这个“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();

                }});

【问题讨论】:

  • 请注意,您正在调用getAllAnswersByQuestion1(),该方法是否调用getAnswers()
  • 不 - 我在 db helper 中有一个 'getAllAnswersByQuestion1() 方法 - 这会传递几个与问题相关的数据数组。然后我在主要活动中有'showRandomNextQuestion3()'方法,它采用这些数组,将一些分配给textviews,但也返回一个新字符串'answerSetId'。该字符串由我的数据库助手中的“getAnswers()”方法获取-该方法将几个不同的数组传递回我的主要活动,其中我尚未提到的另一种方法“showNextAnswers()”调用“getAnswers” ()' 并将剩余的字符串分配给剩余的文本视图。
  • 你能显示你调用getAnswers()的代码吗?
  • 现在已将其添加到上述问题中。当然,它还没有完成,当我还在解决问题时,它已经被大量注释掉了——目前它只是从“showNextAnswer()”中获取空白数组
  • 丹尼尔有什么想法吗?

标签: java android arrays string return


【解决方案1】:

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

您需要向showNextAnswers() 方法添加一个字符串参数,以获取从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!

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

【讨论】:

  • 这很好 - 很抱歉没有回复并将其设置为已解决。我只是碰巧遇到了一个光标窗口已满错误 - 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 释放 835K,6% 释放 24962K/26480K,暂停 22ms,总共 22ms - 有什么想法吗?已经阅读了很多关于错误的内容并确保关闭我的光标 - 我应该在您认为的论坛上提出一个新问题吗?
  • @jrgart 没问题!我不熟悉那个特定的错误,但似乎有很多关于它的线程。我会说尽可能多地阅读它,如果你不能从已经发布的答案中弄清楚,然后发布一个新问题!这是我找到的一篇帖子:stackoverflow.com/questions/11863024 这是另一篇,可能更适合您的情况:stackoverflow.com/questions/20094421
猜你喜欢
  • 2012-11-13
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 2015-03-24
  • 2018-10-30
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
相关资源
最近更新 更多