【发布时间】: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