【问题标题】:How to get Hashmap value and pass it to ResponseListener of volley如何获取Hashmap值并将其传递给volley的ResponseListener
【发布时间】:2016-10-06 13:53:30
【问题描述】:

如何正确地将我的 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;
}

【问题讨论】:

    标签: android hashmap android-volley


    【解决方案1】:

    不要注意这个,因为我忘了加“,”谢谢。 ..

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 2021-09-01
      • 2016-02-13
      • 2018-03-22
      • 2015-02-05
      • 1970-01-01
      • 2014-02-26
      • 2011-09-23
      相关资源
      最近更新 更多