How to get money from Android?

Asked 2 years ago, Updated 2 years ago, 20 views

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

2022-09-21 10:09

1 Answers

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.


2022-09-21 10:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.