2015-04-22 64 views
0

我是新來的Android
我已經學會了如何通過putextra傳遞activites之間的數據,在我的應用程序getextra
,當我從活性1傳遞數據和活性2main_activity並添加數據和把它傳遞給第三活性(輸出
代替此外,只有要麼活性1或活性2的值傳遞給輸出正被傳遞(值被一個我在運行我的應用程序時最後輸入的)
這裏是我的應用程序的代碼
Mainactivity如何活動之間傳遞數據和對數據進行算術運算

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Intent j = getIntent(); 
     a= j.getDoubleExtra("double1",0.0); 

     Intent k = getIntent(); 
     b = k.getDoubleExtra("double2",0.0); 
     result = a+b; 
    } 


    @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_main, 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 on_click_activity1button(View view) { 
     Intent intent1 = new Intent(this,activity1.class); 
       startActivity(intent1); 

    } 

    public void on_click_activity2button(View view) { 
     Intent intent2 = new Intent(this,activity2.class); 
     startActivity(intent2); 

    } 

    public void on_click_outputbutton(View view) { 
     Intent intent3 = new Intent(this,output.class); 
     intent3.putExtra("result", result); 

     startActivity(intent3); 

    } 
} 

活性1

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity1); 
    } 
    public void on_click_activity1done(View view) { 

     EditText text1 = (EditText) findViewById(R.id.activity1edittext); 
     a=Double.parseDouble(text1.getText().toString()); 

     Intent fromactivity1 = new Intent(this,MainActivity.class); 
     fromactivity1.putExtra("double1", a); 
     startActivity(fromactivity1); 
    } 
} 

Activity2.java

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity2); 
    } 

    public void on_click_activity2done(View view) { 

     EditText text2 = (EditText) findViewById(R.id.activity2edittext); 
     b=Double.parseDouble(text2.getText().toString()); 

     Intent fromactivity2 = new Intent(this,MainActivity.class); 
     fromactivity2.putExtra("double2", b); 
     startActivity(fromactivity2); 
    } 
} 

output.java

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.output); 
     Intent frommaindone = getIntent(); 
     Double total = frommaindone.getDoubleExtra("result",0.0); 
     TextView show = (TextView) findViewById(R.id.resulttextview); 
     show.setText("addition is" + total); 
    } 
} 

請幫助我,並告訴我,我做錯了什麼

+0

你這樣做的方式是不正確的,你正在創建多個主要活動,沒有人的數字將永遠是錯誤的。看看startActivityForResult –

+0

我也試過startActivityForResult,但同樣的問題正在發生 – Navdeep

+0

當我運行我的應用程序,並按下按鈕1以獲取textfrom(雙)activity1之後,當我按下按鈕2以獲得textfrom(雙)activity2,我認爲來自activity1的變量值被銷燬。我想知道如何保留它,以便我可以添加兩個值a和b。 – Navdeep

回答

0

您必須使用startActivityForResult方法從兩個輸入活動中獲取值,然後將結果傳遞給輸出活動。按以下方式更改代碼並嘗試。

活性1

公共類活性1延伸ActionBarActivity {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity1); 
} 

public void on_click_activity1done(View view) { 

    EditText text1 = (EditText) findViewById(R.id.activity1edittext); 
    double b = Double.parseDouble(text1.getText().toString()); 

    Intent result = new Intent(); 
    result.putExtra("double1", b); 
    setResult(RESULT_OK, result); 
    finish(); 

} 

}

Activity2.java

public class Activity2 extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity2); 
    } 


    public void on_click_activity2done(View view) { 

     EditText text2 = (EditText) findViewById(R.id.activity2edittext); 
     double b = Double.parseDouble(text2.getText().toString()); 

     Intent result = new Intent(); 
     result.putExtra("double2", b); 
     setResult(RESULT_OK, result); 
     finish(); 

    } 
} 

MainActivity.java

public class MainActivity extends ActionBarActivity { 

    private final int ACTIVITY1 = 1; 
    private final int ACTIVITY2 = 2; 
    private double result, double1,double2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    } 

    public void onActivityOneClick(View view) { 
     Intent intent = new Intent(this, Activity1.class); 
     startActivityForResult(intent, ACTIVITY1); 
    } 

    public void onActivityTwoClick(View view) { 
     Intent intent = new Intent(this, Activity2.class); 
     startActivityForResult(intent, ACTIVITY2); 
    } 

    public void on_click_outputbutton(View view) { 
     result = double1 + double2; 
     Intent intent = new Intent(this, OutPut.class); 
     intent.putExtra("result", result); 
     startActivity(intent); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if(resultCode==RESULT_OK){ 
      switch(requestCode){ 
      case ACTIVITY1:  
       double1 = data.getDoubleExtra("double1", 0); 
       break; 
      case ACTIVITY2: 
       double2 = data.getDoubleExtra("double2", 0); 
       break; 
      }     
     } 
    } 
} 

輸出。java

public class OutPut extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.output); 

     double total = getIntent().getDoubleExtra("result", 0);  
     TextView show = (TextView) findViewById(R.id.resulttextview); 
     show.setText("addition is" + total); 
    } 

} 
+0

非常感謝你..我與最後兩天的問題鬥爭我嘗試使變量靜態,使用startActivityforResult,使用捆綁等但沒有任何工作,你用開關盒來解決問題,你可以解釋我做錯了什麼,你是如何得到解決方案 – Navdeep

+0

當應用程序運行它創建一個實例MainActivity,但你通過調用創建兩個新的MainActivity實例從兩個輸入活動開始活動。所以當你點擊上次創建的mainactivity實例上的輸出按鈕時,它只會顯示你在上一次輸入活動中輸入的值。 – TdSoft

0

這是不可能的,你的代碼的工作,因爲當你回來,並保存一個值,走出去另一個,你失去了舊的價值。

在我看來,你正在爲這個問題做一個壞的方法,但是如果你真的想這樣做,我唯一擁有的就是使用靜態變量,儘管我不推薦這樣做。因此,在MainActivity上創建兩個靜態變量,保存數據時,在那裏執行此操作,並且當您想要輸出時,請在兩者之間發送操作。這種方式工作。

相關問題