0

基本上我正在做的是一个测验应用程序,它从数据库中读取问题以及该问题的相应选项。该应用程序适用于平板电脑,因此屏幕左侧显示问题列表(根据数据库中的问题数量创建),右侧显示带有四个选项的问题。我遇到的问题是,即使它在模拟器上完美运行,它也不能在平板电脑上运行。我从 bin 文件夹中取出 .apk 并将其安装在平板电脑上,看起来它没有成功读取数据库。如果我要发布的代码看起来很草率,我深表歉意,这是我第一次使用 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)。

4

1 回答 1

0

我将插入查询放在onCreate()而不是,MainActivity它工作得很好。

于 2012-09-11T20:17:33.893 回答