【问题标题】:Pass value from Textview in dialog to Asynctask将对话框中 Textview 的值传递给 Asynctask
【发布时间】:2013-08-27 17:28:02
【问题描述】:

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

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

提前谢谢你

【问题讨论】:

  • 请正确格式化您的代码并删除不相关的行,如 Logging 等。用所有不必要的代码,很难找出这里发生了什么。
  • 好的编辑...我正在添加食物标题、描述和价格,在对话框中我填写文本字段并单击按钮,onclick 它应该将值传递给 asynctask 以便它发布它们在数据库中...
  • 能否请您正确编辑并重构代码

标签: android dialog android-asynctask parameter-passing


【解决方案1】:

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

【讨论】:

  • 是的,我只是在几分钟前尝试过,它成功了。谢谢你 :)
  • 您也可以使用 sharedpreferences 来保存关键数据并与您一起保存所有活动周期,您能否将答案标记为已接受以帮助他人
猜你喜欢
  • 2013-07-06
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多