2016-11-29 9 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"); 

問題は何ですか?それを解決するには?

Btw私はkumulosのメソッドを呼び出しています。私はAndroidのためにプログラミングしています。

EDIT:要求通りの正確なコード。

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

「credential'と 'credentialID'を確認してください。そのキーで同じデータセットにアクセスしていますか? –

+1

あなたは 'item.get(" credentialID ")。toString()'を 'String'としてすでに持っています。なぜそれを整数に変換して戻ってきたのですか? – EJP

+0

文字列をIntegerに変換してStringに変換する方法がちょっと変わっています。それは必要ですか ?さらに、[mcve]を提供してください。 – AxelH

答えて

0

これはすべて、あなたのWebサービスの応答が、その後、パラメータのハッシュマップ入力プロセスのコード実行から来ているために起こります。静的な値を追加しているときは動作していますので、このような場合は以下のような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)); 
        } 
       }); 
      } 
     }); 
+0

ありがとう、それは働いた。そのような場合には、複数のスレッドを単独でアンドロイドしていませんでした。 – lcukerd

+0

はい、Webサービスコールはバックグラウンドスレッドで実行されていますが、他のバックグラウンドスレッドを持つUIスレッドで他の作業をしています。 –

関連する問題