简体   繁体   English

Integer.toString()更改int的值

[英]Integer.toString() changes value of int

private int ID=0;
.
.
Integer field2=0;
field2 = Integer.parseInt(item.get("credentialID").toString());
ID=field2.intValue();
UName.setText(Integer.toString(ID));

Above: field2 gets value '9' and UName shows value '9' on textfield as expected. 上图:field2获得值“ 9”,UName按预期在文本字段上显示值“ 9”。

HashMap<String,String> paramage= new HashMap<String, String>();
paramage.put("credential", Integer.toString(ID));

Now when i call method using paramage i get no result (means comparison of ID in method resulted false). 现在,当我使用参数调用方法时,我没有任何结果(方法中ID的比较结果为false)。

However, if I do this and call method now,it works flawlessly (but I can't provide static output to method ,it should be taken from user) 但是,如果我现在执行此操作并调用方法,则它可以正常工作(但是我无法向方法提供静态输出,应从用户那里获取)

HashMap<String,String> paramage= new HashMap<String, String>();
paramage.put("credential", "9" );

What's the problem? 有什么问题? How to Solve it? 如何解决?

Btw I am calling a method on kumulos and I am programming for android. 顺便说一句,我在kumulos上调用一个方法,并且我正在为android编程。

EDIT: My exact code as requested. 编辑:我要求的确切代码。

package lcukerd.com.logintest;

import android.os.Bundle;
import android.support.annotation.BoolRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Application;
import android.widget.Button;
import android.widget.EditText;

import com.kumulos.android.Kumulos;
import com.kumulos.android.ResponseHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class MainActivity extends AppCompatActivity {

    private int ID=0;
    private EditText UName,UPass,UAge;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Kumulos.initWithAPIKeyAndSecretKey("removed", "removed", this);
        UName =(EditText) findViewById(R.id.name);
        UPass = (EditText) findViewById(R.id.password);
        UAge =  (EditText) findViewById(R.id.age);
        Button login = (Button) findViewById(R.id.login);

        login.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {

                String username = UName.getText().toString();
                String password = UPass.getText().toString();
                LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();
                params.put("accountName", username);
                params.put("password",password);
                    Kumulos.call("login", params, new ResponseHandler() {
                        @Override
                        public void didCompleteWithResult(Object result) {

                            Integer field2=0;
                            ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String,Object>>) result;
                            LinkedHashMap<String, Object> item= objects.get(0);
                            field2 = Integer.parseInt(item.get("credentialID").toString());
                            ID=field2.intValue();
                            UName.setText(Integer.toString(ID));
                        }
                    });

                params.clear();
                HashMap<String,String> paramage= new HashMap<String, String>();
                paramage.put("credential",  Integer.toString(ID));     //up here
                    Kumulos.call("getage", paramage, new ResponseHandler() {
                        @Override
                        public void didCompleteWithResult(Object result) {
                            Integer field2=0;
                            ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String,Object>>) result;
                            LinkedHashMap<String, Object> item= objects.get(0);
                            //Boolean check = item.containsKey("age");
                            field2 = Integer.parseInt(item.get("age").toString());
                            int age=field2.intValue();
                            UAge.setText(Integer.toString(age));
                        }
                    });
                }
        });
        Button signup = (Button) findViewById(R.id.signup);
        signup.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {

                String username = UName.getText().toString();
                String password = UPass.getText().toString();
                HashMap<String, String> params = new HashMap<String, String>();
                params.put(username, password);
                try {
                    Kumulos.call("signup", params, new ResponseHandler() {
                        @Override
                        public void didCompleteWithResult(Object result) {

                            ID = (int) result;
                        }
                    });
                    UName.setText("");
                    UPass.setText("");
                    params.put(UAge.getText().toString(), Integer.toString(ID));
                    Kumulos.call("setAge", params, new ResponseHandler() {
                        @Override
                        public void didCompleteWithResult(Object result) {


                        }
                    });
                    UAge.setText(Integer.toString(ID));

                }
                catch (Exception e)
                {
                    UAge.setText("5");
                    e.printStackTrace();
                }

            }
        });
    }

}

Code after signup Button declaration is not tested and edited dont look there. 注册按钮声明后的代码未经过测试和编辑,因此请不要看那里。

This all is happening because your webservice response is coming later then your code execution for paramage Hashmap input process. 这一切都在发生,因为您的Web服务响应稍后出现,然后您将执行参数Hashmap输入过程的代码。 When you are adding static value then it is working so in this case you have to add data after webservice response like below. 当您添加静态值时,它就起作用了,因此在这种情况下,您必须在如下所示的Web服务响应之后添加数据。

HashMap<String,String> paramage= new HashMap<String, String>();

Kumulos.call("login", params, new ResponseHandler() {
            @Override
            public void didCompleteWithResult(Object result) {

                Integer field2 = 0;
                ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String, Object>>) result;
                LinkedHashMap<String, Object> item = objects.get(0);
                field2 = Integer.parseInt(item.get("credentialID").toString());
                ID = field2.intValue();
                UName.setText(Integer.toString(ID));

                paramage.put("credential", Integer.toString(ID));     //up here

                //As you are making web service call for "getage" so I put this code here. You can make a callback to write this code outside for "getage" webservice call.
                Kumulos.call("getage", paramage, new ResponseHandler() {
                    @Override
                    public void didCompleteWithResult(Object result) {
                        Integer field2 = 0;
                        ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String, Object>>) result;
                        LinkedHashMap<String, Object> item = objects.get(0);
                        //Boolean check = item.containsKey("age");
                        field2 = Integer.parseInt(item.get("age").toString());
                        int age = field2.intValue();
                        UAge.setText(Integer.toString(age));
                    }
                });
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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