I have some items (strings) in my spinner and when I select one of these items, I want back an associated value, for example:
If I select SAE 1020, it returns me 250.0 If I select E-155, it returns me 300.0
The value is suposed to be shown in a EditText, but the only value I see is 250.0. After all, this value should be sent to the next Activity (I also do not know if I'm doing the transfer correctly).
What is happening? What can I do?
Code:
package com.mateus.ligacoestubulares;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class Dados extends AppCompatActivity {
String [] AçoMontante = {"SAE 1020", "E-155"};
String [] AçoBanzo = {"SAE 1020", "E-155"};
String [] EspessuraT1 = {"0,75 mm", "0,90 mm", "0,95 mm"};
String [] EspessuraTo = {"0,75 mm", "0,90 mm", "0,95 mm"};
EditText campoFy1;
EditText campoFyo;
EditText campoT1;
EditText campoTo;
EditText normalM;
EditText normalB;
EditText momentoB;
EditText teta;
Button next;
Intent intentNext;
Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dados);
Spinner spinnerMont = (Spinner) findViewById(R.id.spinnerM);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, AçoMontante);
spinnerMont.setAdapter(arrayAdapter1);
campoFy1 = (EditText) findViewById(R.id.fy1);
String montStr = spinnerMont.getSelectedItem().toString();
String fy1 = Double.toString(choiceM(montStr));
campoFy1.setText(fy1);
Spinner spinnerBanzo = (Spinner) findViewById(R.id.spinnerB);
ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, AçoBanzo);
spinnerBanzo.setAdapter(arrayAdapter2);
campoFyo = (EditText) findViewById(R.id.fyo);
String banzoStr = spinnerBanzo.getSelectedItem().toString();
String fyo = Double.toString(choiceB(banzoStr));
campoFyo.setText(fyo);
next = (Button) findViewById(R.id.prosseguir);
intentNext = new Intent(Dados.this, ConferenciaDosDados.class);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String resistenciaM = campoFy1.getText().toString();
String resistenciaB = campoFyo.getText().toString();
bundle.putString("fy1",resistenciaM);
bundle.putString("fyo",resistenciaB);
intentNext.putExtras(bundle);
startActivity(intentNext);
}
});
}
public double choiceM(String str) {
Double f1 = 0.0;
if (str.equals(AçoMontante[0])) {
f1 = 250.0;
} else if (str.equals(AçoMontante[1])) {
f1 = 300.0;
}
return f1;
}
public double choiceB(String str) {
Double fo = 0.0;
if (str.equals(AçoMontante[0])) {
fo = 250.0;
} else if (str.equals(AçoMontante[1])) {
fo = 300.0;
}
return fo;
}
}
Try this. This code is working as you want it to.
String [] AçoMontante = {"SAE 1020", "E-155"};
String [] AçoBanzo = {"SAE 1020", "E-155"};
String [] EspessuraT1 = {"0,75 mm", "0,90 mm", "0,95 mm"};
String [] EspessuraTo = {"0,75 mm", "0,90 mm", "0,95 mm"};
EditText campoFy1;
EditText campoFyo;
Button next;
Intent intentNext;
Bundle bundle;
String montStr,banzoStr, f1,fo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
campoFy1 = (EditText) findViewById(R.id.fy1);
campoFyo = (EditText) findViewById(R.id.fyo);
Spinner spinnerMont = (Spinner) findViewById(R.id.spinnerM);
Spinner spinnerBanzo = (Spinner) findViewById(R.id.spinnerB);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, AçoMontante);
ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, AçoBanzo);
spinnerMont.setAdapter(arrayAdapter1);
spinnerBanzo.setAdapter(arrayAdapter2);
spinnerMont.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
montStr = (String) parent.getItemAtPosition(i);
if(montStr == "SAE 1020"){
f1 = "250.0";
}
else if (montStr == "E-155"){
f1 = "300.0";
}
else{}
campoFy1.setText(f1);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
spinnerBanzo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
banzoStr = (String) parent.getItemAtPosition(i);
if(banzoStr == "SAE 1020"){
fo = "250.0";
}
else if (banzoStr == "E-155"){
fo = "300.0";
}
else{}
campoFyo.setText(fo);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
next = (Button) findViewById(R.id.prosseguir);
intentNext = new Intent(MainActivity.this, Second.class);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String resistenciaM = campoFy1.getText().toString();
String resistenciaB = campoFyo.getText().toString();
bundle.putString("fy1",resistenciaM);
bundle.putString("fyo",resistenciaB);
intentNext.putExtras(bundle);
startActivity(intentNext);
}
});
}
Also, instead of bundles, you can just use private static variables to pass the value.
set f1
and fo
as
private static String f1,fo;
instead of String f1,fo;
.
And call them in another activity as Dados.f1
or Dados.fo
[ Dados here is your current activity ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.