我正在android studio上创建一个问答游戏,它有三个不同的难度级别,简单,中等和难度。上次我问过,我被建议我应该使用一个代表难度的列,并用它来表示困难。 (问题可以在这里找到:https://stackoverflow.com/questions/36315274/android-studio-quiz-game-sqlite-database-multiple-tables-for-different-game-diff)。
1)我使用了建议的方法,但是当我选择easy,medium或hard时,它会为每个困难加载相同的问题。
2)游戏应该显示四个多个答案,但最佳答案是空白但当我点击空白答案时游戏结束(见附图)
package com.example.sqz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class level extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level);
}
public void onButtonClick(View view) {
Intent a = new Intent(this,MainActivity.class); a.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(a);
}
public void btnEasy(View view) {
Intent intent = new Intent(this, QuestionActivity.class);
startActivity(intent);
}
public void btnMedium(View view) {
Intent intent = new Intent(this, QuestionActivity.class);
startActivity(intent);
}
public void btnHard(View view) {
Intent intent = new Intent(this, QuestionActivity.class);
startActivity(intent);
}
}
块引用
package com.example.sqz;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class QuizHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "SQZ";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "qid";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; // correct option
private static final String KEY_OPTN1 = "OPTN1"; // option 1
private static final String KEY_OPTN2 = "OPTN2"; // option 2
private static final String KEY_OPTN3 = "OPTN3"; // option 3
private static final String KEY_OPTN4 = "OPTN4"; // option 4
private static final String DIFFICULT = "difficult";
private SQLiteDatabase dbase;
public QuizHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase = db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER + " TEXT, " +DIFFICULT+ " INTEGER, " + KEY_OPTN1 + " TEXT, "
+ KEY_OPTN2 + " TEXT, " + KEY_OPTN3 + " TEXT, " + KEY_OPTN4 + " TEXT)";
db.execSQL(sql);
addQuestion();
// db.close();
}
private void addQuestion() {
Question q1 = new Question("Which team won FIFA world cup in 2002 ?",0, "Brazil", "England","Germany", "Italy", "Brazil");
this.addQuestion(q1);
Question q2 = new Question("How many goals Messi scored UEFA Champions League 2015 ?",0, "7", "6", "5", "8", "6");
this.addQuestion(q2);
Question q3 = new Question("Which team won cricket world cup in 2015 ?",0, "Australia", "England","New Zealand", "South Africa", "Australia");
this.addQuestion(q3);
Question q4 = new Question("Which team won premier league in 2015 ?",1, "Arsenal", "Manchester city", "Liverpool", "Chelsea", "Chelsea");
this.addQuestion(q4);
Question q5 = new Question("What team does LeBron James play for ?",1, "Cleveland Cavaliers", "Charlotte Hornets", "Los Angeles Clippers", "Miami Heat", "Cleveland Cavaliers");
this.addQuestion(q5);
Question q6 = new Question("Who scored the most goals in 2013 ?",1, "Lionel Messi", "Zlatan Ibrahimovic", "thierry henry", "Cristiano Ronaldo", "Cristiano Ronaldo");
this.addQuestion(q6);
Question q7 = new Question("Which team won world twenty20 in 2012 ?",2, "Australia", "West Indies", "South Africa", "Sri Lanka", "West Indies");
this.addQuestion(q7);
Question q8 = new Question("Who won Formula 1 championship in 2013 ?",2, "Fernando Alonso", "Sebastian Vettel", "Esteban Gutierrez", "Lewis Hamilton", "Sebastian Vettel");
this.addQuestion(q8);
Question q9 = new Question("Who won world darts championship in 2015 ?",2, "Gary Anderson", "Phil Taylor", "Boris Koltsov", "Michael van Gerwen", "Gary Anderson");
this.addQuestion(q9);
// END
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
// Create tables again
onCreate(db);
}
// Adding new question
public void addQuestion(Question quest) {
// SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQUESTION());
values.put(KEY_ANSWER, quest.getANSWER());
values.put(KEY_OPTN1, quest.getOPT1());
values.put(KEY_OPTN2, quest.getOPT2());
values.put(KEY_OPTN3, quest.getOPT3());
values.put(KEY_OPTN4, quest.getOPT4());
// Inserting Row
dbase.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> questionList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase = this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setOPTN1(cursor.getString(3));
quest.setOPTN2(cursor.getString(4));
quest.setOPTN3(cursor.getString(5));
quest.setOPTN4(cursor.getString(6));
quest.setDIFFICULT(cursor.getString(7));
questionList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i=0;i<20;i++)
list.add(i);
Collections.shuffle(list);
List<Question> shuffledQuestionList = new ArrayList<Question>();
for(int i=0;i<20;i++)
shuffledQuestionList.add(questionList.get(list.get(i)));
return shuffledQuestionList;
}
}
块引用
package com.example.sqz;
import android.app.Activity;
public class Question extends Activity {
private int ID;
private String QUESTION;
private String OPTION1;
private String OPTION2;
private String OPTION3;
private String OPTION4;
private int DIFFICULT;
private String ANSWER;
public Question() {
ID = 0;
QUESTION = "";
OPTION1 = "";
OPTION2 = "";
OPTION3 = "";
OPTION4 = "";
ANSWER = "";
}
public Question(String qUESTION,int DIFFCULTY, String OPTN1, String OPTN2, String OPTN3, String OPTN4,
String aNSWER ) {
QUESTION = qUESTION;
OPTION1 = OPTN1;
OPTION2 = OPTN2;
OPTION3 = OPTN3;
OPTION4 = OPTN4;
DIFFICULT = DIFFCULTY;
ANSWER = aNSWER;
}
public int getID() {
return ID;
}
public String getQUESTION() {
return QUESTION;
}
public String getOPT1() {
return OPTION1;
}
public String getOPT2() {return OPTION2;}
public String getOPT3() {return OPTION3;}
public String getOPT4() {return OPTION4;}
public int getDIFFICULT() {
return DIFFICULT;
}
public String getANSWER() {
return ANSWER;
}
public void setID(int id) {
ID = id;
}
public void setQUESTION(String qUESTION) {
QUESTION = qUESTION;
}
public void setOPTN1(String OPTN1) {
OPTION1 = OPTN1;
}
public void setOPTN2(String OPTN2) {OPTION2 = OPTN2; }
public void setOPTN3(String OPTN3) {OPTION3 = OPTN3;}
public void setOPTN4(String OPTN4) {OPTION4 = OPTN4;}
public void setDIFFICULT(String DIFFICULT) {DIFFICULT = DIFFICULT;}
public void setANSWER(String aNSWER) {
ANSWER = aNSWER;
}
}
}
块引用
package com.example.sqz;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.TimeUnit;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class QuestionActivity extends Activity {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQuestion;
TextView txtQuestion, times, scored;
Button button1, button2, button3, button4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
QuizHelper db = new QuizHelper(this); // my question bank class
quesList = db.getAllQuestions(); // this will fetch all quetonall questions
currentQuestion = quesList.get(qid); // the current question
txtQuestion = (TextView) findViewById(R.id.txtQuestion);
// the textview in which the question will be displayed
// the three buttons,
// the idea is to set the text of three buttons with the options from question bank
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
// the textview in which score will be displayed
scored = (TextView) findViewById(R.id.score);
// the timer
times = (TextView) findViewById(R.id.timers);
// method which will set the things up for our game
setQuestionView();
times.setText("00:0:00");
// A timer of 60 seconds to play for, with an interval of 1 second (1000 milliseconds)
CounterClass timer = new CounterClass(200000, 1000);
timer.start();
// button click listeners
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// passing the button text to other method
// to check whether the anser is correct or not
// same for all three buttons
getAnswer(button1.getText().toString());
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button2.getText().toString());
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button3.getText().toString());
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button4.getText().toString());
}
});
}
public void getAnswer(String AnswerString) {
if (currentQuestion.getANSWER().equals(AnswerString)) {
// if conditions matches increase the int (score) by 1
// and set the text of the score view
score++;
scored.setText("Score : " + score);
} else {
// if unlucky start activity and finish the game
Intent intent = new Intent(QuestionActivity.this,
ResultActivity.class);
// passing the int value
Bundle b = new Bundle();
b.putInt("score", score); // Your score
intent.putExtras(b); // Put your score to your next
startActivity(intent);
finish();
}
if (qid < 20) {
// if questions are not over then do this
currentQuestion = quesList.get(qid);
setQuestionView();
} else {
// if over do this
Intent intent = new Intent(QuestionActivity.this,
ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); // Your score
intent.putExtras(b); // Put your score to your next
startActivity(intent);
finish();
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
times.setText("Time is up");
}
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format(
"%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(millis)));
System.out.println(hms);
times.setText(hms);
}
}
private void setQuestionView() {
// the method which will put all things together
txtQuestion.setText(currentQuestion.getQUESTION());
button1.setText(currentQuestion.getOPT1());
button2.setText(currentQuestion.getOPT2());
button3.setText(currentQuestion.getOPT3());
button4.setText(currentQuestion.getOPT4());
qid++;
}
}
块引用
package com.example.sqz;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onButtonClick(View v) {
Intent intent = new Intent(this, level.class);
startActivity(intent);
}
public void Instructions(View view) {
final AlertDialog.Builder Instructions = new AlertDialog.Builder(this);
Instructions.setMessage("The SQZ is designed to test your knowledge across variety of sports, to play: \n1. Click Play \n2. Select Dificulty \n3. when quiz begins, three answers will be provided select the correct one. ")
.setPositiveButton("Got It", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setTitle("Instructions")
.setIcon(R.drawable.queslogo1)
.create();
Instructions.show();
}
public void btnQuit(View view) {
new AlertDialog.Builder(this)
.setIcon(R.drawable.warning)
.setTitle("Closing Application")
.setMessage("Are you sure you want to Quit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
public void btnOptions(View view) {
if(view.getId() == R.id.btnOptions)
{
Intent i = new Intent(MainActivity.this, options.class);
startActivity(i);
}
}
public void QuestionMark(MenuItem item) {
final AlertDialog.Builder generalinfo = new AlertDialog.Builder(this);
generalinfo.setMessage("Important Note: \nAll the images used in this application are created by the developer.\nThe application has been tested on android version 4.4 and 5.1 ")
.setPositiveButton("Got It", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setTitle("General Information")
.setIcon(R.drawable.queslogo1)
.create();
generalinfo.show();
}
public void email(MenuItem item) {
final AlertDialog.Builder contact = new AlertDialog.Builder(this);
contact.setMessage("If you have any suggestions email them to me at\nusman_saddique@hotmail.co.uk ")
.setPositiveButton("Got It", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setTitle("Contact")
.setIcon(R.drawable.emaillogo1)
.create();
contact.show();
}
}
答案 0 :(得分:0)
我没有彻底阅读你的代码,但是当你在btnEasy,btnMedium和btnHard的方法中执行完全相同的语句时,按下它们的效果必须相同。
您可能希望将难度级别添加到您的意图中,并在接收活动中对此进行评估以选择适当的问题。选择问题的适当位置是您的数据库助手类。获取问题的方法可能会将难度作为参数,只返回那些符合所需难度的问题。
提示,并不意味着侮辱:您的代码显示您既不熟悉Java也不熟悉Android。花点时间阅读有关两者的介绍性书籍,这样您就可以掌控正在撰写的代码。