我可以通过一个变量为一体的民营Asyncronous方法? [英] Can I pass a variable into a private Asyncronous method?

查看:152
本文介绍了我可以通过一个变量为一体的民营Asyncronous方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我想要做的就是插上从这个第一种方法(SaveTemplate)为第二个方法(SaveTemplateTask)变量价格变量INT putPrice。

Okay so what I'm trying to do is plug in the variable "int putPrice" from this first method (SaveTemplate) into the variable "price" in the second method (SaveTemplateTask).

首次公开方式:

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

我基本上只是难倒如何通过不同阶级之间的变量,这样我就可以最终把价格定成函数关系是什么在第一个公共类最初的用户输入。

I'm basically just stumped on how to pass the variables between the different classes so that I can ultimately set the price as a function of what the user input originally in the first public class.

推荐答案

好了,你可以通过像参数:

Well, you can pass like a param:

第二种方法:

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

这篇关于我可以通过一个变量为一体的民营Asyncronous方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆