简体   繁体   中英

Targetting user selection from a spinner and using it in next activity - Android

I am having a bit of a dilemma at the moment with regards to the best way to target and use what the user has selected from a spinner (drop-down menu) in my application.

To give you some insight, say I have two activities, one called create line which is where the user selects their option from the spinner and the next called line details where I want to be able to work with what the user has selected.

I am currently successfully saving the users drop down selection to an SQLite database.

In my spinner the user can either select the options of 20 or 30. On the line details which is the second activity I want to be able to write an IF statement to tell my app to do something depending on whether they have selected 20 or 30?

What is the best method to get the users selections and prepare an IF statement?

  • Should I use a db.query to get what the user has selected and use it in an IF statement?

    • Should I store the selection in a Sharedpreference and target it that way?

Here is the spinner code:

spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {


            switch (position) {
                case 0:
                    quantity.setText("");
                    ;
                    break;
                case 1:
                    quantity.setText(PackageType20);
                    timeadded = 28;
                    duration.setText(timeadded + " Hours");


                    break;
                case 2:
                    quantity.setText(PackageType30);
                    timeadded = 27;
                    duration.setText(timeadded + " Hours");

                    break;
                default:
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

EDIT WITH ATTEMPTED METHOD:

CREATE LINE: on create line I am storing the users selection in a public static variable:

public static spinnerSelection2 = String.valueOf(spinner2.getSelectedItem());

LINEDETAILS: then I am referencing that string on the next activity and setting it to this variable in which I will use in the IF statement:

 String spinnerSelection2 = CreateLine.spinnerSelection2;

    if (spinnerSelection2.equals("20")) {
        optimum20 = (int) (diffminutes * 0.383);
        percentage20 = optimum20 / (648 * 100);
    } else if (spinnerSelection2.equals("30"))
            optimum30 = (int) (diffminutes * 0.373);
            percentage30 = optimum30 / (605 * 100);

I am not quite sure of what method is the best to use and also unsure of how to do each.. any help would be great!

Thanks

To do that in your switch cases add following lines to go into new activity:

Intent intent = new Intent(getContext(),yourSecondActivity.class);
intent.putExtra("duration",duration);
intent.putExtra("PackageType", packageType);

In your second activity create two strings to store these values. And to import these values there in on create write this :

yourVariable = intent.getStringExtra("duration");
yourSecondVariable = intent.getStringExtra("packageType");

After that you can apply if else to check which condition satisfies and do operations there accordingly.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM