0

I appreciate any help and guidance. I am learning Java and creating my first Android app.

Layout: (1) Textview for numerical input (ID: inputValue) (2) spinners (ID: firstCity, secondCity) - both spinners have the same 5 cities (1) Button (ID: clickButton) (1) Textview for numerical output (ID: outputValue)

Functionality: 1. User types in numerical value into TextView widget1. Error message for no entry

2. User selects any of the 5 cities from spinner 1

3. User selects any of the 5 cities from spinner 2

4. User clicks button

5. Numerical output pops up in a TextView widget2.

I'm stuck on getting the numerical output (5). Each city has a numerical value assigned to it but I am having hard time trying to figure out the best way to represent this. Hashmap? String array? Switch? The cities are in the strings.xml below. Their respective values are 2,4,6,8,10.

The goal is to use this equation for output: widget1 inputValue x(spinner1/spinner2) = widget2 outputValue

strings.xml

<resources>
<string name="app_name">Cities</string>
<string-array name="cities_array">
    <item>Buffalo, NY</item>
    <item>Portland, OR</item>
    <item>Sacramento, CA</item>
    <item>Jackson, Wyoming</item>
    <item>Santa Fe, NM</item>
</string-array>

package;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.ArrayAdapter;

public class calculate extends AppCompatActivity {

Spinner spinnerA,spinnerB;
Button clickButton;
TextView outputValue;
TextView inputValue;
ArrayAdapter<CharSequence> adapter;


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

    final TextView inputValue = (TextView) findViewById(R.id.inputValue);

    outputValue = (TextView) findViewById(R.id.outputValue);
    Button ClickButton = (Button) findViewById(R.id.ClickButton);

    spinnerA = (Spinner)findViewById(R.id.firstCity); 
    spinnerB = (Spinner)findViewById(R.id.secondCity);

    adapter = ArrayAdapter.createFromResource(this,R.array.cities,
            android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerA.setAdapter(adapter);
    spinnerB.setAdapter(adapter);

    spinnerA.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            //Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+ "selected",
                    Toast.LENGTH_LONG).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    spinnerB.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

          //Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+ "selected",
                    Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    clickButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(inputValue.getText().length()<=0) {
                inputValue.setError("Must input value");
            }
        }
    });
   }

Any direction would be greatly appreciated.Thank you.

JiXu
  • 7
  • 3

2 Answers2

0

First things first:You cannot enter input into a TextView,you need to use EditText for that. Insert an edit text for user input, and implement its listeners. You can simply set the reference for the view as youare doing already and add a onTextChangedListener() to it. This will expose three methods you can override, you can choose the onTextChanged() method for your use case. Simply implement your logic inside this method and modify the textView content by textView.setText() call where the param will be the output of your calculation.

The follwoing links should help: Implementing Text Watcher for EditText

shiredude95
  • 560
  • 3
  • 7
0

Since you are using the button to generate the output, the only listener you should need is the onClickListener for that button. Also, when running an app you cannot type a value into a TextView, so change your input to an EditText.

EditText inputValue = (EditText) findViewById(R.id.inputValue);

Try this for your listener:

clickButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       // Just to make sure there is something in the input
       if(inputValue.getText() == null || inputValue.getText().length()<=0) {
           inputValue.setError("Must input value");
       } else {
            // Put in your formula

            // Get your values for your spinners using their index in the list
            // The index starts at zero, so adding one and
            // multiplying by 2 gives you your numbers
            int spinnerAValue = (spinnerA.getSelectedItemPosition()+1)*2;
            int spinnerBValue = (spinnerA.getSelectedItemPosition()+1)*2;

            // Convert the input to a number (use double if there are decimals)
            double input = Double.parseDouble(inputValue.getText());

            double result = input * (spinnerAValue/spinnerBValue);

            // Set the output TextView to the result, converting it to a string
            outputValue.setText(Double.toString(result));

        }
   }
}
JiXu
  • 7
  • 3
Nordii
  • 479
  • 1
  • 5
  • 15
  • Be sure to check the names of your button. You say the button's id is "clickButton" but your code uses "ClickButton" and "runButton" – Nordii Jun 07 '18 at 17:40
  • What if the values are 56, 73, 92, 123, 140 – JiXu Jun 07 '18 at 19:46
  • In the case of numbers that don't follow a simple pattern, I would use parallel arrays linking the numbers you want and the locations. As in, locations[1]'s number is numberList[1]. – Nordii Jun 07 '18 at 21:56