从db类获取List <string>并存储在其他类中

时间:2015-08-31 18:30:29

标签: java android list get return

我正在我的db类(SQLDatabaseHelper.java)中创建一系列问题,并且有一个创建ListArray的方法,以便在其他类中使用:

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

    //Clear arrays just in case they still existed and had values
    array1.clear();
    array2.clear();

    SQLiteDatabase db = this.getReadableDatabase();

   //Get 5 random values from the questions table
    String selectQuery = "SELECT DISTINCT questionDescr, answerSetId FROM " + TABLE_QUESTION + " ORDER BY RANDOM() LIMIT 5";

   Cursor c = db.rawQuery(selectQuery, null);   

   try {

       //Ignore any null values in the table, go the first value
    if (c!= null && c.moveToFirst())

   {
       do {

     //Create two strings  - one for all question descriptions and one for answersetIDs
     String questdescr = c.getString(c.getColumnIndex(QDESCR));
     String answerSetIds = c.getString(c.getColumnIndex(ASID)); 

     //Put these strings into two separate arrays
     array1.add(questdescr);
     array2.add(answerSetIds);

     //And when all values have been put into arrays......
    } while (c.moveToNext());

 }

    //Create another array to bundle up the two arrays already created
   List< List<String> > listArray = new ArrayList< List<String> >();

   //Clear this array just in case of residual values
   listArray.clear();

   listArray.add(array1);
   listArray.add(array2);

   //Return this array to be picked up in the 'showRandomNextQuestion' method
   return listArray;

} finally {

    //Close cursor and database to prevent memory leaks
        c.close();            
        db.close();
        }
}

我有一个单独的类(QuizQuestionBank.java),它尝试获取返回的ListArray并将其拆分为两个单独的List以供进一步使用:

import java.util.List;

import android.app.Application;

public class QuizQuestionBank extends Application {

Object db;

@SuppressWarnings("unused")
private List<String> questionStrings;

public List<String> getQuestionStrings() {

SQLDatabaseHelper db = new SQLDatabaseHelper(QuizQuestionBank.this);

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

//Get the question/text/answer Strings
List<String> questionStrings = listList.get(0); //question Strings

return questionStrings;
}

public List<String> setQuestionStrings(List<String> questionStrings) {
return this.questionStrings = questionStrings;
}

}

最后我有一个MainActivity试图从我的QuizQuestionBank类中获取List,并使用它来获取一个值然后输出到textview:

private class showNextRandomQuestion1 extends AsyncTask<Void, Void, String> {

                @Override
                protected String doInBackground(Void... params) {

                    QuizQuestionBank qb = new QuizQuestionBank();

                       //question Strings
                       List<String> questionStrings = qb.getQuestionStrings();

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

                        String questionString = questionStrings.get(rand);  

                        //remove and square brackets from array of strings
                        String regex = "\\[|\\]";
                        aQuestionString = questionString.replaceAll(regex, "");
                        return aQuestionString; 
                }

                @Override
                protected void onPostExecute(String result) {
                    //take returned question from above and display it
                     questionView.setText(aQuestionString); 
                }

            }

然而,当我的&#39; showNextRandomQuestion1()&#39;时,我正在收到以下错误方法叫做:

08-31 19:24:55.637: I/Choreographer(20638): Skipped 63 frames!  The application may be doing too much work on its main thread.
08-31 19:24:55.887: I/ActivityManager(20638): Timeline: Activity_idle id: android.os.BinderProxy@41ec2f08 time:939408562
08-31 19:25:01.887: W/dalvikvm(20638): threadid=13: thread exiting with uncaught exception (group=0x41a71d88)
08-31 19:25:02.037: E/AndroidRuntime(20638): FATAL EXCEPTION: AsyncTask #2
08-31 19:25:02.037: E/AndroidRuntime(20638): Process: com.example.quizapp, PID: 20638
08-31 19:25:02.037: E/AndroidRuntime(20638): java.lang.RuntimeException: An error occured while executing doInBackground()
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.lang.Thread.run(Thread.java:841)
08-31 19:25:02.037: E/AndroidRuntime(20638): Caused by: java.lang.NullPointerException
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:263)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at com.example.quizapp.SQLDatabaseHelper.getAllAnswersByQuestion2(SQLDatabaseHelper.java:445)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at com.example.quizapp.QuizQuestionBank.getQuestionStrings(QuizQuestionBank.java:20)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at com.example.quizapp.MainActivity$showNextRandomQuestion1.doInBackground(MainActivity.java:371)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at com.example.quizapp.MainActivity$showNextRandomQuestion1.doInBackground(MainActivity.java:1)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-31 19:25:02.037: E/AndroidRuntime(20638):    ... 4 more
08-31 19:25:02.227: W/dalvikvm(20638): threadid=14: thread exiting with uncaught exception (group=0x41a71d88)
08-31 19:25:02.227: I/Process(20638): Sending signal. PID: 20638 SIG: 9

当我的QuizQuestionBank类试图从db helper类中检索listarray时,它似乎遇到了初步问题。任何人都能看到我需要做些什么才能解决这个问题?非常感谢

1 个答案:

答案 0 :(得分:0)

onPostExecute

中试试这个

更改此

questionView.setText(aQuestionString); 

questionView.setText(result);