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);
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
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
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) {
}
});
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) {
}
});