我创建活动时创建HashMap<String, Boolean>
... hashmap包含:
{email=false, username=false, password=false, firmcode=false}
成功响应后,我从onResponse
回调中的Retrofit调用中获取我将hashmap的密码键值更新为true
inputValidityMap.put(getResources().getString(R.string.password_tag), true);
问题是当我记录hashmap时,密码密钥的值还没有更新
if (connectivityObserver.getConnectivityStatus(getActivity()) != AppConfig.NO_CONNECTION) {
Call<UserConnectionStaff> call = apiService.isPasswordValid(getResources().getString(R.string.passwordValidation), registerPasswordEdt.getText().toString());
call.enqueue(new Callback<UserConnectionStaff>() {
@Override
public void onResponse(Response<UserConnectionStaff> response, Retrofit retrofit) {
int resource_message = inputValidationCodes.get(response.body().getError_code());
if (isAdded()) {
if (response.body().getError_code() != AppConfig.INPUT_OK) {
if (response.body().getError_code() == AppConfig.ERROR_INVALID_INPUT) {
passwordErrorTtv.setText(commonElements.decodeUtf8(commonElements.encodeUtf8(getResources().getString(resource_message))));
} else
passwordErrorTtv.setText(commonElements.decodeUtf8(commonElements.encodeUtf8(getResources().getString(resource_message))));
}
} else {
animationStaff(acceptPasswordRlt, 0.0f, 1.0f, "visible");
animationStaff(showHidePasswordTtv, 1.0f, 0.0f, "gone");
Log.d(debugTag, inputValidityMap.containsKey(getResources().getString(R.string.password_tag)) + "");
inputValidityMap.put(getResources().getString(R.string.password_tag), true);
}
}
}
@Override
public void onFailure(Throwable t) {
if (isAdded()) {
if (t instanceof IOException) {
setToastHelperMsg(getResources().getString(R.string.unavailable_service));
} else {
setToastHelperMsg(getResources().getString(R.string.error_occured));
}
registerUsernameEdt.setText(null);
progressViewActions("stop", usernameProgressView);
}
}
});
} else {
if ( isAdded() )
setToastHelperMsg(getResources().getString(R.string.no_connection));
}
Log.d(debugTag, inputValidityMap.toString());
答案 0 :(得分:2)
我认为问题在于
**Log.d(debugTag, inputValidityMap.toString());**
在onResponse或onFailure事件之外。 onResponse和onFailure将以异步方式执行,并且可能在您到达代码的最后一行(即日志行)之前不会执行。
在onResponse方法上修改哈希映射后,尝试添加完全相同的日志行。您应首先看到值仍为false的最后一行上的日志,稍后您将看到执行onResponse方法后追逐到true的值。
答案 1 :(得分:0)
最后我发现了什么是错的... @ user1880062是绝对的怀疑...... onResponse回调是异步执行的...所以我已经做了一个简单的方法..我创建了一个接口,当onResponse被执行时我打电话它的方法具有所需的布尔值我想更新hashmap的值...所以我得到了callcack之外的值,实现了包含tha方法的接口,我在回调中更改了布尔值...
if (connectivityObserver.getConnectivityStatus(getActivity()) != AppConfig.NO_CONNECTION) {
Call<UserConnectionStaff> call = apiService.isPasswordValid(getResources().getString(R.string.passwordValidation), registerPasswordEdt.getText().toString());
call.enqueue(new Callback<UserConnectionStaff>() {
@Override
public void onResponse(Response<UserConnectionStaff> response, Retrofit retrofit) {
int resource_message = inputValidationCodes.get(response.body().getError_code());
if (isAdded()) {
if (response.body().getError_code() != AppConfig.INPUT_OK) {
if (response.body().getError_code() == AppConfig.ERROR_INVALID_INPUT) {
passwordErrorTtv.setText(commonElements.decodeUtf8(commonElements.encodeUtf8(getResources().getString(resource_message))));
} else {
passwordErrorTtv.setText(commonElements.decodeUtf8(commonElements.encodeUtf8(getResources().getString(resource_message))));
}
x.updatevalue(false);
} else {
animationStaff(acceptPasswordRlt, 0.0f, 1.0f, "visible");
animationStaff(showHidePasswordTtv, 1.0f, 0.0f, "gone");
x.updatevalue(true);
}
}
Log.d(debugTag, inputValidityMap.get("password")+"");
}
@Override
public void onFailure(Throwable t) {
if (isAdded()) {
if (t instanceof IOException) {
setToastHelperMsg(getResources().getString(R.string.unavailable_service));
} else {
setToastHelperMsg(getResources().getString(R.string.error_occured));
}
registerUsernameEdt.setText(null);
progressViewActions("stop", usernameProgressView);
}
}
});
} else {
if ( isAdded() ) setToastHelperMsg(getResources().getString(R.string.no_connection));
}
}
x = new Update() {
@Override
public void updatevalue(boolean boul) {
inputValidityMap.put(getResources().getString(R.string.password_tag), boul);
if ( !inputValidityMap.get(getResources().getString(R.string.password_tag)) ) registerPasswordEdt.getChildAt(1).setVisibility(View.GONE);
}
};