如何获取Hashmap值并将其传递给齐射的ResponseListener

时间:2016-10-06 13:53:31

标签: android hashmap android-volley

如何将Hashmap正确传递给responseListener?对不起,我是android新手请指导我。我有一个会话管理器,它存储用户的数据,以便用户可能不需要再次登录,但我很难检索它并将其应用于我的Volley响应监听器。我想检索user_id,因为我需要它作为我的php文件中的WHERE。我认为这很简单我只是不知道如何应用或转换hashmap值。请帮助我谢谢

这是我的代码: 主要活动

public class SendToDatabase extends AppCompatActivity {
 RatingBar ratingBar;
TextView txtRatingValue;
SessionManager session;
//Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send_to_database);
//HERE IS THE SESSIONMANAGER CODE
    session = new SessionManager(getApplicationContext());
    HashMap<String, Integer> user_int = session.getUserLexileNID();

     final Integer user_id = user_int.get(SessionManager.KEY_USER_ID);

    final EditText etComment = (EditText) findViewById(R.id.etComment);
    final Button bSubmit = (Button) findViewById(R.id.bSubmit);
    final RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
    addListenerOnRatingBar();
   // addListenerOnButton();

    bSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String comment = etComment.getText().toString();
           // final int rate = ratingBar.getNumStars();
            final String rate = txtRatingValue.getText().toString();

            Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if (success){
                            Toast.makeText(SendToDatabase.this,"success"+comment,Toast.LENGTH_SHORT).show();
                            //Intent intent = new Intent (SendToDatabase.this, CommentArea.class);
                           // SendToDatabase.this.startActivity(intent);
                        } else {
                           // Toast.makeText(SendToDatabase.this,"something went wrong try again later", Toast.LENGTH_SHORT).show();
                           // Toast.makeText(SendToDatabase.this, "There might be some problem in the server side, please try again later", Toast.LENGTH_SHORT).show();
                           // Toast.makeText(SendToDatabase.this, response.toString(), Toast.LENGTH_SHORT).show();

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };

//这里我需要通过我的USER_ID

            CommentRateRequest commentRateRequest = new CommentRateRequest(comment, rate, user_id, responseListener);
            RequestQueue queue = Volley.newRequestQueue(SendToDatabase.this);
            queue.add(commentRateRequest);
        }
    });

}
public void addListenerOnRatingBar() {

    ratingBar = (RatingBar) findViewById(R.id.ratingBar);
    txtRatingValue = (TextView) findViewById(R.id.tvRatingValue);

   // if rating value is changed,
    //display the current rating value in the result (textview) automatically
    ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating,
                                    boolean fromUser) {

            txtRatingValue.setText(String.valueOf(rating));

        }
    });
}}

CommentRateRequest其他人将此称为单身类

public CommentRateRequest (String comment, String rate,int user_id Response.Listener<String>listener){
    super(Method.POST, CommentRateUrl, listener, null);
    params = new HashMap<>();
    params.put("comment", comment);
    params.put("rate", rate);
   params.put("user_id", user_id+"");
   // params.put("book_id", book_id+"");
}

我的SessionManager中的代码:

public void createLoginSession(String Fname, String Lname, Integer user_id, Integer lexile, String role_name, String grade_name, String password){
    editor.putBoolean(IS_LOGIN, true);
    editor.putString(KEY_FNAME, Fname);
    editor.putString(KEY_LNAME, Lname);
    editor.putInt(KEY_USER_ID, user_id);
    editor.putInt(KEY_LEXILE, lexile);
    editor.putString(KEY_ROLE_NAME, role_name);
    editor.putString(KEY_GRADE_NAME, grade_name);
    editor.putString(KEY_PASSWORD, password);

    editor.commit();
}


//Part of the tutorial dont seem to have any problem
public HashMap<String, String> getUserDetails(){
    HashMap<String, String> user = new HashMap<String, String>();
    user.put(KEY_FNAME, pref.getString(KEY_FNAME, null));
    user.put(KEY_LNAME, pref.getString(KEY_LNAME, null));
    user.put(KEY_ROLE_NAME, pref.getString(KEY_ROLE_NAME, null));
    user.put(KEY_GRADE_NAME, pref.getString(KEY_GRADE_NAME, null));
    user.put(KEY_PASSWORD, pref.getString(KEY_PASSWORD, null));
    return user;
}

1 个答案:

答案 0 :(得分:0)

不要注意这一点,因为我忘记添加&#34;,&#34;谢谢。 ..