Home > OS >  Passing inputs through activities
Passing inputs through activities

Time:09-22

i was developing an app and this question showed up:

 EditText inputCorrect = (EditText) findViewById(R.id.inputCorrect);
        EditText inputWrong = (EditText) findViewById(R.id.inputWrong);
        EditText inputBlank = (EditText) findViewById(R.id.inputBlank);
        EditText inputAll = (EditText)findViewById(R.id.inputAll);


        String correctAmountText = inputCorrect.getText().toString();
        String wrongAmountText = inputWrong.getText().toString();
        String blankAmountText = inputBlank.getText().toString();
        String allAmountText = inputAll.getText().toString();


    myResultActivty.putExtra("c_a",correctAmountText);
    myResultActivty.putExtra("w_a",wrongAmountText);
    myResultActivty.putExtra("b_a",blankAmountText);
    myResultActivty.putExtra("a_a",allAmountText);
        startActivity(myResultActivty); 

this is the code there are 4 edit texts with inputType of decimal number i am getting string from them and send them to other activity. in the second activity i take those string and turn them into ints with parseint method:

String correctNum = inputActivity.getString("c_a");
String wrongNum = inputActivity.getString("w_a");
String blankNum = inputActivity.getString("b_a");
String allNum = inputActivity.getString("a_a");

float res = 0;
int cN = Integer.parseInt(correctNum);
int wN = Integer.parseInt(wrongNum);
int bN = Integer.parseInt(blankNum);
int aN = Integer.parseInt(allNum);

but whenever i want to do mathematical operations it crashes or shows zero as result.

CodePudding user response:

You could use intents to pass data between activities. In this case, you could create an intent like

Intent intent = Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key",10);
startActivity(intent);

In SecondActivity,

Bundle extras = getIntent().getExtras();
int yourNum = extras.getInt("key"); // you should get 10
  • Related