String[] items = { "less than 4 months", "4 to 6 months", "7 to 12 months", "neutralized grave", "General grave", "Exercise grave", "Roh grave", "Obese grave"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// a code used to write a spinner //
Spinner spinner = findViewById (R.id.spinner); // Spinner initialization
ArrayAdapter<String>adapter=newArrayAdapter<String>( // Let's write the array as a string type.
this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
} // Adapter Method Declaration for Spinner Writing
});
Button button = findViewById(R.id.button);
if (items.equals (the "old tomb")
if ( button != null ) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast("g");
}
});
}
}
}
Create public void showToast(String data) { //Toast Message Method
Toast.makeText(this, data, Toast.LENGTH_LONG).show();
}
}
I created a spinner and created an array as shown above. For example, when I choose "Roh Myo" for spinners, I want to have a random toast message That's not gonna work. What do I do?
spinner android-studio
if (items).equals (the "old tomb")
1 When the condition of the spinner becomes "old seedling".
Button.setOnClickListener(newView).OnClickListener() {
I will register a click listener at button 2.
I think that's what you've been working on..
Let's think differently.
First, register 'Button Click Listener' () {
Why don't you set the value stored in the spinner to be read?
}
Here's what I think.
if (button!=null) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String selectedItemName = spinner.getSelectedItem().toString();
if (selectedItemName.equals ("old seed") {
showToast("g");
} else if (selectedItemName.equals ("obesity") {
showToast("h");
}
}
});
}
1st -> To achieve 2nd order in the first place
The value of the spinner is also the same as the button
A device is required to monitor operations with varying status (although it does exist). https://mailmail.tistory.com/16 See
And simply by putting an if statement at the time of onCreate, it doesn't detect any state value of any object.
Then you don't need a button click listener.
if (Button.onClick) {}
It would have been the same form.
/** First, prepare a device to detect and condition it **/
Please think about why the event listener exists and how it works.
© 2024 OneMinuteCode. All rights reserved.