我已经开发 android 一段时间了,之前从未遇到过这个问题,所以我希望这里有人能提供帮助。我有多个 EditText 和 autocompleTextView,它们从 sharedprefs 中填充自己。问题是视图没有显示它们应该显示的文本。它们显示在那里的旧值,直到我离开活动并回来。奇怪的是,如果我在 editText 上调用 getText 函数时显示错误的值,getText() 会返回正确的值。有人可以解释为什么会这样。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
view = inflater.inflate(R.layout.extra_info, container, false);
myPrefs = PreferenceManager
.getDefaultSharedPreferences(getActivity());
initPrefs();
findTextViews();
findViews();
initDataSource();
runQueries();
getAllFields();
return view;
}
private void findViews() {
EditPayID = (AutoCompleteTextView)view.findViewById( R.id.EditpayID );
EditPayID.setText(PayIDPref);
Log.e("findviews",EditPayID.getText().toString());
//^^ correct value but screen still displays wrong value
}
private void initPrefs(){
editor = myPrefs.edit();
PayIDPref = myPrefs.getString("PayID", "");
Log.e("payidPref", PayIDPref);
}
我认为以下代码与问题无关,但也许我遗漏了一些东西,这是与 payID 关联的所有代码
EditPayID.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
EditPayID.showDropDown();
EditPayID.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String selected = EditPayID.getAdapter().getItem(arg2).toString();
EditPayID.setText(selected);
editor.putString("PayID" ,selected);
editor.apply();
}
});
return false;
}
});
EditPayID.setValidator(new android.widget.AutoCompleteTextView.Validator() {
@Override
public boolean isValid(CharSequence text) {
Log.e("in pay isValid" , "validating");
Collections.sort(validPays);
if (Collections.binarySearch(validPays, text.toString()) >= 0){
return true;
}
return false;
}
@Override
public CharSequence fixText(CharSequence invalidText) {
EditPayID.setError("You have Entered invalid PayID");
editor.putString("PayID" ,"Invalid PayID");
editor.apply();
return "Invalid PayID";
}
});