2016-04-09 26 views
-3

私は3つの難易度が簡単、中、難しいアンドロイドスタジオでクイズゲームを作成しています。私が最後に尋ねたとき、私は難しさを表し、それを難し​​いと呼ぶために使うコラムを使うべきだと示唆されました。 (質問はここにあります:https://stackoverflow.com/questions/36315274/android-studio-quiz-game-sqlite-database-multiple-tables-for-different-game-diff)。SQLite Android Studioで1つの列を使用してクイズゲームの難易度レベル

1)私は、提案された方法を使用しましたが、簡単、中、または難しいを選択すると、困難なそれぞれについて同じ質問が読み込まれます。

2)ゲームは、4つの複数の答えが表示されるはずですが、トップの答えは空白ですが、私は空白の答えにゲーム終了をクリックしたときに(添付写真参照)

App image

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); 
} 
} 

BLOCKQUOTE

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; 

} 


} 

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 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; 
} 

} 

} 

BLOCKQUOTE

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++; 
} 


} 

BLOCKQUOTE

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\[email protected] ") 
      .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(); 
} 

} 
+1

ここでは、すべての**あなたの質問を取得します。難易度にかかわらず。 'String selectQuery =" SELECT * FROM "+ TABLE_QUEST;は無用です**。選択した難易度の質問のみを取得するにはwhere句を追加する必要があります(Spinner、RadioGroupなどで選択します) –

答えて

0

私はあなたのコードを完全には読まなかったが、btnEasy、btnMedium、およびbtnHardのメソッドでまったく同じステートメントを実行するので、それらを押す効果は同じでなければならない。

難易度をあなたの意図に余分に追加し、受信側のアクティビティでこれを評価して適切な質問を選択したいとします。質問を選択する適切な場所は、DBヘルパークラスです。質問を得る方法は難易度をパラメータとし、必要な難易度に一致する質問のみを返します。

ヒントは、侮辱ではありません。あなたのコードは、あなたがJavaやAndroidについてよく知らないことを示しています。両方についての入門書を読む時間があれば、書いているコードをコントロールすることができます。

関連する問題