无法从DB获取数据

时间:2018-02-14 03:51:38

标签: android json database

我有一个示例JSON,如下所示:

{
  "questions": [
    {
      "id": "a200",
      "question": "lorem ipsum dolor _______ amet",
      "optionA": "lorem",
      "optionB": "ipsum",
      "optionC" : "dolor",
      "optionD": "sit",
      "rightAnswer" : "sit"

    },
    {
      "id": "b200",
      "question": "_____ ipsum dolor sit amet",
      "optionA": "lorem",
      "optionB": "ipsum",
      "optionC" : "dolor",
      "optionD": "sit",
      "rightAnswer" : "lorem"

    },
    {
      "id": "c200",
      "question": "lorem _____ dolor sit amet",
      "optionA": "lorem",
      "optionB": "ipsum",
      "optionC" : "dolor",
      "optionD": "sit",
      "rightAnswer" : "ipsum"
    }
  ]
}  

我将JSON上面插入到DB中的表中。代码如下:

package com.emc.kulkaa.learner;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import static android.content.ContentValues.TAG;

/**
 * Created by kulkaa on 2/13/2018.
 */

public class ParseAction {
    SQLiteDatabase SQLITEDATABASE;
    private Context context;

    public ParseAction(Context context, SQLiteDatabase SQLITEDATABASE) {
        this.context = context;
        this.SQLITEDATABASE = SQLITEDATABASE;
        putData();
        /*fetchData();*/
    }

    public String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream inputStream = context.getAssets().open("questions.json");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return json;
    }

    public void putData() {
        try {
            JSONObject obj = new JSONObject(loadJSONFromAsset());
            JSONArray jsonArray = obj.getJSONArray("questions");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                SQLITEDATABASE.execSQL(String.format("INSERT INTO quesTable ( id, question, option1, option2, option3, option4, rightAnswer ) VALUES(?, ?, ?, ?, ?, ?, ?)", Arrays.toString(new Object[]{
                        jsonObject.getString("id"),
                        jsonObject.getString("question"),
                        jsonObject.getString("optionA"),
                        jsonObject.getString("optionB"),
                        jsonObject.getString("optionC"),
                        jsonObject.getString("optionD"),
                        jsonObject.getString("rightAnswer")
                })));

                Log.d(TAG, "putData: " + jsonObject.getString("id"));
                Log.d(TAG, "putData: " + jsonObject.getString("question"));
                Log.d(TAG, "putData: " + jsonObject.getString("optionA"));
                Log.d(TAG, "putData: " + jsonObject.getString("optionB"));
                Log.d(TAG, "putData: " + jsonObject.getString("optionC"));

            }


        } catch (JSONException e1) {
            e1.printStackTrace();
        }
    }
}  

Log.d打印putData,如下所示:

02-14 09:15:17.751 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: a200
02-14 09:15:17.751 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: lorem ipsum dolor _______ amet
02-14 09:15:17.751 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: lorem
02-14 09:15:17.751 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: ipsum
02-14 09:15:17.751 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: dolor
02-14 09:15:17.755 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: b200
02-14 09:15:17.755 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: _____ ipsum dolor sit amet
02-14 09:15:17.755 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: lorem
02-14 09:15:17.755 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: ipsum
02-14 09:15:17.755 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: dolor
02-14 09:15:17.759 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: c200
02-14 09:15:17.759 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: lorem _____ dolor sit amet
02-14 09:15:17.759 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: lorem
02-14 09:15:17.759 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: ipsum
02-14 09:15:17.759 13807-13807/com.emc.kulkaa.learner D/ContentValues: putData: dolor  

现在我正在尝试检索数据并在UI中显示它。我获取数据的代码不起作用。代码如下:

package com.emc.kulkaa.learner;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.ArrayList;

public class QuizActivity extends AppCompatActivity {

    private Context context;
    SQLiteDatabase SQLITEDATABASE;
    ArrayList<questionDataPOJO> questionList;

    private static String TAG = QuizActivity.class.getCanonicalName();

    /*
     * Keep bottom 10% for branding
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        context = this;

        SQLITEDATABASE = openOrCreateDatabase("QuizDatabase", Context.MODE_PRIVATE, null);
        SQLITEDATABASE.execSQL("CREATE TABLE IF NOT EXISTS quesTable(id VARCHAR PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " question VARCHAR, option1 VARCHAR, option2 VARCHAR, option3 VARCHAR, option4 VARCHAR, rightAnswer VARCHAR);");

        ParseAction parseAction = new ParseAction(this, SQLITEDATABASE);

        questionList = new ArrayList<>();

        fetchData();

    }


    private void fetchData() {
        Cursor cursor = SQLITEDATABASE.rawQuery("SELECT * FROM quesTable", null);
        if (cursor.moveToFirst() && cursor.getCount() > 0) {
            do {
                questionList.add(new questionDataPOJO(
                        cursor.getString(0), // id
                        cursor.getString(1), // ques
                        cursor.getString(2), // A
                        cursor.getString(3), // B
                        cursor.getString(4), // C
                        cursor.getString(5)  // D
                ));
                Log.d(TAG, "id is " + cursor.getString(0));
                Log.d(TAG, "Question is " + cursor.getString(1));
                Log.d(TAG, "Option A is " + cursor.getString(2));
            } while (cursor.moveToNext());
        }
        cursor.close();
    }
}  

Log.d返回id的值,但QuestionOptionA等的值为null。如下所示:

02-14 09:15:17.761 13807-13807/com.emc.kulkaa.learner D/com.emc.kulkaa.learner.QuizActivity: id is 1
02-14 09:15:17.762 13807-13807/com.emc.kulkaa.learner D/com.emc.kulkaa.learner.QuizActivity: Question is null
02-14 09:15:17.762 13807-13807/com.emc.kulkaa.learner D/com.emc.kulkaa.learner.QuizActivity: Option A is null
02-14 09:15:17.762 13807-13807/com.emc.kulkaa.learner D/com.emc.kulkaa.learner.QuizActivity: id is 2
02-14 09:15:17.762 13807-13807/com.emc.kulkaa.learner D/com.emc.kulkaa.learner.QuizActivity: Question is null
02-14 09:15:17.762 13807-13807/com.emc.kulkaa.learner D/com.emc.kulkaa.learner.QuizActivity: Option A is null
02-14 09:15:17.762 13807-13807/com.emc.kulkaa.learner D/com.emc.kulkaa.learner.QuizActivity: id is 3
02-14 09:15:17.762 13807-13807/com.emc.kulkaa.learner D/com.emc.kulkaa.learner.QuizActivity: Question is null
02-14 09:15:17.762 13807-13807/com.emc.kulkaa.learner D/com.emc.kulkaa.learner.QuizActivity: Option A is null  

我该如何解决? 修改
我正在使用下面给出的POJO类:

public class questionDataPOJO {
    String id, question, optionA, optionB, optionC, optionD, rightAnswer;

    public questionDataPOJO(String id, String question, String optionA, String optionB, String optionC,
                            String optionD) {
        this.id = id;
        this.question = question;
        this.optionA = optionA;
        this.optionB = optionB;
        this.optionC = optionC;
        this.optionD = optionD;
        this.rightAnswer = rightAnswer;
    }

    public String getId() {
        return id;
    }

    public String getQuestion() {
        return question;
    }

    public String getOptionA() {
        return optionA;
    }

    public String getOptionB() {
        return optionB;
    }

    public String getOptionC() {
        return optionC;
    }

    public String getOptionD() {
        return optionD;
    }

    public String getRightAnswer() {
        return rightAnswer;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public void setOptionA(String optionA) {
        this.optionA = optionA;
    }

    public void setOptionB(String optionB) {
        this.optionB = optionB;
    }

    public void setOptionC(String optionC) {
        this.optionC = optionC;
    }

    public void setOptionD(String optionD) {
        this.optionD = optionD;
    }

    public void setRightAnswer(String rightAnswer) {
        this.rightAnswer = rightAnswer;
    }
}

1 个答案:

答案 0 :(得分:0)

请定义您的Initialize ArrayList类型。

questionList = new ArrayList<>();

questionList = new ArrayList< questionDataPOJO >();