-2

Here is screenshot ...my changes in spinner value reflects only after 2 swipes I have an app with 10 screens and i have 1 spinner in my main activity and a textbox in fragment .I want to display the recently selected spinner value in my textbox .

I wrote a code to display the selected spinner value in my text box and its become stable throughout all my screens.But i don't know how to dynamically change the text box value according to the recently selected spinner value. (Before downvoting kindly suggest me some solutions)

Here is code to get the spinner value

String pos = (String) spinner.getSelectedItem();
        SharedPreferences sharedPref = getSharedPreferences("Mode", Activity.MODE_PRIVATE);
        SharedPreferences.Editor prefEditor = sharedPref.edit();
        prefEditor.putString("userChoicemode", pos);
        prefEditor.commit();

Here is the code to retrieve it

TextView modeselect = (TextView) view.findViewById(R.id.pass);

        SharedPreferences sharedPref = this.getActivity().getSharedPreferences("Mode", Activity.MODE_PRIVATE);
        String get = sharedPref.getString("userChoicemode",selected);

        modeselect.setText("" + get);

4 Answers4

0

Try this

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

                    String selectedText = parent.getItemAtPosition(position).toString()

                    // Set your TextView here
                    myTextView.setText(selectedText);
                }

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

                }
            });

And if your TextView is in different Fragment then follow this link Communication between Activity and Fragment

Kunu
  • 5,078
  • 6
  • 33
  • 61
0

Call below method

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       String text=spinner.getSelectedItem().toString();
       txtview.setText(text);
                    }
            }

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

            }
        });

this above method calls when list set to your spinner and whenever user selects any item from spinner, you can get your spinner value from button click also by calling getSelectedItem() or if u want to get text only when user selects item from spinner you have to call this above override method

Shashwat Gupta
  • 876
  • 9
  • 22
  • wait bro i ll try – Bala_Poyyamoli Sep 11 '17 at 11:15
  • It works well but one prblm. It changes its state only after 2 swipes – Bala_Poyyamoli Sep 11 '17 at 12:52
  • @Bala_Poyyamoli will u please brifly explain whose state is changed?? and on whose swipe?? – Shashwat Gupta Sep 11 '17 at 12:54
  • as i said in my ques with help of ur code whenever i change my spinner value it refelcts in textbox of further screens on swiping.But the state of textbox changes only after 2 swipes.Thats my prblm – Bala_Poyyamoli Sep 11 '17 at 13:06
  • i am not getting your point bro @Bala_Poyyamoli, will you please show up the issue with image description – Shashwat Gupta Sep 11 '17 at 13:22
  • yup bro wait a min – Bala_Poyyamoli Sep 11 '17 at 13:28
  • i have added it bro – Bala_Poyyamoli Sep 11 '17 at 13:36
  • my app contains 100 swipable screens i have 2 spinners in main activity and 2 textboxes in fragment activity. each spinner have 2 values "mode 1,mode2" and "mode 3,mode4". i have to pass the value of spinners to my text boxes.Your code helps me out to change textbox value according to the current value in spinner.but it changes only after 2 swipes."its not updating concurrently".........thats my problem – Bala_Poyyamoli Sep 11 '17 at 13:43
  • fragment having override method setUserVisiblehint it calls when a fragment visible to user call ur function in that method, Or make static variable in ur activity and pass it in your fragment by Activity.staticvariable – Shashwat Gupta Sep 11 '17 at 13:53
  • @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { //put your function here to set Text in fragment } } – Shashwat Gupta Sep 11 '17 at 14:01
0

You can call spinner's setOnItemSelectedListener

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

                 modeselect.setText(spinner.getSelectedItem().toString());
                 //or 
                 //modeselect.setText(adapterView.getItemAtPosition(i));

                }

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

                }
            });
Upendra Shah
  • 2,218
  • 17
  • 27
0
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

             textView.setText(spinner.getSelectedItem().toString());


            }

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

            }
        });
Arjun Solanki
  • 236
  • 1
  • 11