0

The functionality of my program is not working properly. It is not doing what I need it to do. I don't have any errors in the code but the app isn't doing what I want it to do.

**UPDATE: Okay thank you very much for the help. the button displays a value for TextView ce. However, the app does not seem to run my ifstatement to get a new value of CE1 which would be = 4*credit_1. The app is taking the initialized value of CE1 which is = 0, and converting that to a string CE1s. Hence dispalying 0.0 in TextView ce. Why is the app not taking my ifstatement if(grade_1.equals("A")) into consideration?

Here is the MainActivity.java:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText course1 = (EditText)findViewById(R.id.Course1);
    EditText credit1 = (EditText)findViewById(R.id.editText7);
    final EditText grade1 = (EditText)findViewById(R.id.editText13);
    final Button b = (Button)findViewById(R.id.button1);
    final TextView ce = (TextView)findViewById(R.id.textView5);
    String grade_1 = grade1.getText().toString();
    String text = credit1.getText().toString();
    if (!TextUtils.isEmpty(text)) {
        // call parseDouble in here
        double credit_1 = Double.parseDouble(text); 
        double CE1=0;
        if (grade_1 =="A")
            CE1 = 4*credit_1;
        final String CE1s = Double.toString(CE1);

        b.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
            ce.setText(CE1s);
            }   
        });
    }

    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }


    }
}

Here is the AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rutgersgpacalculator"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.rutgersgpacalculator.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
yellowmonkey
  • 197
  • 3
  • 11
  • 1
    yep nice, and WHAT is not working? – PKlumpp May 24 '14 at 18:36
  • ---> Why is the app not taking my if statement if(grade_1.equals("A")) into consideration? Well, you will need to grab the value again. Onec it's assigned to grade_1 it's initialized and value has been set. Use (grade1.getText().toString().toLowerCase().equals("a")) ? 4*credit_1 : 0 And do not forget to upvote if it helped you solving your problem. – Emanuel May 24 '14 at 20:30

1 Answers1

0

make sure that you verify if text is not null and it's rode when you click it. You should also consider verify without TextUtils like if (text != null && text.length > 0) {...}

    b.setOnClickListener(new OnClickListener(){
    public void onClick(View v){

    // get the text when you click it to make sure it's live while click
    String text = credit1.getText().toString();

    if (!TextUtils.isEmpty(text)) {
    double credit_1 = Double.parseDouble(text); 
    ce.setText(Double.toString( (grade_1.equals("A")) ? 4*credit_1 : 0));
    }
 });
Emanuel
  • 8,027
  • 2
  • 37
  • 56