2016-11-29 71 views
0
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'。Integer.toString()更改int的值

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

现在,当我调用使用paramage的方法时,我没有得到任何结果(意味着方法中的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(); 
       } 

      } 
     }); 
    } 

} 

注册后的代码按钮声明没有测试和编辑不看那里。

+0

检查你的钥匙..凭证和凭证ID。您是否使用该键访问相同的数据集? –

+1

您已将'item.get(“credentialID”)。toString()'作为'String'。你为什么要把它转换成一个整数,然后再返回? – EJP

+0

我发现有点奇怪,你如何获得一个字符串转换为整数重新将其转换为字符串。有必要吗 ?另外,请提供[mcve]。 – AxelH

回答

0

这一切都是因为你的web服务响应迟来了,然后你的代码执行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)); 
        } 
       }); 
      } 
     }); 
+0

非常感谢它的工作。在这种情况下,自己并不知道android多线程 – lcukerd

+0

是的,Webservice调用在后台线程上执行,并且您正在UI线程上与其他后台线程一起做其他一些工作,这会导致不匹配。 –