0

我在将对话框中的文本视图中的值传递到异步任务以添加到数据库时遇到问题,代码如下:

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 类?

先感谢您

4

1 回答 1

0

您可以创建全局变量,该变量从类中获取值EditText并使用该变量AsyncTask

于 2013-08-24T22:27:21.060 回答