- Forum posts: 2
Jul 14, 2017, 11:36:20 AM via Website
Jul 14, 2017 11:36:20 AM via Website
Hello,
I prepared AsyncTask with AlertDialog in which are Yes / No button. I need return 1 for Yes and 0 for No.
I dont understand AsyncTask fully. What hate to add to wait for click? Current state doesnt wait :/
CODE CLASS:
class MyTask extends AsyncTask<Void,Integer,Integer> {
TextView ozn_=null;
AlertDialog dialog=null;
AlertDialog.Builder builder=null;
ImageView icon=null;
int ret=0;
public int getRetValue(){
return ret;
}
public MyTask(int a){
ret=a;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
ozn_ = new TextView(LoginActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
ImageView icon = new ImageView(LoginActivity.this);
icon.setBackgroundResource(R.drawable.warn);
builder.setMessage("oznam");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(set_dp(60), set_dp(60));
icon.setLayoutParams(params);
ozn_.setGravity(Gravity.LEFT);
LinearLayout l1 = new LinearLayout(LoginActivity.this);
l1.setPadding(set_dp(12), set_dp(12), set_dp(12), set_dp(20));
l1.setOrientation(LinearLayout.HORIZONTAL);
l1.setGravity(Gravity.CENTER_VERTICAL);
ozn_.setPadding(set_dp(12), 0, 0, 0);
l1.addView(icon);
l1.addView(ozn_);
builder.setView(l1);
builder.setPositiveButton("Áno", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
publishProgress(11); //somewhere here is needed control of wait
getResources().notifyAll();
dialog.dismiss();
}
});
builder.setNegativeButton("Nie", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
publishProgress(222); //somewhere here is needed control of wait
getResources().notifyAll();
dialog.dismiss();
}
});
dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(0xff00b300);
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(0xffd56300);
}
});
dialog.setCancelable(false); //backbutton
dialog.setCanceledOnTouchOutside(false); //klick outside dialog
dialog.show();
}
@Override
protected Integer doInBackground(Void... rets) {
return ret;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate();
ret++;
}
@Override
protected void onPostExecute(Integer aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(LoginActivity.this, "post"+ret, Toast.LENGTH_SHORT).show();
}
}
and CODE IN ACTIVITY:
*****************************
int a=654;
MyTask task=new MyTask(a);
task.execute();
a=task.getRetValue();
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.