我在将值从对话框中的textview传递到asynctask以添加到数据库时遇到问题,这里是代码:
public void addfooddialog(View v){
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.addfooddialog);
dialog.setTitle("Insert food");
dialog.setCancelable(false);
dialog.show();
Button b = (Button)dialog.findViewById(R.id.addfood);
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
new add().execute();
}
});
}
这是扩展AsyncTask的类
class add extends AsyncTask<String, String, String> {
Dialog d= new Dialog(context);
@Override
protected void onPreExecute() {
super.onPreExecute();
d.setContentView(R.layout.addfooddialog);
Log.i("","ata3 men hon");
}
@Override
protected String doInBackground(String... arg0) {
TextView title= (TextView)d.findViewById(R.id.plattername);
TextView description= (TextView) d.findViewById(R.id.description);
TextView price= (TextView)d. findViewById(R.id.price);
String foodname = title.getText().toString();
String fooddescription = description.getText().toString();
String foodprice = price.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("title", foodname));
params.add(new BasicNameValuePair("description", fooddescription));
params.add(new BasicNameValuePair("price", foodprice));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_add,
"POST", params);
final String TAG_SUCCESS = "success";
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Log.i("sucees","---------------------------------");
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
现在我知道正在重新创建对话框,这就是值返回null的原因,但是如何将值直接传递给asynctask类?
提前谢谢
答案 0 :(得分:0)
你可以创建全局变量,从EditText
获取值并使用AsyncTask
类中的变量