Java 我可以将变量传递到私有异步方法中吗?
好的,我要做的是将第一个方法(SaveTemplate)中的变量“int putPrice”插入第二个方法(SaveTemplateTask)中的变量“price” 第一个公共方法:Java 我可以将变量传递到私有异步方法中吗?,java,android,asynchronous,parse-platform,Java,Android,Asynchronous,Parse Platform,好的,我要做的是将第一个方法(SaveTemplate)中的变量“int putPrice”插入第二个方法(SaveTemplateTask)中的变量“price” 第一个公共方法: public boolean SaveTemplate(View view) { final EditText txtPrice = new EditText(getContext()); txtPrice.setKeyListener(DigitsKeyListener.getI
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();
}
}
//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);
第二个私有方法:
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();
}
}
//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);
私有类SaveTemplateTask扩展了AsyncTask{
@凌驾
受保护的布尔doInBackground(Void…params){
试一试{
MainActivity MainActivity=((MainActivity)getActivity());
mainActivity.DrawDesignOnResult();
UUID UUID=mainActivity.SaveResultBitmapToExternalStorage();
//价格是我最终希望由putPrice确定的变量,因为这是最终上传到后端的变量。
int price=Integer.parseInt(putPrice);
String productType=selectedProduct.getSKU();
UploadExternalFileToServer(uuid.toString());
UploadDesignToMarketFeed(GenerateBitmapFromTemplateView(),uuid,price,productType);
}捕获(IOE异常){
Log.e(标记,e.toString());
返回false;
}捕获(解析异常){
Log.e(标记,e.toString());
e、 printStackTrace();
返回false;
}
返回true;
}
@凌驾
受保护的void onPostExecute(布尔结果){
如果(!result){Toast.makeText(getActivity().getApplicationContext(),R.string.failed__保存图像,Toast.LENGTH_SHORT.show();}
Toast.makeText(getActivity().getApplicationContext(),“设计已保存”,Toast.LENGTH\u LONG.show();
}
}
我基本上只是在不同类之间传递变量,以便最终根据用户最初在第一个公共类中输入的内容设置价格。好吧,您可以像参数一样传递: 第二种方法:
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();
}
}
//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);
//这就是您应该如何调用
int参数=某物//您的参数值
新建MyAsyn().execute(参数);
//您的AsyncTask类应该是
类MyAsyn扩展了异步任务{
@凌驾
受保护的void onPostExecute(布尔结果){
super.onPostExecute(结果);
}
@凌驾
受保护的布尔doInBackground(整数…参数){
返回false;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的void onProgressUpdate(整型…值){
super.onProgressUpdate(值);
}
图像是关于方法的参数映射。Awesome。效果非常好。在AsyncTask中破解这三个参数的工作方式仍然有困难。顺序控制是什么?就像第一个参数为Void时,我不能使用Integar paramptprice,但一旦我将Void改为Integer,它就起作用了。剩下的两个参数是什么rameters会做什么?我基本上已经学会了我所知道的大部分编码,只是通过随机的黑客攻击和删减,所以我倾向于向后拼凑。@Kris做了一个很好的(好的,不太好)让我们解释一下:ParamA-将数据传递给asyncTask.ParamB-计算一些进度。ParamC-doInBackground必须传递给onPostExecute方法的返回类型。