Either pass your dialog object to your sendTransactionInfo() or make ProgressDialog dialog global would be my suggestion. It looks like it may be a scope issue (just briefly looking at your code).
Android dismiss ProgressDialog after AlertDialog
-
18-07-2023 - |
题
I'm trying to send POST method to server after the user click on confirm button but after click and I get the responce the ProgressDialog still runing.
paymentSubmitBtn = (Button)findViewById(R.id.paymentSubmitBtn);
paymentSubmitBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final String receiver = receiverMymoIdEdtTxt.getText().toString();
runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
builder.setTitle(R.string.message);
builder.setMessage("Transaction Details")
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
ProgressDialog dialog = ProgressDialog.show(Payment.this, "", "Connect to server..." , true);
sendTransactionInfo(receiver);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
});
This is the function that I use to send the POST method and try to dismiss ProgressDialog.
protected void sendTransactionInfo(final String receiver)
{
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
String URL = baseURL + "/api-create-transaction";
try {
HttpPost post = new HttpPost(URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("section", "API" ));
nameValuePairs.add(new BasicNameValuePair("receiver", receiver ));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = client.execute(post);
if(response!=null)
{
String res = EntityUtils.toString(response.getEntity());
final JSONObject result = new JSONObject(res);
String operation = result.getString("operation");
if(operation.trim().equalsIgnoreCase("success"))
{
JSONObject data = result.getJSONObject("data");
}
else if(operation.trim().equalsIgnoreCase("error"))
{
runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
builder.setTitle(R.string.message);
try
{
builder.setMessage(result.getString("message"))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
AlertDialog alert = builder.create();
alert.show();
}
});
Toast.makeText(getBaseContext(), result.getString("message") , Toast.LENGTH_LONG).show();
}
dialog.dismiss();
}
} catch(final Exception e) {
runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
builder.setTitle(R.string.message);
builder.setMessage(e.toString())
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
Looper.loop();
}
};
t.start();
}
解决方案
其他提示
Hmmm try this for a change
alert.dismiss();
but make it global..the referene to the AlertDialog
不隶属于 StackOverflow