我可以将变量传递给私有的Asyncronous方法吗?

时间:2016-01-09 03:19:19

标签: java android asynchronous parse-platform

好的,我正在尝试做的是将第一个方法(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();
        }
    }

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

2 个答案:

答案 0 :(得分:3)

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

第二种方法:

//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);

答案 1 :(得分:1)

//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);
    }

http://www.aspose.com/community/forums/aspose.cells-product-family/19/showforum.aspx

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