【问题标题】:Can I pass a variable into a private Asyncronous method?我可以将变量传递给私有异步方法吗?
【发布时间】:2016-01-09 03:19:18
【问题描述】:

好的,我要做的是将第一个方法(SaveTemplate)中的变量“int putPrice”插入到第二种方法(SaveTemplateTask)中的变量“price”中。

第一个公共方法:

public boolean SaveTemplate(View view) {

        final EditText txtPrice = new EditText(getContext());
        txtPrice.setKeyListener(DigitsKeyListener.getInstance(false, false));

        new AlertDialog.Builder(getContext())
                .setTitle(selectedProduct.getName())
                .setMessage("You will earn every dollar above the base price (" + "USD $" + selectedProduct.getCost() + ") for this item!")
                .setView(txtPrice)
                .setPositiveButton("Sell Product", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        // putPrice is the variable set by the user. I want to pass this into SaveTemplateTask.
                        int putPrice = Integer.parseInt(txtPrice.getText().toString());
                        Toast.makeText(getActivity().getApplicationContext(), "Saving design", Toast.LENGTH_SHORT).show();
                        new SaveTemplateTask().execute();

                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
                .show();


        return true;
    }

第二种私有方法:

private class SaveTemplateTask extends AsyncTask<Void, Integer, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                MainActivity mainActivity = ((MainActivity) getActivity());

                mainActivity.DrawDesignOnResult();

                UUID uuid = mainActivity.SaveResultBitmapToExternalStorage();

                // price is the variable I ultimately want to be determined by putPrice, because this is the variable that is ultimately uploaded to the back-end.
                int price = Integer.parseInt(putPrice);
                String productType = selectedProduct.getSKU();

                mainActivity.UploadExternalFileToServer(uuid.toString());
                ParseHelper.UploadDesignToMarketFeed(GenerateBitmapFromTemplateView(), uuid, price, productType);

            } catch (IOException e) {
                Log.e(TAG, e.toString());
                return false;
            } catch (ParseException e) {
                Log.e(TAG, e.toString());
                e.printStackTrace();
                return false;
            }

            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if(!result) { Toast.makeText(getActivity().getApplicationContext(), R.string.failed_to_save_image, Toast.LENGTH_SHORT).show(); }
            Toast.makeText(getActivity().getApplicationContext(), "Design saved", Toast.LENGTH_LONG).show();
        }
    }

我基本上只是对如何在不同类之间传递变量感到困惑,以便我最终可以将价格设置为用户最初在第一个公共类中输入的函数。

【问题讨论】:

    标签: java android asynchronous parse-platform


    【解决方案1】:

    好吧,你可以像参数一样传递:

    第二种方法:

    //First Integer is your param, change in doInBackground too.
    private class SaveTemplateTask extends AsyncTask<Integer, Integer, Boolean> {
    
        @Override
        protected Boolean doInBackground(Integer... params) {
            Integer paramPutPrice = params[0];
            //.. your logic
        }
    
    }
    

    在第一种方法中:

    new SaveTemplateTask().execute(putPrice);
    

    【讨论】:

    • 太棒了。工作得很好。仍然无法破译这三个参数在 AsyncTask 中的工作方式。订单控制什么?
    • 。就像第一个是 Void 时,我不能使用 Integar paramPutPrice,但是一旦我将 Void 更改为 Integer,它就起作用了。如果有的话,剩下的两个参数有什么作用?我基本上只是通过随意乱砍和砍杀来学习我所知道的大部分编码知识,所以我倾向于把东西拼凑起来。
    • @Kris 拍了一张好(好吧,不太好)的照片。让我们解释一下: Parama - 将数据传递给 asyncTask。 ParamB - 统计一些进度。 ParamC - doInBackground 必须传递给 onPostExecute 方法的返回类型。
    【解决方案2】:
    //This is how you should call your 
    
    int parameter=something;//your parameter value
    new  MyAsyn().execute(parameter);
    
    //Your AsyncTask class should be 
     class MyAsyn extends AsyncTask<Integer,Integer,Boolean>{
        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
        }
    
        @Override
        protected Boolean doInBackground(Integer... params) {
            return false;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }
    

    图片是关于参数与方法的映射。

    【讨论】:

      猜你喜欢
      • 2022-06-17
      • 2017-05-21
      • 2019-12-11
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多