如何检索按钮上的数据并移动到下一步按钮?

我正在进行Android测验。 我创建了我的数据库,其中包含一个问题和四个选项以及难度级别。 我创建了一个布局,用四个按钮显示问题。 现在问题是如何将我的数据库与问题和四个按钮连接起来。

所以,当我点击右键时,它会移动到下一个问题,当我点击错误的按钮时,它会出错并退出。

XML格式的代码

        

我的DBHelper.java

 public class DBHelper extends SQLiteOpenHelper{ //The Android's default system path of your application database. private static String DB_PATH = "/data/data/com.starchazer.cyk/databases/"; private static String DB_NAME = "questionsDb"; private SQLiteDatabase myDataBase; private final Context myContext; /** * Constructor * Takes and keeps a reference of the passed context in order to access to the application assets and resources. * @param context */ public DBHelper(Context context) { super(context, DB_NAME, null, 1); this.myContext = context; } /** * Creates a empty database on the system and rewrites it with your own database. * */ public void createDataBase() throws IOException{ boolean dbExist = checkDataBase(); if(!dbExist) { //By calling this method and empty database will be created into the default system path //of your application so we are gonna be able to overwrite that database with our database. this.getReadableDatabase(); try { copyDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } } } /** * Check if the database already exist to avoid re-copying the file each time you open the application. * @return true if it exists, false if it doesn't */ private boolean checkDataBase(){ SQLiteDatabase checkDB = null; try{ String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ //database does't exist yet. } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } /** * Copies your database from your local assets-folder to the just created empty database in the * system folder, from where it can be accessed and handled. * This is done by transfering bytestream. * */ private void copyDataBase() throws IOException{ //Open your local db as the input stream InputStream myInput = myContext.getAssets().open(DB_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; //Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } //Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } public void openDataBase() throws SQLException{ //Open the database String myPath = DB_PATH + DB_NAME; myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } @Override public synchronized void close() { if(myDataBase != null) myDataBase.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } // Add your public helper methods to access and get content from the database. // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy // to you to create adapters for your views. public List getQuestionSet(int difficulty, int numQ){ List questionSet = new ArrayList(); Cursor c = myDataBase.rawQuery("SELECT * FROM QUESTIONS WHERE DIFFICULTY=" + difficulty + " ORDER BY RANDOM() LIMIT " + numQ, null); while (c.moveToNext()){ //Log.d("QUESTION", "Question Found in DB: " + c.getString(1)); Question q = new Question(); q.setQuestion(c.getString(1)); q.setAnswer(c.getString(2)); q.setOption1(c.getString(3)); q.setOption2(c.getString(4)); q.setOption3(c.getString(5)); q.setRating(difficulty); questionSet.add(q); } return questionSet; } } 

我的QuestionActivity

 public class QuestionActivity extends Activity implements OnClickListener{ private Question currentQ; private GamePlay currentGame; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.question); /** * Configure current game and get question */ currentGame = ((XYZ_Application)getApplication()).getCurrentGame(); currentQ = currentGame.getNextQuestion(); Button c1 = (Button) findViewById(R.id.answer1 ); c1.setOnClickListener(this); Button c2 = (Button) findViewById(R.id.answer2 ); c2.setOnClickListener(this); Button c3 = (Button) findViewById(R.id.answer3 ); c3.setOnClickListener(this); Button c4 = (Button) findViewById(R.id.answer4 ); c4.setOnClickListener(this); /** * Update the question and answer options.. */ setQuestions(); } /** * Method to set the text for the question and answers from the current games * current question */ private void setQuestions() { //set the question text from current question String question = Utility.capitalise(currentQ.getQuestion()) + "?"; TextView qText = (TextView) findViewById(R.id.question); qText.setText(question); //set the available options List answers = currentQ.getQuestionOptions(); TextView option1 = (TextView) findViewById(R.id.answer1); option1.setText(Utility.capitalise(answers.get(0))); TextView option2 = (TextView) findViewById(R.id.answer2); option2.setText(Utility.capitalise(answers.get(1))); TextView option3 = (TextView) findViewById(R.id.answer3); option3.setText(Utility.capitalise(answers.get(2))); TextView option4 = (TextView) findViewById(R.id.answer4); option4.setText(Utility.capitalise(answers.get(3))); } @Override public void onClick(View arg0) { /** * validate a buttonselected has been selected */ if (!checkAnswer()) return; /** * check if end of game */ if (currentGame.isGameOver()){ Intent i = new Intent(this, Main.class); startActivity(i); finish(); } else{ Intent i = new Intent(this, QuestionActivity.class); startActivity(i); finish(); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK : return true; } return super.onKeyDown(keyCode, event); } /** * Check if a checkbox has been selected, and if it * has then check if its correct and update gamescore */ private boolean checkAnswer() { String answer = getSelectedAnswer(); if (answer==null){ return false; } else { if (currentQ.getAnswer().equalsIgnoreCase(answer)) { //Log.d("Questions", "Correct Answer!"); currentGame.incrementRightAnswers(); } else{ //Log.d("Questions", "Incorrect Answer!"); currentGame.incrementWrongAnswers(); } return true; } } /** * */ private String getSelectedAnswer() { Button c1 = (Button)findViewById(R.id.answer1); Button c2 = (Button)findViewById(R.id.answer2); Button c3 = (Button)findViewById(R.id.answer3); Button c4 = (Button)findViewById(R.id.answer4); if (c1.callOnClick()) { return c1.getText().toString(); } if (c2.callOnClick()) { return c2.getText().toString(); } if (c3.callOnClick()) { return c3.getText().toString(); } if (c4.callOnClick()) { return c4.getText().toString(); } return null; } } 

对不起,我不会在这里给出代码。

这是我在Actvity开始时所做的

  1. 使用选项从Db加载问题
  2. 我更喜欢选择arrays/ arraylist
  3. 为每个按钮分配文本选项
  4. 像这样添加一个常见的onclick监听器

     public void onClick(View v){ int id=v.getId(); String answer=v.getText(); if(answer.equals('ANSWER FROM DB'){ // If right then next question // Else to MainActivity } /**switch(id){ case R.id.answer1: processAnswer(1); break; case R.id.answer2: processAnswer(2); break; // So on } **/ } 

而且processAnswer看起来像

 void processAnswer(int option){ // Since I have the option here // I will check it with Database // If its correct then next question else exit }