Spinner value forwarding question.

Asked 1 years ago, Updated 1 years ago, 133 views

I'm making an application to calculate the amount of cat food.

I'm having a hard time with a spinner.

Build it, run the emulator, and it's just stored in the spinner as the value of the first array Afterwards, even if you select a different array and press the button, the result is only the first array value. I tried it with if (button!= null) but as soon as I ran the emulator, it was only the first arrangement in the spinner The value is saved, and it doesn't change even if I change it afterwards What should I do with this?

public class MainActivity extends AppCompatActivity {
    double gaJung;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);


        String[] items = new String[8];
        items[0] = "less than 4 months";
        items[1] = "4 to 6 months";
        items[2] = "7-12 months";
        items[3] = "Neutralized Holy Sepulchre";
        items[4] = "General Holy Sepulchre";
        items[5] = "Exercise-intensive graves";
        items[6] = "Roh Myo";
        items[7] = "Obese tomb";


        // a code used to write a spinner //
        final spinner spinner = findViewById(R.id.spinner); // initialize spinner
        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


            String selectedItemName = spinner.getSelectedItem().toString();
            if (selectedItemName.equals ("old seed") {
                gaJung = 0.7;
            } else if (selectedItemName.equals ("obesity") {
                gaJung = 0.8;
            } else if (selectedItemName.equals ("less than 4 months") {
                gaJung = 3.0;
            } else if (selectedItemName.equals ("4 to 6 months") {
                gaJung = 2.5;
            } else if (selectedItemName.equals ("7-12 months") {
                gaJung = 2.0;
            } else if (selectedItemName.equals ("neutrified graves") {
                gaJung = 1.2;
            } else if (selectedItemName.equals ("General Holy Sepulchre") {
                gaJung = 1.4;
            } else if (selectedItemName.equals ("Exerciseful Grave") {
                gaJung = 1.6;
            }



        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                double Der = 10 * gaJung;
                String str = String.valueOf(Der);
                showToast(str);
            }
        });

spinner android-studio

2022-09-20 22:04

1 Answers

Every time a spinner is selected

Among the spinner's listeners, onItemSelected is called.

https://developer.android.com/guide/topics/ui/controls/spinner?hl=ko

I recommend you read this article.


2022-09-20 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.