1

Hello I want to Know that how can i keep the spinner items selected throughout the activity so that if i come back form other activity to the activity that is consisting of spinner its still remain selected as it is.

The above statement that i have used that has been solved after this there is an another issue that whenever i restart my app its the selected portion remains as it is i want to know what i can do so that the spinner does not show what i have selected before

I have a code of spinner which i want to keep it selected throughout all the activities ,

daySelection = (Spinner)findViewById(R.id.daypreferance);
        //String[] dayName = new String[]{
          //      "sunday", "monday", "tuesday" 
        //};

        MyClass[] dayName ={

                new MyClass("sunday", ""),
                new MyClass("monday", "2"),
                new MyClass("tuesday", "3")



        };
        ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(this , R.layout.spinner_items ,R.id.spinneritem,dayName);
        adapter.setDropDownViewResource(R.layout.spinner_items);
        hotelSelection.setAdapter(adapter);

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

                preferDay=  parent.getItemAtPosition(position).toString();


            }

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

            }
        });
public void onPause() {
        super.onPause();
        daySelection = (Spinner)findViewById(R.id.daypreferance);


        SharedPreferences prefs = getSharedPreferences("prefs_name", Context.MODE_PRIVATE);

        prefs.edit().putInt("spinner_indx", daySelection.getSelectedItemPosition()).apply();

    }
 public void onResume() {
        super.onResume();
        daySelection = (Spinner)findViewById(R.id.daypreferance);


        SharedPreferences prefs = getSharedPreferences("prefs_name", Context.MODE_PRIVATE);

        int spinnerIndx = prefs.getInt("spinner_indx", 0);


        daySelection.setSelection(spinnerIndx);

    }

I need to add the code that whenever I restart my app its the selected portion remains as it is I want to know what I can do so that the spinner does not show what I have selected before

Moulick
  • 45
  • 5
  • You can take one static variable in activity and set it with selected position and on same activity in onResume method set spinner selection by that variable – Vickyexpert Aug 06 '16 at 08:59
  • @Vickyexpert why static? – Shaishav Aug 06 '16 at 09:03
  • @Moulick don't finish the current activity having spinner just go to next activity when u have done with that just finish the activity, automatically previous activity will be resumed and your selection will not lost. – Malik Rizwan Aug 06 '16 at 09:07
  • but how to do that usually after filling the data i will go to next activity to show its summary and if i think i need to go back i will come back to this activity(view) and it should show whatever i selected before – Moulick Aug 06 '16 at 09:47

3 Answers3

0

try dis and let me know if any error....

daySelection = (Spinner)findViewById(R.id.daypreferance);
            //String[] dayName = new String[]{
              //      "sunday", "monday", "tuesday" 
            //};

            MyClass[] dayName ={

                    new MyClass("sunday", ""),
                    new MyClass("monday", "2"),
                    new MyClass("tuesday", "3")



            };
            ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(this , R.layout.spinner_items ,R.id.spinneritem,dayName);
            adapter.setDropDownViewResource(R.layout.spinner_items);
            hotelSelection.setAdapter(adapter);

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

    //check if preferDay has some valur rhen set on spinner
                     if(preferDay!=null)
    {
                     daySelection.setSelection(Integer.parseInt(preferDay));
    }


                }

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

                }
            });
Manish
  • 234
  • 2
  • 13
0

you can save state of activity and then set those values after came back

check this stack over flow question and this one

Community
  • 1
  • 1
Hoven
  • 563
  • 1
  • 5
  • 24
0

Save the state of the spinner in the onPause() method.

@Override
public void onPause() {
   super.onPause();

   Spinner spinner = (Spinner)findViewById(R.id.daypreferance);

   SharedPreferences prefs = getSharedPreferences("prefs_name", Context.MODE_PRIVATE);
   prefs.edit().putInt("spinner_indx", spinner.getSelectedItemPosition()).apply();
}

Restore the state of the spinner in the onResume() method.

@Override
public void onResume() {
   super.onResume();

   Spinner spinner = (Spinner)findViewById(R.id.daypreferance);

   SharedPreferences prefs = getSharedPreferences("prefs_name", Context.MODE_PRIVATE);
   int spinnerIndx = prefs.getInt("spinner_indx", 0);
   spinner.setSelection(spinnerIndx);
}
Pierfrancesco Soffritti
  • 1,678
  • 1
  • 18
  • 22
  • just copy-paste my code and replace "spinner" with your actual spinner :) – Pierfrancesco Soffritti Aug 06 '16 at 09:54
  • no that i understand but these 2 methods onPause and onResume how can i place it inside that code because i have 3 more spinner content also – Moulick Aug 06 '16 at 09:57
  • Add this methods (onPause and onResume) to your Activity. You can add how many spinners you want, with the same principle. – Pierfrancesco Soffritti Aug 06 '16 at 09:59
  • its showing error on set selection as "cannot resolve setSelection(int)" – Moulick Aug 06 '16 at 10:03
  • still the same issue – Moulick Aug 06 '16 at 10:13
  • hey hey just one issue it worked on the first one but after rerunning the app the selected item didnt changed and on applying on other spinners it wasnt working – Moulick Aug 06 '16 at 10:41
  • I answered your question, now I don't understand what you're talking about. Just add another int preference for each Spinner. – Pierfrancesco Soffritti Aug 06 '16 at 10:44
  • i was saying on re running the app again if i have selected monday its always showing monday other then that it should show sunday – Moulick Aug 06 '16 at 10:48
  • Just reset the preference in the onStop method. And study some good theory ;) here: https://developer.android.com/training/basics/activity-lifecycle/index.html and here: https://developer.android.com/training/basics/data-storage/shared-preferences.html – Pierfrancesco Soffritti Aug 06 '16 at 10:51
  • i think i need onStop code for this how i should onStop method so that when ever i am rerunning the app the the spinner items remain unselected – Moulick Aug 08 '16 at 05:57