2017-04-03 5 views
0

私はアンドロイドを新しくしました。私はカテゴリ(歴史、スポーツなど)を持つクイズアプリを作ろうとしています。ボタンのクリックでint値を渡すにはどうすればいいですか?

データベース内に60個の質問があるテーブルは1つだけです。最初の10の質問は、カテゴリー1(履歴)からであり、カテゴリー10から20はカテゴリー2(スポーツ)などです。最初のカテゴリをクリックすると最初の10の質問のみが取得され、2番目のカテゴリをクリックすると10から20の質問のみが取得されます。 onclick()のインテントでこの問題を解決し、それ以外の場合にその値を使用して質問を囲むことはできますか?

QuestionActivity.java

public class QuestionActivity extends Activity { 
List<Question> quesList; 
int score = 0; 
Random r = new Random(); 
int qid= (r.nextInt(60) + 0); 

Question currentQ; 
TextView txtQuestion, times, scored; 
Button button1, button2, button3; 
CounterClass timer; 

@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 
    currentQ = 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); 

    // 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:00:30"); 

    // A timer of 30 seconds to play for, with an interval of 1 second 
    (1000 milliseconds) 
    timer = new CounterClass(30000, 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()); 
     } 
    }); 
} 

public void getAnswer(String AnswerString) { 
    if (currentQ.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 
    timer.cancel(); 
     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 < 60) { 

     // if questions are not over then do this 
     currentQ = quesList.get(qid); 
     setQuestionView(); 
    } else { 
     // if over do this 
     timer.cancel(); 
     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.GINGERBREAD) 
@SuppressLint("NewApi") 
public class CounterClass extends CountDownTimer { 

    public CounterClass(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
     // TODO Auto-generated constructor stub 
    } 


    @Override 
    public void onFinish() { 
     times.setText("Time is up"); 
     Intent intent = new Intent(QuestionActivity.this, 
       ResultActivity.class); 
     timer.cancel(); 
     Bundle b = new Bundle(); 
     b.putInt("score", score); // Your score 
     intent.putExtras(b); // Put your score to your next 
     startActivity(intent); 
     finish(); 

    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     // TODO Auto-generated method stub 

     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(currentQ.getQUESTION()); 
    button1.setText(currentQ.getOPTA()); 
    button2.setText(currentQ.getOPTB()); 
    button3.setText(currentQ.getOPTC()); 
    qid= (r.nextInt(60) + 0); 

    } 


    } 

Home.java

public class Home extends Activity { 
Button play,about,help,quit; 
ImageView img; 
@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home); 
    play=(Button)findViewById(R.id.play); 
    about=(Button)findViewById(R.id.about); 
    quit=(Button)findViewById(R.id.quit); 
    help=(Button)findViewById(R.id.help); 

    play.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Intent intent=new Intent(getApplicationContext(),QuestionActivity.class); 
      startActivity(intent); 
      finish(); 
     } 
    }); 
    quit.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      finish(); 
     } 
    }); 
    about.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent=new Intent(getApplicationContext(),about.class); 
      startActivity(intent); 
     } 
    }); 
    help.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent=new Intent(getApplicationContext(),help.class); 
      startActivity(intent); 
     } 
    }); 

} 

}

+0

クリックしたボタンに基づいてQuestionActivityを開始するときに、あなたの意図に余分なintを設定したいですか? QuestionActivityと呼ぶ場所にコードを投稿してください。 –

+0

私はquestionactivityと呼ばれるjavaファイルが追加されました。 –

+0

[Androidアプリケーションのアクティビティ間でデータを渡すにはどうすればいいですか?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) –

答えて

0

あなたはインテントオブジェクトを作成した後がありますが、startActivity(intent);使用呼び出す前に:あなたがしたい場合はintent.putExtra("name",123);を他のアクティビティで整数を取得するあなたがそのint値を算出したアクティビティでint foo = getIntent().getIntExtra("name");

0

を、あなたは他のActivityに行きたい:ityが単に使用

OtherClassToGoonCreate()方法で
int magicVariable = 100; 
Intent intent = new Intent(this, OtherClassToGo.class); 
intent.putExtra("EXTRA_VALUE_KEY", magicVariable); 
startActivity(intent); 

Intent intent = getIntent(); 
int magicVariableFromOtherClass = intent.getIntExtra("EXTRA_VALUE_KEY"); 

もちろん、"EXTRA_VALUE_KEY"の場合は、パブリック静的変数を使用することができますので、それを「記憶」する方が簡単です。

関連する問題