如何在nextActivity上显示计算值?

时间:2014-02-03 09:31:56

标签: android android-intent

这是我的代码来计算包的价格。

public class SecondActivity extends Activity implements OnClickListener {

        double LRU;
        double KU;
        double EX;
        double KRF;
        double  KRT;
        double  KRF1;
        double  KRT1;
        double ANSWER;
        int RE=100;
        double jg=17.775;
        Button refresh;
        Intent refreshIntent;
        RadioGroup rg;
        RadioButton yes;
        RadioButton no;
        DBAdapter dba;
        SQLiteDatabase database;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second_main);
            looseRueUtaro = (EditText) findViewById(R.id.elooserue);
            kapasiyaUtaro = (EditText) findViewById(R.id.ekapasiyautaro);
            expense = (EditText) findViewById(R.id.eexpense);
            kapasRateFrom = (EditText) findViewById(R.id.ekapasratefrom);
            kapasRateTo = (EditText) findViewById(R.id.ekapasrateto);
            kapasiyaRateFrom = (EditText) findViewById(R.id.ekapasiyaratefrom);
            kapasiyaRateTo = (EditText) findViewById(R.id.ekapasiyarateto);
            txtLooseRue = (TextView) findViewById(R.id.tlooserue);
            txtKapasiyaUtaro = (TextView) findViewById(R.id.tkapasiyautaro);
            txtexpense = (TextView) findViewById(R.id.texpense);
            txtkapasRateFrom = (TextView) findViewById(R.id.tkapasratefrom);
            txtkapasRateTo = (TextView) findViewById(R.id.tkapasrateto);
            txtkapasiyaRateFrom = (TextView) findViewById(R.id.tkapasiyaratefrom);
            txtkapasiyaRateTo = (TextView) findViewById(R.id.tkapasiyarateto);
            txtRoundOff = (TextView) findViewById(R.id.troundoff);


            yes = (RadioButton) findViewById(R.id.ryes);

            no = (RadioButton) findViewById(R.id.rno);
            rg=(RadioGroup)findViewById(R.id.rg);
            refresh = (Button) findViewById(R.id.refresh1);
            refresh.setOnClickListener(this);
        }
        public void onRadioButtonClicked(View view) {
            // Is the button now checked?
            boolean checked = ((RadioButton) view).isChecked();

            // Check which radio button was clicked
            switch(view.getId()) {
                case R.id.ryes:
                    if (checked)

                    break;
                case R.id.rno:
                    if (checked)

                    break;
            }
        }
        public void onClick(View v) {
            // insert data into database
            getTextAndStoreIntoDB();
            // /////////////
            refreshIntent = new Intent(SecondActivity.this, ThirdActivity.class);
            Toast.makeText(SecondActivity.this, "Saved Successfully!!!",
                        Toast.LENGTH_LONG).show();
             refreshIntent.putExtra("AddValue", ANSWER);
                startActivity(refreshIntent);

        }

        public void getTextAndStoreIntoDB() {
            String looseRue = looseRueUtaro.getText().toString();
            String kapasiya = kapasiyaUtaro.getText().toString();
            String expenseStr = expense.getText().toString();
            String kapasRateFr = kapasRateFrom.getText().toString();
            String kapasRateto = kapasRateTo.getText().toString();
            String kapasiyaRateFr = kapasiyaRateFrom.getText().toString();
            String kapasiyaRateto = kapasiyaRateTo.getText().toString();

            String[] columns = { DBAdapter.COLUMN_LOOSERUE,
                    DBAdapter.COLUMN_KAPASIYAUTARO, DBAdapter.COLUMN_EXPENSE,
                    DBAdapter.COLUMN_KAPASRATETO, DBAdapter.COLUMN_KAPASRATEFROM,
                    DBAdapter.COLUMN_KAPASIYARATETO,
                    DBAdapter.COLUMN_KAPASIYARATEFROM };
            Object[] values = { looseRue, kapasiya, expenseStr, kapasRateto,
                    kapasRateFr, kapasiyaRateto, kapasiyaRateFr };
            dba = new DBAdapter(getApplicationContext());
            database = dba.getWritableDatabase();
            dba.insertIntoDatabase(columns, values);
             LRU= Double.parseDouble(looseRueUtaro.getText().toString());
             KU = Double.parseDouble(kapasiyaUtaro.getText().toString());
             EX = Double.parseDouble(expense.getText().toString());
             KRF = Double.parseDouble(kapasRateFrom.getText().toString());
             KRT = Double.parseDouble(kapasRateTo.getText().toString());
             KRF1= Double.parseDouble(kapasiyaRateFrom.getText().toString());
             KRT1 = Double.parseDouble(kapasiyaRateTo.getText().toString());

             ANSWER = ((((KRF+EX)*RE)-(KRF1*KU))/LRU)*jg;


        }


    }

此代码用于计算Bales的结果,但它显示在当前活动上,我想在ThirdActivity(nextActivity)上显示结果。

我可以从EditTetxt计算结果,但我想在ThirdActivity上显示结果 我试过了。但无法解决问题。

有人可以建议我怎么做吗?

2 个答案:

答案 0 :(得分:0)

OnCreate()方法中的第三个活动中!使用 getIntent()

double addValue = getIntent().getDoubleExtra("AddValue");

如果你在另一个活动中传递它:

Intent intent = new Intent(this, YourNewActivty.class);
intent.putExtra("AddValue", value);
startActivity(intent);

并使用 getIntent()来获取上述值。

答案 1 :(得分:0)

尝试使用Bundle:

第二项活动;

public void onClick(View v) {
    Intent intent = new Intent(v.getContext(), FIRSTACTIVITY.class);
    Bundle bundle = new Bundle();
    bundle.putDouble("AddValue",ANSWER);
    intent.putExtras(bundle);               
    startActivity(intent);
}

第三项活动:

public void activity_value() {
Intent i = getIntent();
Bundle extras=i.getExtras();
if(extras !=null) {           
    double answer = extras.getDouble("AddValue");
}
}

希望它对你有所帮助。请尝试让我知道。感谢。