簡體   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));

上圖:field2獲得值“ 9”,UName按預期在文本字段上顯示值“ 9”。

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

現在,當我使用參數調用方法時,我沒有任何結果(方法中ID的比較結果為false)。

但是,如果我現在執行此操作並調用方法,則它可以正常工作(但是我無法向方法提供靜態輸出,應從用戶那里獲取)

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

            }
        });
    }

}

注冊按鈕聲明后的代碼未經過測試和編輯,因此請不要看那里。

這一切都在發生,因為您的Web服務響應稍后出現,然后您將執行參數Hashmap輸入過程的代碼。 當您添加靜態值時,它就起作用了,因此在這種情況下,您必須在如下所示的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