Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

java - How to dynamically change the text box value according to the recently selected spinner value?

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...