Как использовать onItemSelectedListener внутри onClickListener?
Я делаю простой калькулятор, но с помощью счетчика как способа выбора оператора, поэтому всякий раз, когда я нажимаю "Рассчитать", он никогда не вычисляет его, но когда я изменяю элемент, выбранный на счетчике, он вычисляет x и y с их соответствующими операторами. Как правильно использовать /code onItemSelectedListener внутри onClickListener?
Вот мой код
package com.example.leks.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
private TextView tvResult;
private EditText etFirstnumber, etSecondnumber;
private Button btnCalculate;
private Spinner spnOperators;
private int result;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
etFirstnumber = (EditText) findViewById(R.id.etFirstnumber);
etSecondnumber = (EditText) findViewById(R.id.etSecondnumber);
spnOperators = (Spinner) findViewById(R.id.spnOperators);
tvResult = (TextView) findViewById(R.id.tvResult);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnCalculate.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
try {
switch (view.getId()) {
case R.id.btnCalculate:
spnOperators.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l)
{
String num1 = etFirstnumber.getText().toString();
String num2 = etSecondnumber.getText().toString();
int x = Integer.parseInt(num1);
int y = Integer.parseInt(num2);
String SelectedItem = parent.getItemAtPosition(position).toString();
if (SelectedItem.equals("+"))
{
result = x + y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("-"))
{
result = x - y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("*"))
{
result = x * y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("/"))
{
result = x/y;
tvResult.setText(String.valueOf(result));
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
break;
}
}catch(Exception e)
{
tvResult.setText("Error");
}
}
}
2 ответа
Попробуйте код ниже:
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
String num1="",num2="";
int x=0,y=0;
String SelectedItem ;
private TextView tvResult;
private EditText etFirstnumber, etSecondnumber;
private Button btnCalculate;
private Spinner spnOperators;
private int result;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
etFirstnumber = (EditText) findViewById(R.id.etFirstnumber);
etSecondnumber = (EditText) findViewById(R.id.etSecondnumber);
spnOperators = (Spinner) findViewById(R.id.spnOperators);
tvResult = (TextView) findViewById(R.id.tvResult);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnCalculate.setOnClickListener(this);
num1 = etFirstnumber.getText().toString();
num2 = etSecondnumber.getText().toString();
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
spnOperators.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l)
{
SelectedItem = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
}
@Override
public void onClick(View view)
{
try {
switch (view.getId()) {
case R.id.btnCalculate:
calculate();
break;
}
}catch(Exception e)
{
tvResult.setText("Error");
}
}
public void calculate(){
if (SelectedItem.equals("+"))
{
result = x + y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("-"))
{
result = x - y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("*"))
{
result = x * y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("/"))
{
result = x/y;
tvResult.setText(String.valueOf(result));
}
}
}
Пожалуйста, поместите ваш onItemSelectedListener в методе init.
Вам не нужно ставить его методом onClick.
private void init()
{
etFirstnumber = (EditText) findViewById(R.id.etFirstnumber);
etSecondnumber = (EditText) findViewById(R.id.etSecondnumber);
spnOperators = (Spinner) findViewById(R.id.spnOperators);
tvResult = (TextView) findViewById(R.id.tvResult);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnCalculate.setOnClickListener(this);
spnOperators.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l)
{
String num1 = etFirstnumber.getText().toString();
String num2 = etSecondnumber.getText().toString();
int x = Integer.parseInt(num1);
int y = Integer.parseInt(num2);
String SelectedItem = parent.getItemAtPosition(position).toString();
if (SelectedItem.equals("+"))
{
result = x + y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("-"))
{
result = x - y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("*"))
{
result = x * y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("/"))
{
result = x/y;
tvResult.setText(String.valueOf(result));
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
}