mercredi 14 septembre 2016

How to pass multiple generated values into another activity as input

I am developing an Math quiz app.In that it consist of 9 levels ,each level consist of 10 questions which generates random operands from array.In particular level,it generates random operands ,I have to pass these operands to another level.For example:if level 1 1st input is randomly 4 and 3 ,the level 2 1st input should be 3 and 4 likewise it should pass all 10 set operands to another level .I have share code with this,please help me out it.

public class levelFour extends AppCompatActivity implements View.OnClickListener {

int qid = 0,score=0;
private int answer=0,operand1=0,operand2=0;
private int[] level4op1={0,1,2,3,4,5,6,7,8,9};
private int[] level4op2={0,1,2,3,4,5,10};
private Random random;

private int enteredInput = 0;

//Declare ui elements
private TextView question, answerTxt, responseTxt, timeTxt;
private ImageView response;
private Button btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven, btn_eight, btn_nine, btn_zero,
        btn_clr;

private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainpage);
    Log.e("tag","correct : " );

    // Get input from Text View
    question = (TextView)findViewById(R.id.question);
    answerTxt = (TextView)findViewById(R.id.answer);
    response = (ImageView)findViewById(R.id.response);
    responseTxt = (TextView)findViewById(R.id.responseTxt);
    timeTxt=(TextView) findViewById(R.id.timeTxt);

    // to hide the response
    response.setVisibility(View.INVISIBLE);

    //Get input from Buttons

    btn_one = (Button)findViewById(R.id.btn_one);
    btn_two = (Button)findViewById(R.id.btn_two);
    btn_three = (Button)findViewById(R.id.btn_three);
    btn_four = (Button)findViewById(R.id.btn_four);
    btn_five = (Button)findViewById(R.id.btn_five);
    btn_six = (Button)findViewById(R.id.btn_six);
    btn_seven = (Button)findViewById(R.id.btn_seven);
    btn_eight = (Button)findViewById(R.id.btn_eight);
    btn_nine = (Button)findViewById(R.id.btn_nine);
    btn_zero = (Button)findViewById(R.id.btn_zero);
    btn_clr = (Button)findViewById(R.id.btn_clr);

    //onclick functions

    btn_one.setOnClickListener(this);
    btn_two.setOnClickListener(this);
    btn_three.setOnClickListener(this);
    btn_four.setOnClickListener(this);
    btn_five.setOnClickListener(this);
    btn_six.setOnClickListener(this);
    btn_seven.setOnClickListener(this);
    btn_eight.setOnClickListener(this);
    btn_nine.setOnClickListener(this);
    btn_zero.setOnClickListener(this);
    btn_clr.setOnClickListener(this);

    random= new Random();
    //To randomly generate questions
    selectQuestion();
    startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread, 0);
}

private void selectQuestion() {
    enteredInput = 0;
    answerTxt.setText("?");
    //get random operand
    operand1=getOperand1();
    operand2=getOperand2();
    //show question
    question.setText(operand1 + " * " + operand2);
    answer=operand1*operand2;
    qid++;
    Log.e("tag","qid : " + qid);
}

private int getOperand1() {
    operand1=random.nextInt(level4op1.length);
    return operand1;


}
private int getOperand2() {
    operand2=random.nextInt(level4op2.length);
    return operand2;
}

private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        timeTxt.setText("" + mins + ":"
                + String.format("%02d", secs));
        customHandler.postDelayed(this, 0);
    }

};

@Override
public void onClick(View view) {

    if (view.getId() == R.id.btn_one || view.getId() == R.id.btn_two
            || view.getId() == R.id.btn_three || view.getId() == R.id.btn_four
            || view.getId() == R.id.btn_five || view.getId() == R.id.btn_six
            || view.getId() == R.id.btn_seven || view.getId() == R.id.btn_eight
            || view.getId() == R.id.btn_nine || view.getId() == R.id.btn_zero) {

        //final MediaPlayer correctMP=MediaPlayer.create(this,R.raw.correct);
        //final MediaPlayer wrongMP=MediaPlayer.create(this,R.raw.wrong);

        Spannable word = new SpannableString("Correct");
        word.setSpan(new ForegroundColorSpan(Color.GREEN), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        Spannable wordTwo = new SpannableString("Wrong");
        wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        if (answer <= 9) {
            Log.e("tag","ans : " + answer);
            //get number from tag
            int enteredNum = Integer.parseInt(view.getTag().toString());
            if (answerTxt.getText().toString().endsWith("?"))
                answerTxt.setText("" + enteredNum);
            if (enteredNum == answer) {
                //      correctMP.start();
                score++;
                Log.e("tag","score : " + score);
                response.setImageResource(R.drawable.tick);
                response.setVisibility(View.VISIBLE);
                responseTxt.setText(word);
            } else {
                //    wrongMP.start();
                Log.e("tag","score : " + score);
                response.setImageResource(R.drawable.cross);
                response.setVisibility(View.VISIBLE);
                responseTxt.setText(wordTwo);
            }
            if(qid < 10){
                selectQuestion();
            }
            else {
                Intent intent=new Intent(levelFour.this,last.class);
                Bundle b= new Bundle();
                b.putInt("score",score);
                intent.putExtras(b);
                startActivity(intent);
            }
        }
        else if (answer > 9) {
            Log.e("tag","ans : " + answer);
            int enteredNum = Integer.parseInt(view.getTag().toString());
            enteredInput = enteredInput * 10 + enteredNum;
            answerTxt.setText("" + enteredInput);
            Log.e("tag","enteredInput : " + enteredInput);
            if (enteredInput == answer) {
                Log.e("tag","correct : " );
                //  correctMP.start();
                score++;
                Log.e("tag","score : " + score);
                response.setImageResource(R.drawable.tick);
                response.setVisibility(View.VISIBLE);
                responseTxt.setText(word);
                if(qid < 10){
                    selectQuestion();
                }
                else {
                    Intent intent=new Intent(levelFour.this,last.class);
                    Bundle b= new Bundle();
                    b.putInt("score",score);
                    intent.putExtras(b);
                    startActivity(intent);
                }
            }
            else if (enteredInput > 9) {
                Log.e("tag","wrong : " );
                //wrongMP.start();
                Log.e("tag","score : " + score);
                response.setImageResource(R.drawable.cross);
                response.setVisibility(View.VISIBLE);
                responseTxt.setText(wordTwo);
                if(qid < 10){
                    selectQuestion();
                }
                else {
                    Intent intent=new Intent(levelFour.this,last.class);
                    Bundle b= new Bundle();
                    b.putInt("score",score);
                    intent.putExtras(b);
                    startActivity(intent);
                }
            }
        }
    }
    else if (view.getId() == R.id.btn_clr)
    {
        answerTxt.setText("?");
        enteredInput=0;
    }
}

}

This is one level which generates random values for 10 questions ,I want to use the same order of question which is generated randomly in another level. For EX: Level 1 input 1: 4 and 3 input 2: 2 and 3 (generates randomly) Level 2 input 1: 3 and 4 input 2: 3 and 2 (from level 1 input)




Aucun commentaire:

Enregistrer un commentaire