2

I have 4 activity with spinner inside every activity and this spinner include 3 string data (drop-down selection), when I pass from activity to another one I must pass this selected data inside spinner like if I have chosen data x from the list in the spinner and click in button the selected data must be in second activity spinner as x too. I read several solutions without any solving. I hope to solve it here and this is my code for spinner and where to put the intent code

public class Page1 extends AppCompatActivity {
    Spinner spinner;
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page1); 

    spinner = (Spinner)findViewById(R.id.spinner);
    adapter =   ArrayAdapter.createFromResource(this,R.array.film_type,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();

            switch (position)
            {case 0:
                btn[0] = (FloatingTextButton) findViewById(R.id.btn);
                btn[0].setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        try {
                            double thick = Double.valueOf(editText1.getText().toString());
                            double width = Double.valueOf(editText2.getText().toString());
                    }
                });
                break;
                case 1:
                    btn[0] = (FloatingTextButton) findViewById(R.id.btn);
                    btn[0].setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            try {

                            } catch (NumberFormatException e) {
                                //not a double
                            }
                        }
                    });
                    break;
            }
        }
    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
    }
});
Wael Youssef
  • 49
  • 1
  • 10

3 Answers3

1

this is the most painless way i can think of: make a new class or a static member of an existing class but the second solution makes the code less understandable.

public class SpinnerPosHolder{
    public static int poition;
}

then in all 4 of them:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
SpinnerPosHolder.position = spinner.getSelectedItemPosition();

For using it:

spinner.setSelection(SpinnerPosHolder.position)
M.Mavini
  • 589
  • 8
  • 20
1

First save the position of selected data from spinner in a String variable,

int positionOfSelectedDataFromSpinner;

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       positionOfSelectedDataFromSpinner= position;
}

Then on button click send intent to Another activity with putExtra

Intent i = new Intent (this, activity2.class);

i.putExtra("position", positionOfSelectedDataFromSpinner);
startActivity(i);

get int from getIntent in another activity

Intent intent = getIntent();
int positionToShowToSpinner = intent.getStringExtra("position");

then set the position to spinner

spinner.setSelection(positionToShowToSpinner);

I think this my solve your problem.

UltimateDevil
  • 2,807
  • 2
  • 18
  • 31
  • You can putExtra as integer. https://stackoverflow.com/questions/7074097/how-to-pass-integer-from-one-activity-to-another – M.Mavini Sep 17 '17 at 17:44
  • thanks i did with your code like this : 1-finalint[]positionOfSelectedDataFromSpinner = new int[1]; 2- positionOfSelectedDataFromSpinner[0] = position; and get it in second activity 3- Intent intent = getIntent(); int positionToShowToSpinner = intent.getIntExtra("position",0); 4- spinner.setSelection(positionToShowToSpinner); – Wael Youssef Sep 21 '17 at 07:36
0

You can do it in this way

String selectedItem=spinner1.getSelectedItem().toString();

And that String you can pass using Intent

Sunil P
  • 3,698
  • 3
  • 13
  • 20
  • You can putExtra as integer. https://stackoverflow.com/questions/7074097/how-to-pass-integer-from-one-activity-to-another – M.Mavini Sep 17 '17 at 17:44