Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

android - unable to fetch data from DB

I've a sample JSON as given below:

{
  "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"
    }
  ]
}  

I'm inserting above JSON to a table in DB. Code for that is as given below:

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 prints putData as given below:

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  

Now I'm trying to retrieve the data and show it in UI. My code for fetching data isn't working. Code is as given below:

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 returns values for id, but values for Question, OptionA etc are null. It is as given below:

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  

How can I fix it?
EDIT:
I'm using a POJO class as given below:

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please defined your Initialize ArrayList type.

questionList = new ArrayList<>();

to

questionList = new ArrayList< questionDataPOJO >();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...