获取1个活动中的数据以显示在另一个活动中

时间:2013-09-21 03:53:43

标签: android android-activity

我正在使用AddDebt活动来获取债务信息。我希望它然后将数据传递给我的MainActivity活动以供显示。

AddDebt如下:

public class AddDebt extends Activity implements OnClickListener {
Button Home;

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


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.default_menu, menu);

    MenuItem gohome = menu.findItem(R.id.gohome);
    Intent intent5 = new Intent(this, MainActivity.class);
    gohome.setIntent(intent5);

    MenuItem newDebt = menu.findItem(R.id.newDebt);
    Intent intent1 = new Intent(this, AddDebt.class);
    newDebt.setIntent(intent1);

    MenuItem About = menu.findItem(R.id.about);
    Intent intent2 = new Intent(this, About.class);
    About.setIntent(intent2);

    MenuItem Calcu = menu.findItem(R.id.Calcu);
    Intent intent3 = new Intent(this, Calculator.class);
    Calcu.setIntent(intent3);

    MenuItem Manu = menu.findItem(R.id.manu);
    Intent intent4 = new Intent(this, ManuPage.class);
    Manu.setIntent(intent4);
    return true;
}

    public void ButtonOnClick(View v) {
    // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.calculate:

            EditText myEdit = (EditText)     findViewById(R.id.editBalance);
            String myEditValue = myEdit.getText().toString();
            double loanAmount = Double.parseDouble(myEditValue);

            EditText myEdit2 = (EditText) findViewById(R.id.editRate);
            String myEditValue2 = myEdit2.getText().toString();
            double interestRate = Double.parseDouble(myEditValue2);

            EditText myEdit3 = (EditText) findViewById(R.id.editTerm);
            String myEditValue3 = myEdit3.getText().toString();
            Double loanPeriod = Double.parseDouble(myEditValue3);

            double r = interestRate/1200;
            double r1 = Math.pow(r+1,loanPeriod);

            double editMnthlypmt = (double) ((r+(r/(r1-1))) * loanAmount);

            TextView textMnthlypmt = (TextView)findViewById(R.id.textMntlypmt);
            textMnthlypmt.setText("" + String.valueOf(editMnthlypmt));

        break;
    }
}

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}

当我的布局中的“添加”按钮被选中时,我希望它将数据发送到此处显示的MainActivity:

public class MainActivity extends Activity  implements OnClickListener {
    Button addDebtor, Menu, About, Calc;

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_table);
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.default_menu, menu);

            MenuItem gohome = menu.findItem(R.id.gohome);
            Intent intent5 = new Intent(this, MainActivity.class);
            gohome.setIntent(intent5);

            MenuItem newDebt = menu.findItem(R.id.newDebt);
            Intent intent1 = new Intent(this, AddDebt.class);
            newDebt.setIntent(intent1);

            MenuItem About = menu.findItem(R.id.about);
            Intent intent2 = new Intent(this, About.class);
            About.setIntent(intent2);

            MenuItem Calcu = menu.findItem(R.id.Calcu);
            Intent intent3 = new Intent(this, Calculator.class);
            Calcu.setIntent(intent3);

            MenuItem Manu = menu.findItem(R.id.manu);
            Intent intent4 = new Intent(this, ManuPage.class);
            Manu.setIntent(intent4);
            return true;
        }


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
}

我不熟悉如何在任何级别上执行此操作,并且无法找到任何可行的教程来提供帮助。截至目前,我只担心传递1组数据,我可以稍后再添加......

1 个答案:

答案 0 :(得分:2)

如果要将数据从AddDebit传递给mainactivity。我正在将loanAmount值用于另一个活动。

    EditText myEdit = (EditText)findViewById(R.id.editBalance);
    String myEditValue = myEdit.getText().toString();
    Double loanAmount = Double.parseDouble(myEditValue);
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("loanAmount",loanAmount);
    startActivity(intent);

    MainActivity.class

   TextView t;
   Double value1 = super.getIntent().getExtras().getDouble("loanAmount");
   t=(TextView)findViewById(R.id.textView1);
   t.setText("LoanAmount  :" + value1);  
   Similarly you can pass values for another activity and get values from AddDebit