简体   繁体   中英

hashmap boolean value is not being updated

I create a HashMap<String, Boolean> when my activity is created...the hashmap contains:

{email=false, username=false, password=false, firmcode=false}

after the successful response I get from Retrofit call inside onResponse callback I update the value of password key of hashmap to true

inputValidityMap.put(getResources().getString(R.string.password_tag), true);

the problem is that when I log the hashmap the value of password key has not been updated

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());

I think the problem is that

 **Log.d(debugTag, inputValidityMap.toString());**

Is outside of the onResponse or onFailure events. onResponse and onFailure are going to execute asynchronously and will probably not execute before your reach the last line of your code which is the log line.

Try to add the exactly same line of log just after your modify the hash map on the onResponse method. You should see first the log on the last line where the value is still false and later you will see the value chasing to true that is after the execution of the onResponse method.

finally i found out what was wrong...@user1880062 was absolute wright...the onResponse callback is executed asynchronously..so i have made a simple way around..i create an interface that when onResponse is executed i call its method with the desired boolean value i want to update tha value of hashmap...so i get the value outside of the callcack instanstiating the interface which contains tha method with the boolean value i have changed in the callback...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM