2015-09-07 49 views
-2

我正在尝试执行更新配置文件,从EditProfile.java中获取我的值,并在另一个名为ConfirmEdit.java的页面上显示它以在用户按下确认按钮时更新数据库中的登录记录。我使用intent putExtra将值从一个类传递到另一个类,但在这里没有做到。如何从EditText中获取值以传入另一个类中的asyntask?

作为我的代码,我传递了三个参数,即用户名,名称和手机。但是,当我在ConfirmEdit.java上打印时,只打印了用户名。

请帮助谢谢!

的代码如下所示:

EditProfile.java

public class EditProfile extends Activity { 

    Button backbtn, updatebtn; 
    String user, name_entered, username_returned, registered_name; 
    String handphone_no; 
    String updated_username; 
    EditText username, password_entered, name, handphone; 
    JSONParser jsonParser = new JSONParser(); 
    private static final String LOGIN_URL = url; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.editprofile); 

     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      user = extras.getString("username"); 
     } 

     username = (EditText) findViewById(R.id.username_signup); 
     password_entered = (EditText) findViewById(R.id.password); 
     name = (EditText) findViewById(R.id.name); 
     handphone = (EditText) findViewById(R.id.handphone); 
     backbtn = (Button) findViewById(R.id.back); 
     updatebtn = (Button) findViewById(R.id.updatebtn); 


     new AttemptLogin().execute(user); 

     updatebtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       registered_name = name.getText().toString(); 
       Intent i = new Intent(EditProfile.this, ConfirmEdit.class); 
       i.putExtra("registered_name", registered_name); 
       i.putExtra("user", user); 
       i.putExtra("password", password_entered.getText().toString()); 
       i.putExtra("handphone", handphone.getText().toString()); 
       startActivity(i); 
      } 
      }); 
       /*updated_username = name.getText().toString(); 
       new updateProfile().execute(updated_username, user, handphone.getText().toString() ,password_entered.getText().toString()); 
      } 
     });*/ 

     backbtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent i = new Intent(EditProfile.this, AccountPage.class); 
       startActivity(i); 
      } 
     }); 

    } 




    class AttemptLogin extends AsyncTask<String, String, JSONObject> { 
     protected JSONObject doInBackground(String... args) { 
      // TODO Auto-generated method stub 
      // here Check for success tag 
      try { 
       HashMap<String, String> params = new HashMap<>(); 
       params.put("user", args[0]); 
       JSONObject json = jsonParser.makeHttpRequest(
         LOGIN_URL, "POST", params); 

       if (json != null) { 

        Log.d("JSON result", json.toString()); 

        return json; 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     protected void onPostExecute(JSONObject json) { 

      if (json != null) { 
       Toast.makeText(EditProfile.this, json.toString(), 
         Toast.LENGTH_LONG).show(); 

       try { 
        username_returned = json.getString("username"); 
        handphone_no = json.getString("handphone"); 
        name_entered = json.getString("name"); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

       String messageString = username_returned; 
       username.setText(messageString); 
       String messageString2 = handphone_no; 
       handphone.setText(messageString2); 
       String messageString3 = name_entered; 
       name.setText(messageString3); 
      } 
     } 
    } 

} 

ConfirmEdit.java

public class ConfirmEdit extends Activity { 

    String name, handphone, username, password; 
    JSONParser jsonParser = new JSONParser(); 
    private static final String UPDATE_PROFILE = url; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.confirmedit); 

     Button updatebtn = (Button) findViewById(R.id.cfmupdatebtn); 

     TextView t = (TextView) findViewById(R.id.textView10); 
     TextView t1 = (TextView) findViewById(R.id.textView11); 
     TextView t2 = (TextView) findViewById(R.id.textView12); 

     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      name = extras.getString("registered_name"); 
      username = extras.getString("user"); 
      password = extras.getString("password"); 
      handphone = extras.getString("handphone"); 
     } 

     String messageString = name; 
     t.setText(messageString); 
     String messageString1 = username; 
     t1.setText(messageString1); 
     String messageString2 = handphone; 
     t2.setText(messageString2); 



     updatebtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       new updateProfile().execute(name,username,handphone); 
       System.out.println(name); 
       System.out.println(username); 
       System.out.println(handphone); 

      } 
     }); 
    } 
     class updateProfile extends AsyncTask<String, String, JSONObject> { 
      private static final String TAG_SUCCESS = "success"; 
      private static final String TAG_MESSAGE = "message"; 

      protected JSONObject doInBackground(String... args) { 
       // TODO Auto-generated method stub 
       // here Check for success tag 
       try { 
        HashMap<String, String> params = new HashMap<>(); 
        params.put("name", args[0]); 

        params.put("username", args[1]); 
        params.put("handphone", args[2]); 
        JSONObject json = jsonParser.makeHttpRequest(
          UPDATE_PROFILE, "POST", params); 

        if (json != null) { 

         Log.d("JSON result", json.toString()); 

         return json; 
        } 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       return null; 
      } 

      protected void onPostExecute(JSONObject json) { 
       int success = 0; 
       String message = ""; 

       if (json != null) { 
        Toast.makeText(ConfirmEdit.this, json.toString(), 
          Toast.LENGTH_LONG).show(); 

        try { 
         success = json.getInt(TAG_SUCCESS); 
         message = json.getString(TAG_MESSAGE); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        if (success == 1) { 
         Log.d("Acknowledgement!", message); 
        } 

       } 
      } 
     } 
    } 
+0

如何在您的Asynktask中创建自己的构造函数。检查我的回答 –

+0

可能应用程序崩溃。吐司是否显示? –

+0

@ρяσѕρєяK应用程序不会崩溃 –

回答

1

好,具有:

class AttemptLogin extends AsyncTask<String, String, JSONObject> { 
    protected JSONObject doInBackground(String... args) { 
     // TODO Auto-generated method stub 
     // here Check for success tag 
     try { 
      HashMap<String, String> params = new HashMap<>(); 
      params.put("user", args[0]); 
      JSONObject json = jsonParser.makeHttpRequest(
        LOGIN_URL, "POST", params); 

      if (json != null) { 

       Log.d("JSON result", json.toString()); 

       return json; 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    protected void onPostExecute(JSONObject json) { 

     if (json != null) { 
      Toast.makeText(EditProfile.this, json.toString(), 
        Toast.LENGTH_LONG).show(); 

      try { 
       username_returned = json.getString("username"); 
       handphone_no = json.getString("handphone"); 
       name_entered = json.getString("name"); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      String messageString = username_returned; 
      username.setText(messageString); 
      String messageString2 = handphone_no; 
      handphone.setText(messageString2); 
      String messageString3 = name_entered; 
      name.setText(messageString3); 
     } 
    } 
} 

你可以添加一些自定义和自己的构造函数,如这样的:

class AttemptLogin extends AsyncTask<String, String, JSONObject> { 
String username, password, handphone, name; 

public AttemptLogin(String _username, String _password, String _handphone, String _name){ 

    this.username = _username; 
    this.password = _password; 
    this.handphone = _handphone; 
    this.name = _name; 

} 
protected JSONObject doInBackground(String... args) { 
     // TODO Auto-generated method stub 
     // here Check for success tag 
     try { 
      HashMap<String, String> params = new HashMap<>(); 
      params.put("user", args[0]); 
      JSONObject json = jsonParser.makeHttpRequest(
        LOGIN_URL, "POST", params); 

      if (json != null) { 

       Log.d("JSON result", json.toString()); 

       return json; 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    protected void onPostExecute(JSONObject json) { 

     if (json != null) { 
      Toast.makeText(EditProfile.this, json.toString(), 
        Toast.LENGTH_LONG).show(); 

      try { 
       username_returned = json.getString("username"); 
       handphone_no = json.getString("handphone"); 
       name_entered = json.getString("name"); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      String messageString = username_returned; 
      username.setText(messageString); 
      String messageString2 = handphone_no; 
      handphone.setText(messageString2); 
      String messageString3 = name_entered; 
      name.setText(messageString3); 
     } 
    } 
} 

,那么你应该改变这一行:

new AttemptLogin().execute(user); 

这样:

new AttemptLogin(username, password, handphone, name).execute(); 

,这是处理一个最好的办法。

希望有所帮助。问候

编辑 如果你想发送的TextView/EditText上或一些控制,你只需要改变你的构造,因为这样:

EditText username, password, handphone, name; 

public AttemptLogin(EditText _username, EditText _password, EditText _handphone, EditText _name){ 

    this.username = _username; 
    this.password = _password; 
    this.handphone = _handphone; 
    this.name = _name; 

} 

很显然,当你处理你的数据,你提取字符串,所以我认为是同样的事情。获取EditText/TextView的字符串是:

yourEditText.getText().toString(); 
+0

但我想要做的是EditProfile有EditView,键入的值将被传入到ConfirmEdit,所以我可以运行UpdateProfile()来更新数据库值..我是使用putextra方法,但它不工作。我不确定你的解决方案是否能解决这个问题。 –

+0

所以改变你的构造函数,检查我的编辑 –

+0

好吧,一旦构造函数被设置,我如何从ConfirmEdit.java获取值? –

相关问题