我可以将变量传递给私有异步方法吗?
Can I pass a variable into a private Asyncronous method?
好的,我要做的是将第一个方法 (SaveTemplate) 中的变量 "int putPrice" 插入到第二个方法 (SaveTemplateTask) 中的变量 "price" 中。
第一个public方法:
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();
}
}
我基本上只是对如何在不同 classes 之间传递变量感到困惑,这样我才能最终将价格设置为用户最初在第一个 [=27= 中输入的内容的函数] class.
嗯,你可以像参数一样传递:
第二种方法:
//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);
//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);
}
图像是关于参数与方法的映射。
好的,我要做的是将第一个方法 (SaveTemplate) 中的变量 "int putPrice" 插入到第二个方法 (SaveTemplateTask) 中的变量 "price" 中。
第一个public方法:
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();
}
}
我基本上只是对如何在不同 classes 之间传递变量感到困惑,这样我才能最终将价格设置为用户最初在第一个 [=27= 中输入的内容的函数] class.
嗯,你可以像参数一样传递:
第二种方法:
//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);
//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);
}
图像是关于参数与方法的映射。