Integer.toString()更改int的值

时间:2016-11-29 08:58:18

标签: java android integer kumulos

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

上方:field2获得价值' 9'和UName显示价值' 9'在textfield上如预期的那样。

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

现在,当我使用paramage调用方法时,我得不到任何结果(意味着比较方法中的ID导致错误)。

但是,如果我现在这样做并调用方法,它可以完美地工作(但是我不能为方法提供静态输出,应该从用户那里获取)

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

问题是什么?如何解决?

顺便说一下我在kumulos上调用一个方法,我正在为android编程。

编辑:我提供的确切代码。

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

            }
        });
    }

}

注册后的代码按钮声明未经过测试和编辑,请勿查看。

1 个答案:

答案 0 :(得分:0)

这一切都在发生,因为您的webservice响应将在您的代码执行后出现,用于paramage Hashmap输入过程。当您添加静态值时,它正在工作,所以在这种情况下,您必须在webservice响应之后添加数据,如下所示。

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