2016-11-29 41 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線程上與其他後臺線程一起做其他一些工作,這會導致不匹配。 –