I want to pass Edittext's values inside doInBackground AsyncTask, I tride this code:
protected void onCreate(Bundle savedInstanceState) {
ID = (EditText) findViewById(R.id.ID);
lname = (EditText) findViewById(R.id.lname);
fname = (EditText) findViewById(R.id.fname);
phone = (EditText) findViewById(R.id.phone);
H = ID.getText().toString();
Q = lname.getText().toString();
C = fname.getText().toString();
Ls = phone.getText().toString();
addbtn = (Button) findViewById(R.id.addbtn);
addbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new CreateNewexercise().execute(H,Q,C,Ls);
}
});
}
class CreateNewexercise extends AsyncTask<String, String, String> {
/**
* Creating exercise
*/
protected String doInBackground(String... args) {
//getting field's text
id=args[0];
ln=args[1];
fn=args[2];
p=args[3];
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("ID", id));
params.add(new BasicNameValuePair("lname", ln));
params.add(new BasicNameValuePair("fname",fn));
params.add(new BasicNameValuePair("phone", p));
Log.d("ID", "Value: " + id);
Log.d("lname", "Value: " + ln);
Log.d("fname", "Value: " + fn);
Log.d("phone", "Value: " + p);
I passed the parameter as arguments, but it always empty I don't know why?
can someone please tell me what I'm doing wrong?
You are loading the text from the EditText
in your onCreate
method, not on demand when the button is clicked. Remove the lines calling getText()
from outside the OnClickListener
and add them inside.
addbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
H = ID.getText().toString();
Q = lname.getText().toString();
C = fname.getText().toString();
Ls = phone.getText().toString();
new CreateNewexercise().execute(H,Q,C,Ls);
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.