jpgerb
jpgerb

Reputation: 1120

not displaying calculation in textview from editview

I am trying to allow people to input hypothetical new loan information in to editText fields. Upon clicking on button "Calculator" I want it to calculate the monthly payment.

Below is the code on the calculator.java page I have.

public class AddDebt extends Activity implements OnClickListener {
Button Home;

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


}

This part is for my Menu to switch between my pages (Activities):

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

    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;
}

I have "android:onClick="ButtonOnClick"" declared in my .xml on the Calculator button:

    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();
            int myEditBal = Integer.parseInt(myEditValue);

            EditText myEdit2 = (EditText) findViewById(R.id.editRate);
            String myEditValue2 = myEdit2.getText().toString();
            int myEditRate = Integer.parseInt(myEditValue2);

            EditText myEdit3 = (EditText) findViewById(R.id.editTerm);
            String myEditValue3 = myEdit3.getText().toString();
            int myEditTerm = Integer.parseInt(myEditValue3);

Also FYI: I know this is not a valid loan calculation, I'm using it to ensure I get a valid number:

            int editMnthlypmt = myEditBal + myEditRate + myEditTerm;

            TextView textMnthlypmt = new TextView(this);
            textMnthlypmt.setText("" + String.valueOf(editMnthlypmt));

        break;
    }
}

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

    }
}

I can input the data; however, it does not populate the answer in to my TextView field "textMnthlypmt". Any ideas?

Upvotes: 0

Views: 80

Answers (3)

CRUSADER
CRUSADER

Reputation: 5472

You have created an instance of TextView but you haven't added to parentView...

try like this..

TextView textMnthlypmt = new TextView(this);
textMnthlypmt.setText("" + String.valueOf(editMnthlypmt));
parentView.addView(textMnthlypmt);//where parentView is parent layout defined in layout.activity_new_debt

Hope this helps..

Upvotes: 0

Rajnish Mishra
Rajnish Mishra

Reputation: 836

check out you xml once again and

use a already declared text view

TextView textMnthlypmt = new TextView(this);

with this code you are creating a new text view but not putting it on activity.

Upvotes: 0

Looking Forward
Looking Forward

Reputation: 3585

textMnthlypmt.setText("" + String.valueOf(editMnthlypmt));

Try this...

Upvotes: 1

Related Questions