私は簡単、中、難の3つの難しさを持つクイズゲームを開発しています。私はcollection.shuffleを使用していますが、動作していないようです。ランダム(collection.shuffle)の質問と回答ボタンAndroidスタジオ
注 - 私は1つのレベル(簡単)が働いていましたが、今は3つのレベルがありません。私は研究して遊んでいますが、運はありません。私はコーディングが新しく、説明付きの答えが有益になるでしょう。コードの下
がQuizHelperクラスからのものである
BLOCKQUOTE
package com.example.sqz;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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";
private static final String LEVEL = "LEVEL";
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, " + KEY_OPTN1 + " TEXT, "
+ KEY_OPTN2 + " TEXT, " + KEY_OPTN3 + " TEXT, " + KEY_OPTN4 + " TEXT, "+ LEVEL+ " TEXT)";
db.execSQL(sql);
addQuestion();
// db.close();
}
private void addQuestion() {
Question q1 = new Question("Which team won FIFA world cup in 2002 ?", "Brazil", "England","Germany", "Italy", "Brazil",1);
this.addQuestion(q1);
Question q2 = new Question("How many goals Messi scored UEFA Champions League 2015 ?", "7", "6", "5", "8", "6",1);
this.addQuestion(q2);
Question q3 = new Question("Which team won cricket world cup in 2015 ?", "Australia", "England","New Zealand", "South Africa", "Australia",1);
this.addQuestion(q3);
Question q4 = new Question("Which team won premier league in 2015 ?", "Arsenal", "Manchester city", "Liverpool", "Chelsea", "Chelsea",1);
this.addQuestion(q4);
Question q5 = new Question("What team does LeBron James play for ?", "Cleveland Cavaliers", "Charlotte Hornets", "Los Angeles Clippers", "Miami Heat", "Cleveland Cavaliers",1);
this.addQuestion(q5);
Question q6 = new Question("Who scored the most goals in 2013 ?", "Lionel Messi", "Zlatan Ibrahimovic", "thierry henry", "Cristiano Ronaldo", "Cristiano Ronaldo",1);
this.addQuestion(q6);
Question q7 = new Question("Which team won world twenty20 in 2012 ?", "Australia", "West Indies", "South Africa", "Sri Lanka", "West Indies",1);
this.addQuestion(q7);
Question q8 = new Question("Who won Formula 1 championship in 2013 ?", "Fernando Alonso", "Sebastian Vettel", "Esteban Gutierrez", "Lewis Hamilton", "Sebastian Vettel",1);
this.addQuestion(q8);
Question q9 = new Question("Who won world darts championship in 2015 ?", "Gary Anderson", "Phil Taylor", "Boris Koltsov", "Michael van Gerwen", "Gary Anderson",1);
this.addQuestion(q9);
Question q10 = new Question("Cristiano Ronaldo played for which team in 2007 ?", "Arsenal", "Chelsea", "Liverpool", "Man United", "Man United",1);
this.addQuestion(q10);
Question q11 = new Question("how many goals Steven gerrard has scored for liverpool ?", "186", "195", "188", "174", "186",1);
this.addQuestion(q11);
Question q12 = new Question("Steve Smith plays for which cricket team ?", "England", "New Zealand", "South Africa", "Australia", "Australia",1);
this.addQuestion(q12);
Question q13 = new Question("How many goals Neymar scored for Santos ?", "137", "136", "140", "138", "136",1);
this.addQuestion(q13);
Question q14 = new Question("Steven Finn plays for which cricket team ?", "England", "New Zealand", "South Africa", "Australia", "England",1);
this.addQuestion(q14);
Question q15 = new Question("Which football team Wayne Rooney play for ?", "Man City", "Liverpool", "Man United", "Arsenal", "Man United",1);
this.addQuestion(q15);
Question q16 = new Question("Which tennis player was ranked number 1 in 2016 ?", "Roger Federer", "Tomas Berdych", "Novak Djokovic", "Rafael Nadal", "Novak Djokovic",1);
this.addQuestion(q16);
Question q17 = new Question("Who won Wimbledon Championships ? 2013", "Roger Federer", "Tomas Berdych", "Novak Djokovic", "Andy Murray", "Andy Murray",1);
this.addQuestion(q17);
Question q18 = new Question("Which team won FIFA world cup in 2014 ?", "Spain", "Germany", "Brazil", "Italy", "Germany",1);
this.addQuestion(q18);
Question q19 = new Question("Which team won twenty20 cricket in 2009 ?", "Pakistan", "Indian", "England", "Sri Lanka", "Pakistan",1);
this.addQuestion(q19);
Question q20 = new Question("Which team won cricket world cup in 2007 ?", "India", "Sri Lanka", "New Zealnad", "Australia", "Australia",1);
this.addQuestion(q20);
Question q21 = new Question("Who won tour de france 2014 ?", "Bradley Wiggins", "Leopold Konig", "Vincenzo Nibali", "Alberto Contador", "Vincenzo Nibali",1);
this.addQuestion(q21);
Question q22 = new Question("who was the manager of liverpool in 2009 ?", "Rafael Benitez", "Leopold Konig", "Roy Hodgson", "Brendan Rodgers", "Rafael Benitez",1);
this.addQuestion(q22);
Question q23 = new Question("Angelo Mathews plays test cricket for which team ?", "Australia", "West Indies", "Sri Lanka", "England", "Sri Lanka",2);
this.addQuestion(q23);
Question q24 = new Question("Which football team Luis Suarez played for from 2007-2011 ?", "Italy", "Barcelona", "Ajax", "Liverpool", "Ajax",2);
this.addQuestion(q24);
Question q25 = new Question("Which team won premier league in 2008 ?", "Manchester United", "Liverpool", "Arsenal", "Chelsea", "Manchester United",2);
this.addQuestion(q25);
Question q26 = new Question("What team does Kobe Bryant play for ?", "Swansea City", "Wycombe Wonderes", "Arsenal", "Southampton", "Swansea City",2);
this.addQuestion(q26);
Question q27 = new Question("Who won Formula 1 championship in 2007 ?", "Falipo Massa", "Kimi Räikkönen", "Fernando Alonso", "Lewis Hamilton", "Kimi Räikkönen",2);
this.addQuestion(q27);
Question q28 = new Question("Which football team does Wilfried Bony play for ?", "Bradley Wiggins", "Leopold Konig", "Vincenzo Nibali", "Alberto Contador", "Vincenzo Nibali",2);
this.addQuestion(q28);
Question q29 = new Question("What is the highest possible break in snooker ?", "156", "154", "155", "153", "155",2);
this.addQuestion(q29);
Question q30 = new Question("Cristiano Ronaldo played for which football team in 2007 ?", "Manchester United", "Liverpool", "Brazil", "Spain", "Manchester United",2);
this.addQuestion(q30);
Question q31 = new Question("In snooker what is the value of green ball ?", "4", "3", "5", "6", "3",2);
this.addQuestion(q31);
Question q32 = new Question("The game of badminton became popular in the United States during what time ?", "1930", "1931", "1929", "1932", "1930",2);
this.addQuestion(q32);
Question q33 = new Question("What is height of badminton net ?", "5.2ft", "5ft", "5.3ft", "5.5ft", "5ft",2);
this.addQuestion(q33);
Question q34 = new Question("How many points are there in a table tennis game ?", "9", "10", "11", "12", "11",2);
this.addQuestion(q34);
Question q35 = new Question("What is the weight of a table tennis ball ?", "2 gram", "2.5 gram", "2.7 gram", "2.9 gram", "2.7 gram",2);
this.addQuestion(q35);
Question q36 = new Question("In yards, how far apart are the cricket wickets ?", "18", "20", "22", "24", "22",2);
this.addQuestion(q36);
Question q37 = new Question("How old was Pele when he won his first World Cup ?", "15", "17", "18", "16", "17",2);
this.addQuestion(q37);
Question q38 = new Question("How many ways can a batsman be out ?", "7", "9", "10", "8", "10",2);
this.addQuestion(q38);
Question q39 = new Question("Which baseball player hit his 700th home run on September 17, 2004 ?", "Joe DiMaggio", "Barry Bond", "Ty Cobb", "Carl Yastrzemski", "Barry Bond",2);
this.addQuestion(q39);
Question q40 = new Question("How many games are in a Major League Baseball season per team ?", "154", "160", "162", "158", "162",2);
this.addQuestion(q40);
Question q41 = new Question("Which rugby player got criticism for his 'swallow dives' in 2011 ?", "Samson Lee", "Chris Ashton", "Warren Gatland", "Mike Brown", "Chris Ashton",2);
this.addQuestion(q41);
Question q42 = new Question("In darts, what is the lowest score that cannot be scored with a single dart ?", "21", "24", "23", "19", "23",2);
this.addQuestion(q42);
Question q43 = new Question("James Naismith invented which sport ?", "Baseball", "Basketball", "Table tennis", "Hockey", "Basketball",2);
this.addQuestion(q43);
Question q44 = new Question("Which company supplied the Euro 2012 matchball ?", "Nike", "Adidas", "Puma", "Umbro", "Adidas",3);
this.addQuestion(q44);
Question q45 = new Question("How old was Linford Christie when he won gold for the 100m in Barcelona ?", "35", "31", "32", "33", "32",3);
this.addQuestion(q45);
Question q46 = new Question("To the nearest 1,000 how many rides did the legendary Willie Shoemaker take ?", "40,000", "30,000", "50,000", "20,000", "40,000",3);
this.addQuestion(q46);
Question q47 = new Question("In which year was the first Wimbledon tournament held ?", "1818", "1877", "1845", "1919", "1877",3);
this.addQuestion(q47);
Question q48 = new Question("How many World Cups have been won by Australian ?", "5", "4", "6", "7", "4",3);
this.addQuestion(q48);
Question q49 = new Question("What sport used the term 'home run' long before baseball ?", "Cricket", "Hockey", "Basketball", "Darts", "Cricket",3);
this.addQuestion(q49);
Question q50 = new Question("In horse racing, what is a Yankee ?", "13 individual bets", "12 individual bets", "11 individual bets", "10 individual bets", "11 individual bets",3);
this.addQuestion(q50);
Question q51 = new Question("How many players make up a Canadian Football team ?", "13", "14", "12", "15", "14",3);
this.addQuestion(q51);
Question q52 = new Question("In which year was the Cotton Bowl first contested? ?", "1938", "1937", "1945", "1930", "1937",3);
this.addQuestion(q52);
Question q53 = new Question("where was the summer Olympics of 1928 held ?", "New York", "Amsterdam", "London", "Brazil", "Amsterdam",3);
this.addQuestion(q53);
Question q54 = new Question("In which year did martin Johnson retire from International Rugby? ?", "2005", "2004", "2007", "2001", "2004",3);
this.addQuestion(q54);
Question q55 = new Question("How many players feature in a male Lacrosse team? ?", "8", "10", "11", "13", "10",3);
this.addQuestion(q55);
Question q56 = new Question("Ken Walton was a long time commentator most famously associated with which sport? ?", "Basketball", "Wrestling", "Cricket", "Tennis", "Wrestling",3);
this.addQuestion(q56);
Question q57 = new Question("The Superbowl is usually contested in which month? ?", "May", "January", "March", "April", "January",3);
this.addQuestion(q57);
Question q58 = new Question("In inches, how big is the diameter of a basketball hoop? ?", "8", "8.5", "9", "7", "8",3);
this.addQuestion(q58);
Question q59 = new Question("What number lies between 5 and 9 on a British dart board? ?", "15", "12", "10", "14", "12",3);
this.addQuestion(q59);
Question q60 = new Question("In golf, over how many holes is The Open Championship contested? ?", "65", "68", "72", "75", "72",3);
this.addQuestion(q60);
Question q61 = new Question("Kareem Abdul-Jabbar played 20 seasons in which sport? ?", "Cricket", "Basketball", "Tennis", "Hockey", "Basketball",3);
this.addQuestion(q61);
// 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());
values.put(LEVEL, quest.getLevel());
// 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));
questionList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i=0;i<60;i++)
list.add(i);
Collections.shuffle(list);
List<Question> shuffledQuestionList = new ArrayList<Question>();
for(int i=0;i<60;i++)
shuffledQuestionList.add(questionList.get(list.get(i)));
return shuffledQuestionList;
}
public List<Question> getAllQuestionsByLevel(int level) {
List<Question> questionList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST+" where "+LEVEL+" ='"+String.valueOf(level)+"'";
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.setLevel(cursor.getInt(7));
if(quest.getLevel()==level)
questionList.add(quest);
} while (cursor.moveToNext());
}
return questionList;
}
}
BLOCKQUOTE
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 level;
private String ANSWER;
public Question() {
ID = 0;
QUESTION = "";
OPTION1 = "";
OPTION2 = "";
OPTION3 = "";
OPTION4 = "";
ANSWER = "";
level=0;
}
public Question(String qUESTION, String OPTN1, String OPTN2, String OPTN3,String OPTN4,
String aNSWER,int level) {
QUESTION = qUESTION;
OPTION1 = OPTN1;
OPTION2 = OPTN2;
OPTION3 = OPTN3;
OPTION4 = OPTN4;
ANSWER = aNSWER;
this.level=level;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
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 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 setANSWER(String aNSWER) {
ANSWER = aNSWER;
}
}
BLOCKQUOTE
package com.example.sqz;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
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;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class QuestionActivity extends Activity {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQuestion;
TextView txtQuestion, times, scored;
Button Answer1, Answer2, Answer3, Answer4;
QuizHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int level=getIntent().getIntExtra("level",-1);
if(level!=-1) {
db=new QuizHelper(this);
quesList=db.getAllQuestionsByLevel(level);
}
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
Answer1 = (Button) findViewById(R.id.btnAnswer1);
Answer2 = (Button) findViewById(R.id.btnAnswer2);
Answer3 = (Button) findViewById(R.id.btnAnswer3);
Answer4 = (Button) findViewById(R.id.btnAnswer4);
// 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(10000, 1000);
timer.start();
// button click listeners
Answer1.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(Answer1.getText().toString());
}
});
Answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(Answer2.getText().toString());
}
});
Answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(Answer3.getText().toString());
}
});
Answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(Answer4.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) { // please add atleast 21 questions for each set of levels or else the value will be less than 20 and your game will stop working :)
// 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.LOLLIPOP)
@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());
Answer1.setText(currentQuestion.getOPT1());
Answer2.setText(currentQuestion.getOPT2());
Answer3.setText(currentQuestion.getOPT3());
Answer4.setText(currentQuestion.getOPT4());
qid++;
}
}
正確には機能しないものはありますか?あなたは起こっていない、起こっていないことは何ですか? – jonathanrz
私は簡単なレベルのための20の質問、ハードレベルのための中程度の20の質問20の質問があります。私はゲームをプレイするときに質問の負荷が再び私は質問を別の順序で表示したいゲームをクリックしてクリックします。 – Ozzy
'getAllQuestions'メソッドを呼び出すコードを投稿する –