[英]Progress Dialog not showing in Android
Progress Dialog is not showing in AsyncTask Class In Android. 进度对话框未显示在Android的AsyncTask类中。
My AsyncTask Class Code: 我的AsyncTask类代码:
class SyncData1 {
Context context;
DatabaseHelper myDbHelper;
ArrayList<String> ItemNo= new ArrayList<String>();
ArrayList<String> RepReceivedQty= new ArrayList<String>();
ArrayList<String> HotelReceivedQty= new ArrayList<String>();
ArrayList<String> IStatus= new ArrayList<String>();
String jsodata;
String ChallanNo="",HotelRepReceivedOn="",HotelReceivedOn="",Status="";
String url=null;
Commans com;
public SyncData1(Context context)
{
this.context=context;
}
public void fetchData()
{
ItemNo.clear();
IStatus.clear();
RepReceivedQty.clear();
HotelReceivedQty.clear();
com= new Commans(context);
myDbHelper =new DatabaseHelper(context);
com.connectdb(myDbHelper);
Cursor curval= myDbHelper.dbQery("Select * from Challan_R where Flag=1 limit 1");
if(curval != null && curval.moveToFirst()) {
ChallanNo= curval.getString(0);
Status=curval.getString(5);
HotelRepReceivedOn=curval.getString(6);
HotelReceivedOn= curval.getString(7);
}
curval.close();
Cursor curItem= myDbHelper.dbQery("Select * from ChallanItems where ChallanNo= "+"'"+ChallanNo+"'");
if(curItem != null && curItem.moveToFirst()) {
while( !curItem.isAfterLast())
{
ItemNo.add(curItem.getString(0));
IStatus.add(curItem.getString(2));
RepReceivedQty.add(curItem.getString(9));
HotelReceivedQty.add(curItem.getString(1));
curItem.moveToNext();
}
}
curItem.close();
if(ChallanNo.length()>1)
{
jsodata=com.reciveJson(ChallanNo, HotelRepReceivedOn, HotelReceivedOn, Status, ItemNo, RepReceivedQty, HotelReceivedQty, IStatus);
Log.d("Json", jsodata);
ReciveChallanAsyn task = new ReciveChallanAsyn();
task.execute("");
}
}
private class ReciveChallanAsyn extends AsyncTask<String, Void, String> {
public static final int HTTP_TIMEOUT = 30 * 1000;
@Override
protected String doInBackground(String... urls) {
String response = "";
HttpEntity resEntity;
try {
//url=urls[0].getUrl();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Constants.SERVICE_SYNC_DATA);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("data",jsodata));
nameValuePairs.add(new BasicNameValuePair("token",CreateChallan.token));
nameValuePairs.add(new BasicNameValuePair("customer_code",CreateChallan.strcustomer_code));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse responsePOST = httpclient.execute(httppost);
resEntity = responsePOST.getEntity();
response=EntityUtils.toString(resEntity);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
@SuppressLint("DefaultLocale")
@Override
protected void onPostExecute(String result) {
Log.d("Result", result);
pd.dismiss();
if(result!=null)
{
if(result.contentEquals("1") && result!=null)
{ com.connectdb(myDbHelper);
Toast.makeText(context, " Challan Sent ", Toast.LENGTH_LONG).show();
myDbHelper.ChallN_Updateb("", "2", ChallanNo,"");
Cursor curval1= myDbHelper.dbQery("Select * from Challan_R where Flag=1 limit 1");
if(curval1 != null && curval1.moveToFirst()) {
fetchData();
}
curval1.close();
com.disconnectdb();
}
else
Toast.makeText(context, "Error In Sending Challan", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(context, "Data Not Fount", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onPreExecute()
{
// pd = ProgressDialog.show(HomeChallan.this, ""," Please wait...");
pd = new ProgressDialog(HomeChallan.this);
pd.setMessage(" Please wait... ");
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
}
}
It's a subclass of my main class where I am using the Async Task class 这是我使用异步任务类的主类的子类
I am using the class SyncData1 class on onclickListerner Method 我在onclickListerner方法上使用类SyncData1类
Cursor curval1= myDbHelper.dbQery("Select * from Challan_R where Flag=1 limit 1");
if(curval1 != null && curval1.moveToFirst()) {
SyncData1 data= new SyncData1(context);
data.fetchData();
Hope this can solve your issue, do some editing as per your requirment.. 希望这可以解决您的问题,根据您的要求进行一些编辑。
private final Handler handler = new Handler();
private Thread thread;
private QueueRunner runner = new QueueRunner();
private CustomProgressDialog mProgressBar;
private class QueueRunner implements Runnable
{
public void run()
{
try
{
synchronized(this)
{
handler.post(new Runnable()
{
public void run()
{
if (((Activity) AsyncWebPostClient.this.context).isFinishing() == false)
mProgressBar = CustomProgressDialog.show(AsyncWebPostClient.this.context,
null,null,true,true );
}
});
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
in your preExecute call this.. 在您的preExecute中调用此。
thread = new Thread(runner);
thread.start();
in your onpost call this.. 在你的岗位上叫这个。
mProgressBar.dismiss();
Edit: I have use customeProgress Dialog you can use Progress Dialog 编辑:我有使用customeProgress对话框,您可以使用进度对话框
this is your asynctask method,copy and paste it hope it's worked: 这是您的asynctask方法,请复制并粘贴它,希望它能起作用:
private class ReciveChallanAsyn extends AsyncTask<String, Void, String> {
public static final int HTTP_TIMEOUT = 30 * 1000;
@Override
protected void onPreExecute()
{
// pd = ProgressDialog.show(HomeChallan.this, ""," Please wait...");
pd = new ProgressDialog(HomeChallan.this);
pd.setMessage(" Please wait... ");
pd.setIndeterminate(false);
pd.setCancelable(false);
pd.show();
}
@Override
protected String doInBackground(String... urls) {
String response = "";
HttpEntity resEntity;
try {
//url=urls[0].getUrl();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Constants.SERVICE_SYNC_DATA);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("data",jsodata));
nameValuePairs.add(new BasicNameValuePair("token",CreateChallan.token));
nameValuePairs.add(new BasicNameValuePair("customer_code",CreateChallan.strcustomer_code));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse responsePOST = httpclient.execute(httppost);
resEntity = responsePOST.getEntity();
response=EntityUtils.toString(resEntity);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
@SuppressLint("DefaultLocale")
@Override
protected void onPostExecute(String result) {
Log.d("Result", result);
pd.dismiss();
if(result!=null)
{
if(result.contentEquals("1") && result!=null)
{ com.connectdb(myDbHelper);
Toast.makeText(context, " Challan Sent ", Toast.LENGTH_LONG).show();
myDbHelper.ChallN_Updateb("", "2", ChallanNo,"");
Cursor curval1= myDbHelper.dbQery("Select * from Challan_R where Flag=1 limit 1");
if(curval1 != null && curval1.moveToFirst()) {
fetchData();
}
curval1.close();
com.disconnectdb();
}
else
Toast.makeText(context, "Error In Sending Challan", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(context, "Data Not Fount", Toast.LENGTH_LONG).show();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.