I'm a student who just got into Java and Android studios
public class MainActivity extends AppCompatActivity {
int num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = spinner.getSelectedItem().toString();
if(position == 0) {
Toast.makeText(getApplicationContext(), text + "You have selected.", Toast.LENGTH_SHORT).show();
num = 50 * 1000;
} } else if(position == 1) {
Toast.makeText(getApplicationContext(), text + "You have selected.", Toast.LENGTH_SHORT).show();
num = 100 * 1000;
} } else if(position == 2) {
Toast.makeText(getApplicationContext(), text + "You have selected.", Toast.LENGTH_SHORT).show();
num = 150 * 1000;
} } else if(position == 3) {
Toast.makeText(getApplicationContext(), text + "You have selected.", Toast.LENGTH_SHORT).show();
num = 300 * 1000;
} } else if(position == 4) {
Toast.makeText(getApplicationContext(), text + "You have selected.", Toast.LENGTH_SHORT).show();
num = 600 * 1000;
} } else if(position == 5) {
Toast.makeText(getApplicationContext(), text + "You have selected.", Toast.LENGTH_SHORT).show();
num = 1200 * 1000;
} } else if(position == 6) {
Toast.makeText(getApplicationContext(), text + "You have selected.", Toast.LENGTH_SHORT).show();
num = 1800 * 1000;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
System.out.println ("Num has a value of = " + num);
}
}
I want to get the num value of the selected spinner in System.out.println, but when I turn it like this, I get 0
What I want to do is that every time I change the value with a spinner, I want the value I wrote to be included in the num and it comes out as System.out.println.
How can I get the num value selected by the spinner?
android
Every time you select a value with a spinner,
System.out.println ("Num has a value of =" + num);
Do you want to invoke this code?
Then the corresponding code is also
Inside the spinner's setOnItemSelectedListener
Please put it in.
778 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
555 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
556 GDB gets version error when attempting to debug with the Presense SDK (IDE)
554 Uncaught (inpromise) Error on Electron: An object could not be cloned
554 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.