Java 文本框值未根据微调器值的更改进行正确更新(我在stackoverflow中尝试了大多数方法)
MainActivity中有一个微调器,fragment中有一个文本框。我的任务是根据最近选择的微调器值更改文本框的值。我的代码运行良好,但文本框仅在两次滑动后才更改其状态。我不知道为什么会这样。请在表决前帮我一把,请说出一些解决办法 这是我在主要活动中的代码Java 文本框值未根据微调器值的更改进行正确更新(我在stackoverflow中尝试了大多数方法),java,android,android-spinner,Java,Android,Android Spinner,MainActivity中有一个微调器,fragment中有一个文本框。我的任务是根据最近选择的微调器值更改文本框的值。我的代码运行良好,但文本框仅在两次滑动后才更改其状态。我不知道为什么会这样。请在表决前帮我一把,请说出一些解决办法 这是我在主要活动中的代码 spinner = (Spinner)findViewById(R.id.spinner2); final ArrayAdapter<String> dataAdapter1 = new ArrayAdapte
spinner = (Spinner)findViewById(R.id.spinner2);
final ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,array2);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter1);
spinner.invalidate();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//dataAdapter1.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),"well",Toast.LENGTH_LONG).show();
String pos = (String) spinner.getSelectedItem();
SharedPreferences sharedPref = getSharedPreferences("Mode", Activity.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoicemode", pos);
prefEditor.commit();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
我的片段代码:
final TextView modeselect = (TextView) view.findViewById(R.id.pass);
final SharedPreferences sharedPref = this.getActivity().getSharedPreferences("Mode", Activity.MODE_PRIVATE);
String get = sharedPref.getString("userChoicemode",selected);
modeselect.setText("" + get);
在你的片段中
只需将此重写方法添加到片段中,并将所选微调器项的值放入此方法中片段的文本视图中。你的膨胀视图应该在spinner上全局声明。setOnItemSelectedListener在TextView中重写此问题重复-@shshwat没有答案。我已经尝试了所有这些方法duplicate@dipali请添加您的评论作为答案,代码可能重复,但还有一个问题兄弟…它只在刷卡时更改。我想要要在更改微调器值后立即更改文本框值,只需将设置文本视图值的代码放入方法中,并在选中项时从活动调用该方法ExampleFragment fragment=ExampleFragment getFragmentManager.findFragmentByIdR.id.example\u fragment;碎片
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
try {
if (isVisibleToUser) {
final TextView modeselect = (TextView) view.findViewById(R.id.pass);
final SharedPreferences sharedPref = this.getActivity().getSharedPreferences("Mode", Activity.MODE_PRIVATE);
String get = sharedPref.getString("userChoicemode",selected);
modeselect.setText("" + get);
}
} catch (Exception e) {
e.printStackTrace();
}
super.setUserVisibleHint(isVisibleToUser);
}