現在クイズゲームを作成していて、問題をデータベースから表示させるところまで出来ていて、その問題に対応した解説を問題表示画面で、回答ボタンを押した後に表示させるようにしたいのです。
問題ごとにidを一緒に登録して、解説画面になったらそのidをデータベースから取り出せればいいのかなとは思っているのですが、具体的にどうすればいいのかわからないので教えていただきたいです。
具体的にはMainGame.javaで表示させた問題の解説を回答ボタンを押した後のkaitou.javaで表示させたいです。
public class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { /* ここで任意のデータベースファイル名と、バージョンを指定する データベースファイル名 = MyTable.db バージョン = 1 */ super(context, "MyTable.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { // テーブルの作成 db.execSQL("CREATE TABLE MyTable " + "(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT" + ", Pref TEXT" + ", City0 TEXT" + ", City1 TEXT" + ", City2 TEXT" + ", City3 TEXT" + ", City4 TEXT" + ",kaisetu TEXT" + ", Clear INTEGER" + ")"); db.execSQL("INSERT INTO MyTable(Pref,City0, City1, City2, City3, City4, kaisetu, Clear) values ('問題文','正解','問1','問2','問3','問4','これは解説です',1);"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
**MainGame.java** public class MainGame extends Activity { String QuestionNo; String Seikai; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //タイトルバーを非表示にする requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main_game); } // 問題文セット処理呼び出し setQuestion(); } // 問題文セット処理 private void setQuestion(){ // 作成したDatabaseHelperクラスに読み取り専用でアクセス DatabaseHelper dbHelper = new DatabaseHelper(this); SQLiteDatabase db = dbHelper.getReadableDatabase(); /* SELECT文 テーブル名 MyTableから _idとQuestionNoがマッチする項目を取得する条件式 */ String sql = "SELECT Pref, City0, City1, City2, City3, City4 FROM MyTable WHERE _id ORDER BY RANDOM()"; // SQL文を実行してカーソルを取得 Cursor c = db.rawQuery(sql, null); c.moveToFirst(); // データベースから取ってきたデータを変数にセット String Kenmei = c.getString(c.getColumnIndex("Pref")); String Choice1 = c.getString(c.getColumnIndex("City1")); // 四択の選択肢1 String Choice2 = c.getString(c.getColumnIndex("City2")); // 四択の選択肢2 String Choice3 = c.getString(c.getColumnIndex("City3")); // 四択の選択肢3 String Choice4 = c.getString(c.getColumnIndex("City4")); // 四択の選択肢4 Seikai = c.getString(c.getColumnIndex("City0")); // 答え // データベースのクローズ処理 c.close(); db.close(); ((TextView)findViewById(R.id.textQuestion)).setText(Kenmei); ((Button)findViewById(R.id.button1)).setText(Choice1); // 四択の選択肢1をボタンに表示 ((Button)findViewById(R.id.button2)).setText(Choice2); // 四択の選択肢2をボタンに表示 ((Button)findViewById(R.id.button3)).setText(Choice3); // 四択の選択肢3をボタンに表示 ((Button)findViewById(R.id.button4)).setText(Choice4); // 四択の選択肢4をボタンに表示 } // 選択肢がクリックされた時の処理 public void onClick(View v) { Intent intent; switch (v.getId()) { case R.id.button1: intent = new Intent(MainGame.this, kaitou.class); // 遷移開始 startActivity(intent); break; case R.id.button2: intent = new Intent(MainGame.this, kaitou.class); // 遷移開始 startActivity(intent); break; case R.id.button3: intent = new Intent(MainGame.this, kaitou.class); // 遷移開始 startActivity(intent); break; case R.id.button4: intent = new Intent(MainGame.this, kaitou.class); // 遷移開始 startActivity(intent); break; } } }
**kaitou.java** public class kaitou extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_kaitou); } // ボタンがタッチされた時の処理 public void onClick(View v){ // 遷移先のActivityを指定 // Intent intent = new Intent(このクラスから, このクラスへ) Intent intent = new Intent(kaitou.this, MainGame.class); // 遷移開始 startActivity(intent); } }
あなたの回答
tips
プレビュー