No matter how much I edit or change to my Java code for this assignment, it refused to properly update the value of the integer variable I've set for the result. Can someone show me what on earth I'm missing here? Below is my code: package com.example.minicalc;
import com.example.minicalc.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
public class MainActivity extends Activity implements OnEditorActionListener {
private static final String TAG = "MiniCalc";
private SharedPreferences savedValues;
private EditText editText1;
private EditText editText2;
private Button button1;
private Spinner spinner1;
private TextView answerLbl;
private int ivalue1 = 0;
private int ivalue2 = 0;
private int ianswer;
private int spinChoice;
//private String[] spinner_strings = {"+", "-", "×", "÷"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate event started");
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
button1 = (Button) findViewById(R.id.button1);
answerLbl = (TextView) findViewById(R.id.answerTxt);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText1.setOnEditorActionListener(this);
editText2.setOnEditorActionListener(this);
savedValues = getSharedPreferences("SavedValues", MODE_PRIVATE);
spinChoice = spinner1.getSelectedItemPosition();
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.d(TAG, "onClick event started");
ianswer = 0;
if (spinChoice == 0) {
ianswer = ivalue1 + ivalue2;
Log.d(TAG, "ianswer VALUE IS: " + Integer.valueOf(ianswer).toString());
} else if (spinChoice == 1) {
ianswer = ivalue1 - ivalue2;
Log.d(TAG, "ianswer VALUE IS: " + Integer.valueOf(ianswer).toString());
} else if (spinChoice == 2) {
ianswer = ivalue1 * ivalue2;
Log.d(TAG, "ianswer VALUE IS: " + Integer.valueOf(ianswer).toString());
} else if (spinChoice == 3) {
ianswer = ivalue1 / ivalue2;
Log.d(TAG, "ianswer VALUE IS: " + Integer.valueOf(ianswer).toString());
}
answerLbl.setText(Integer.toString(ianswer));
//answerLbl.setText(""+ianswer);
Log.d(TAG, "answerLbl TEXT IS: " + answerLbl.getText());
}
});
}
@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);
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.d(TAG, "onEditorAction event started");
if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
ivalue1 = Integer.parseInt(editText1.getText().toString());
ivalue2 = Integer.parseInt(editText2.getText().toString());
}
return false;
}
@Override
public void onPause() {
Log.d(TAG, "onPause event started");
Editor editor = savedValues.edit();
editor.putString("value1", editText1.getText().toString());
editor.putString("value2", editText2.getText().toString());
editor.commit();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume event started");
editText1.setText(savedValues.getString("value1", ""));
editText2.setText(savedValues.getString("value2", ""));
}
}
Here is my layout XML, activity_main.xml:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.minicalc.MainActivity" >
<TextView
android:id="@+id/valLbl1"
android:labelFor="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="40dp"
android:text="@string/valueLbl1" />
<TextView
android:id="@+id/valLbl2"
android:labelFor="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/valLbl1"
android:layout_below="@+id/editText1"
android:layout_marginTop="24dp"
android:text="@string/valueLbl2" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/valLbl1"
android:layout_below="@+id/valLbl1"
android:layout_marginTop="20dp"
android:ems="5"
android:inputType="number">
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/valLbl2"
android:layout_below="@+id/valLbl2"
android:layout_marginTop="16dp"
android:ems="5"
android:inputType="number" />
<TextView
android:id="@+id/answerTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/answerLblTxt"
android:layout_centerHorizontal="true"
android:ems="7"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="@string/submit" />
<TextView
android:id="@+id/answerLblTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/editText2"
android:layout_below="@+id/button1"
android:layout_marginTop="16dp"
android:text="@string/answerLbl" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/valLbl2"
android:layout_marginStart="28dp"
android:layout_toEndOf="@+id/editText2"
android:entries="@array/spinner_strings" />
</RelativeLayout>
Now, the app runs fine, the LogCat messages show up fine...except when I put them in the onClickListener. So yes, I've sorta identified WHERE the problem is, I just don't know WHAT the problem is. I'd show a screen shot of the error, but this is my first time posting. In any case, the phone I'm running the app on is a Samsung Galaxy Note 3, running Android 4.4.4. Can anyone help me?
No comments:
Post a Comment