我正在尝试执行更新配置文件,从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);
}
}
}
}
}
如何在您的Asynktask中创建自己的构造函数。检查我的回答 –
可能应用程序崩溃。吐司是否显示? –
@ρяσѕρєяK应用程序不会崩溃 –