Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid database not reading in tablet
    text
    copied!<p>Basically what I'm making is a quiz application that reads questions from a database and that question's respective option. The application is for tablet, so the screen shows the list of questions on the left (created based on the number of questions in the database) and on the right it shows the question with the four options. The problem I'm having is that even though it works perfectly on the emulator, it is not working on a tablet. I took the .apk from the bin folder and installed it on the tablet and it looks like it is not reading the database successfully. My apologies if the code I'm about to post seems sloppy, this is the first time that I've ever used Android and everything I'm doing is based on what I'm reading in various tutorials I've read through the internet.</p> <p>This is the Database Helper:</p> <pre><code>//Declaration of the Questions Database public class DBHelper extends SQLiteOpenHelper{ //Columns to be created in the table questions public static final String TABLE_QUESTIONS = "questions"; public static final String COLUMN_QUESTIONID = "_questionid"; public static final String COLUMN_QUESTION = "question"; public static final String COLUMN_EXPLANATION = "explanation"; public static final String COLUMN_QIMAGE = "questionimage"; public static final String COLUMN_EIMAGE = "explanationimage"; public static final String COLUMN_MODULEID = "moduleid"; //***************************************************************// //Columns to be created in the table options public static final String TABLE_OPTIONS = "options"; public static final String COLUMN_OPTIONID = "_optionid"; public static final String COLUMN_OPTION = "option"; public static final String COLUMN_CORRECTANSWER = "correctanswer"; //public static final String COLUMN_QUESTIONID = "questionid"; //***************************************************************// //Database name and version private static final String DATABASE_NAME = "discipulus.db"; private static final int DATABASE_VERSION = 1; // Database creation SQL statement of questions table private static final String QUESTIONS_CREATE = "CREATE TABLE " + TABLE_QUESTIONS + "(" + COLUMN_QUESTIONID + " integer primary key autoincrement, " + COLUMN_QUESTION + " text not null, " + COLUMN_EXPLANATION + " text not null, " + COLUMN_QIMAGE + " text not null, " + COLUMN_EIMAGE + " text not null, " + COLUMN_MODULEID + " integer not null);"; private static final String OPTIONS_CREATE = "CREATE TABLE " + TABLE_OPTIONS + "(" + COLUMN_OPTIONID + " integer primary key autoincrement, " + COLUMN_OPTION + " text not null, " + COLUMN_CORRECTANSWER + " integer not null, " + COLUMN_QUESTIONID + " integer not null);"; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase database) { database.execSQL(QUESTIONS_CREATE); database.execSQL(OPTIONS_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(DBHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUESTIONS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_OPTIONS); onCreate(db); } } public class QuestionDataSource { private SQLiteDatabase database; private DBHelper dbHelper; private String[] allColumns = { DBHelper.COLUMN_QUESTIONID, DBHelper.COLUMN_QUESTION, DBHelper.COLUMN_EXPLANATION, DBHelper.COLUMN_QIMAGE, DBHelper.COLUMN_EIMAGE, DBHelper.COLUMN_MODULEID }; public QuestionDataSource(Context context) { dbHelper = new DBHelper(context); } public void open() throws SQLException { database = dbHelper.getWritableDatabase(); } public void close() { dbHelper.close(); } </code></pre> <p>This is the <code>Helper</code> for the table questions</p> <pre><code>public Question createQuestion(String question, String explanation, String questionImage, String explanationImage, int moduleID) { ContentValues values = new ContentValues(); values.put(DBHelper.COLUMN_QUESTION, question); values.put(DBHelper.COLUMN_EXPLANATION, explanation); values.put(DBHelper.COLUMN_QIMAGE, questionImage); values.put(DBHelper.COLUMN_EIMAGE, explanationImage); values.put(DBHelper.COLUMN_MODULEID, moduleID); long insertID = database.insert(DBHelper.TABLE_QUESTIONS, null, values); Cursor cursor = database.query(DBHelper.TABLE_QUESTIONS, allColumns, DBHelper.COLUMN_QUESTIONID + " = " + insertID, null, null, null, null); cursor.moveToFirst(); Question newQuestion = cursorToQuestion(cursor); cursor.close(); return newQuestion; } public List&lt;Question&gt; getAllQuestions() { List&lt;Question&gt; questions = new ArrayList&lt;Question&gt;(); Cursor cursor = database.query(DBHelper.TABLE_QUESTIONS, allColumns, null, null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Question question = cursorToQuestion(cursor); questions.add(question); cursor.moveToNext(); } cursor.close(); return questions; } private Question cursorToQuestion(Cursor cursor) { Question question = new Question(); question.setQuestionID(cursor.getInt(0)); question.setQuestion(cursor.getString(1)); question.setExplanation(cursor.getString(2)); question.setQuestionImage(cursor.getString(3)); question.setExplanationImage(cursor.getString(4)); question.setModuleId(cursor.getInt(5)); return question; } } </code></pre> <p>And this is the helper for the table options</p> <pre><code>public class OptionDataSource { private SQLiteDatabase database; private DBHelper dbHelper; private String[] allColumns = { DBHelper.COLUMN_OPTIONID, DBHelper.COLUMN_OPTION, DBHelper.COLUMN_CORRECTANSWER, DBHelper.COLUMN_QUESTIONID }; public OptionDataSource(Context context) { dbHelper = new DBHelper(context); } public void open() throws SQLException { database = dbHelper.getWritableDatabase(); } public void close() { dbHelper.close(); } public Option createOption(String option, int correctAnswer, int questionID) { ContentValues values = new ContentValues(); values.put(DBHelper.COLUMN_OPTION, option); values.put(DBHelper.COLUMN_CORRECTANSWER, correctAnswer); values.put(DBHelper.COLUMN_QUESTIONID, questionID); long insertID = database.insert(DBHelper.TABLE_OPTIONS, null, values); Cursor cursor = database.query(DBHelper.TABLE_OPTIONS, allColumns, DBHelper.COLUMN_OPTIONID + " = " + insertID, null, null, null, null); cursor.moveToFirst(); Option newOption = cursorToOption(cursor); cursor.close(); return newOption; } public List&lt;Option&gt; getAllOptions() { List&lt;Option&gt; options = new ArrayList&lt;Option&gt;(); Cursor cursor = database.query(DBHelper.TABLE_OPTIONS, allColumns, null, null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Option option = cursorToOption(cursor); options.add(option); cursor.moveToNext(); } cursor.close(); return options; } public List&lt;Option&gt; getOptions(int qID) { List&lt;Option&gt; options = new ArrayList&lt;Option&gt;(); String MY_QUERY = "SELECT * FROM options WHERE _questionid = " + qID + ";"; Cursor cursor = database.rawQuery(MY_QUERY, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Option option = cursorToOption(cursor); options.add(option); cursor.moveToNext(); } cursor.close(); return options; } private Option cursorToOption(Cursor cursor) { Option option = new Option(); option.setOptionID(cursor.getInt(0)); option.setOption(cursor.getString(1)); int cAnswer = cursor.getInt(2); if (cAnswer == 0) { option.setCorrectAnswer(false); }else { option.setCorrectAnswer(true); } option.setQuestionID(cursor.getInt(3)); return option; } } </code></pre> <p>And this is the <code>MainActivity</code> class:</p> <pre><code>public class MainActivity extends ListActivity { private QuestionDataSource dataSourceQuestions; private OptionDataSource dataSourceOptions; ArrayList&lt;String&gt; listItems = new ArrayList&lt;String&gt;(); ArrayAdapter&lt;String&gt; adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dataSourceQuestions = new QuestionDataSource(this); dataSourceQuestions.open(); dataSourceOptions = new OptionDataSource(this); dataSourceOptions.open(); List&lt;Question&gt; questions = dataSourceQuestions.getAllQuestions(); int qnCount = questions.size(); for(Question qn : questions) { TextView pregunta = (TextView) findViewById(R.id.textView1); pregunta.setText(qn.getQuestion()); } for (int i = 0; i &lt; qnCount; i++) { listItems.add("Question " + (i + 1)); } listItems.add("Question 3"); adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, listItems); setListAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } protected void onListItemClick(ListView l, View v, int position, long id) { List&lt;Question&gt; questions = dataSourceQuestions.getAllQuestions(); TextView pregunta = (TextView) findViewById(R.id.textView1); pregunta.setText(questions.get(position).getQuestion()); int questionID = questions.get(position).getQuestionID(); setRadioButtons(questionID); } private void setRadioButtons(int questionID) { List&lt;Option&gt; options = dataSourceOptions.getOptions(questionID); RadioButton rdb1 = (RadioButton) findViewById(R.id.rdbOption1); rdb1.setText(options.get(0).getOption()); RadioButton rdb2 = (RadioButton) findViewById(R.id.rdbOption2); rdb2.setText(options.get(1).getOption()); RadioButton rdb3 = (RadioButton) findViewById(R.id.rdbOption3); rdb3.setText(options.get(2).getOption()); RadioButton rdb4 = (RadioButton) findViewById(R.id.rdbOption4); rdb4.setText(options.get(3).getOption()); } } </code></pre> <p>If anyone can help me out with this problem I will be very grateful. Again, the problem I'm having is that it works fine on the emulator, but it doesn't seem to be reading the database when I run it on a tablet (installing the .apk in the tablet).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload