Java 将数组列表拆分为单独的字符串

Java 将数组列表拆分为单独的字符串,java,android,arrays,sqlite,Java,Android,Arrays,Sqlite,我已经从我的SQLite数据库中创建了一个值数组——它只包含来自内部联接表中两个不同列的两个值 在my SQLDatabaseHelper.java中: public List<List<String>> getAllAnswersByQuestion1() { List<String> array1 = new ArrayList<String>(); List<String> array2 = new Array

我已经从我的SQLite数据库中创建了一个值数组——它只包含来自内部联接表中两个不同列的两个值

在my SQLDatabaseHelper.java中:

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

    SQLiteDatabase db = this.getReadableDatabase();

    String selectQuery = "SELECT  * FROM " + TABLE_ANSWERS + " ta, "
            + TABLE_QUESTION + " tq, " + TABLE_QUESTANS + " tqa WHERE  ta." + ASID
            + " = " + "tqa." + ASID + " AND tq." + QID + " = "
            + "tqa." + QID;

   Cursor c = db.rawQuery(selectQuery, null);
   if (c.moveToFirst()) {
    do {

     String questdescr = c.getString(c.getColumnIndex(QDESCR));
     String questid = c.getString(c.getColumnIndex(QID));
     array1.add(questdescr);
     array2.add(questid);

    } while (c.moveToNext());

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

   return listArray;

}
但是,代码不会编译,因为它希望“将“getAllAnswersByQuestion1()的返回类型更改为字符串”-或“删除参数以匹配StingTokenizer(字符串)”

似乎绕不开这件事

for(List l : db.getAllAnswersByQuestion1()){
           for(String s : l){
                StringTokenizer tokens = new StringTokenizer(s, ",");
        String first = tokens.nextToken();
        String second = tokens.nextToken();

        questionView.setText(first);
        answerText1.setText(second);
           }
 }

此更新至少会使您的代码正常工作。看起来您的问题和答案都存储在
QDESCR
字段中,以逗号分隔

如果是这样的话,那么下面的代码就可以了。 我假设您想要从列表中选择一个随机问题,给定方法名

您可以使用
Random
生成列表的随机索引。 您可能希望添加逻辑以防止再次出现以前选择的问题

以下是获取问题/答案和问题ID的基本代码:

public void showNextRandomQuestion() {

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

    //Get the question/answer Strings and the question IDs
    List<String> questionStrings = listList.get(0); //question and answer Strings
    List<String> questionIDs = listList.get(1); //question IDs

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

    //get question ID for randomly selected question
    String questionID = questionIDs.get(rand);

    //Separate out the question/answer of randomly selected question
    StringTokenizer tokens = new StringTokenizer(questionStrings.get(rand), ",");

    String first = tokens.nextToken();
    String second = tokens.nextToken();

    questionView.setText(first);
    answerText1.setText(second);
}
public void showNextRandomQuestion(){
//从数据库中获取数据
List=db.getAllAnswersByQuestion1();
//获取问题/答案字符串和问题ID
List questionStrings=listList.get(0);//问题和答案字符串
List questionId=listList.get(1);//问题ID
//生成随机索引
随机r=新随机();
int rand=Math.abs((r.nextInt()%questionStrings.size());
//获取随机选择的问题的问题ID
字符串questionID=questionID.get(rand);
//将随机选择的问题/答案分开
StringTokenizer令牌=新的StringTokenizer(questionStrings.get(rand),“,”;
String first=tokens.nextToken();
String second=tokens.nextToken();
questionView.setText(第一);
回答文本1.设置文本(第二);
}

另外,请注意,最好使用
String.split()
,如中所述。

这是因为
StringTokenizer
需要一个
String
,而您正在传递一个
列表
数组已经由单独的字符串组成。你还想用它做什么?您只有两个编辑字段和许多字符串。没有道理。
public void showNextRandomQuestion() {

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

    //Get the question/answer Strings and the question IDs
    List<String> questionStrings = listList.get(0); //question and answer Strings
    List<String> questionIDs = listList.get(1); //question IDs

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

    //get question ID for randomly selected question
    String questionID = questionIDs.get(rand);

    //Separate out the question/answer of randomly selected question
    StringTokenizer tokens = new StringTokenizer(questionStrings.get(rand), ",");

    String first = tokens.nextToken();
    String second = tokens.nextToken();

    questionView.setText(first);
    answerText1.setText(second);
}