2012-07-30 67 views
0

基本上我正在做的是一個測驗應用程序,它從數據庫中讀取問題以及該問題的相應選項。該應用程序適用於平板電腦,因此屏幕會顯示左側的問題列表(根據數據庫中的問題數量創建),右側顯示帶有四個選項的問題。我遇到的問題是,儘管它在模擬器上完美工作,但它不適用於平板電腦。我將bin文件夾中的.apk文件安裝到平板電腦上,看起來沒有成功讀取數據庫。我很抱歉,如果我即將發佈的代碼看起來很sl,,這是我第一次使用Android,而且我所做的一切都基於我在通過互聯網閱讀的各種教程中閱讀的內容。android數據庫沒有在平板電腦上閱讀

這是數據庫助手:

//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(); 
} 

這是Helper爲表問題

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<Question> getAllQuestions() { 
    List<Question> questions = new ArrayList<Question>(); 
    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; 
} 
} 

這是表選項

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<Option> getAllOptions() { 
    List<Option> options = new ArrayList<Option>(); 
    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<Option> getOptions(int qID) { 
    List<Option> options = new ArrayList<Option>(); 
    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; 
} 
} 

而這個幫手是MainActivity分類:

public class MainActivity extends ListActivity { 
private QuestionDataSource dataSourceQuestions; 
private OptionDataSource dataSourceOptions; 
ArrayList<String> listItems = new ArrayList<String>(); 
ArrayAdapter<String> 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<Question> 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 < qnCount; i++) { 
     listItems.add("Question " + (i + 1)); 
    } 
    listItems.add("Question 3"); 
    adapter = new ArrayAdapter<String>(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<Question> 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<Option> 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()); 
} 
} 

如果有人能幫我解決這個問題,我將非常感激。再一次,我遇到的問題是它在模擬器上工作正常,但是當我在平板電腦上運行它(在平板電腦中安裝.apk)時,它似乎不會讀取數據庫。

+0

該應用程序是否安裝沒有錯誤? – javajavajava 2012-07-30 15:18:33

+0

是的。事實上,如果你看到主要活動,你會發現我編寫了「問題3」。它出現在平板電腦上,確認它確實沒有讀取數據庫。 – plasmy 2012-07-30 16:53:26

+0

我把插入查詢放在onCreate而不是MainActivity中,它的工作完美。 – plasmy 2012-07-31 15:37:28

回答

0

我把插入查詢置於onCreate()而不是MainActivity,它的工作很完美。

相關問題