how to initialize ProgressDialog in custom adapter - android

I am trying to use AsuncTask in custom adapter. when i tried to use
class UpdateProductVariantTask extends AsyncTask<String, String, Void> {
private ProgressDialog progressDialog = new ProgressDialog(context);
String result = "";
protected void onPreExecute() {
progressDialog.setCancelable(false);
progressDialog.setMessage("Please wait.....");
progressDialog.show();
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface arg0) {
UpdateProductVariantTask.this.cancel(true);
}
});
}
}
I am getting this error:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
in line progressDialog.show();
How to fix this error?

try to use Activity not context.
private ProgressDialog progressDialog = new ProgressDialog(activity);

Related

AsyncTask onPreExecute new progressdialog

class AddStudent extends AsyncTask<String, Void, ResultData> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AddStudentActivity.this);
pDialog.setMessage("Adding Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
private Context context;
//CHANGE HERE....ADD PARAMATER
TextView tv_msg;
public AddStudent(Context context, TextView tv_msg) {
this.context = context;
this.tv_msg = tv_msg;
}
I have an error in (AddStudentActivity.this);
Error = com.blablablabla.AddStudentActivity is not an enclosing class.
What's the problem?.
How can I fix this?.
If the asynctask is not a nested class of an activity you need to set/add the context as a parameter to the constructor.
class AddStudent extends AsyncTask<String, Void, ResultData> {
private ProgressDialog pDialog;
private Context context;
public AddStudent(Context context) {
this.context = context;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context) ;
pDialog.setMessage("Adding Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
}
TextView it is part of the activity.
Or, if the asynctask is a nested class of an activity, then you can do what you want. More you can read in the example below:
ProgressDialog and AsyncTask
You have not posted your entire code, so this is a bit of speculation, but here goes:
Most probably you have created a separate file for your AddStudent AsyncTask, or put it outside your AddStudentActivity class. You need to make AddStudent an inner class of AddStudentActivity to be able to use AddStudentActivity.this.
More info here : Android: AsyncTask recommendations: private class or public class?
pDialog = new ProgressDialog(AddStudentActivity.this) ;
change to
pDialog = new ProgressDialog(context) ;
its work.
Error = com.blablablabla.AddStudentActivity is not an enclosing class.
above error comes whenever you try to use Activity context in other separate class .
This should even harmful for you when it will give you memory leaks.
pDialog = new ProgressDialog(context);

Show ProgressDialog in Fragment class

I am trying to show a ProgressDialog within a Fragment class. The following code just works within an Activity class but not for Fragment. Can somebody please help me on this, why this ProgressDialog implementaion just works within an Activity and not for a Fragment?
private class ProcessUpdateProfile extends
AsyncTask<String, String, JSONObject> {
private ProgressDialog nDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
nDialog = new ProgressDialog(PFragment.this); //Here I get an error: The constructor ProgressDialog(PFragment) is undefined
nDialog.setMessage("Loading..");
nDialog.setTitle("Checking Network");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
}
Try this in Fragment
nDialog = new ProgressDialog(getActivity());
ProgressDialog take Context input so use getActivity() in object creation.
ProgressDialog dialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);

Showing activity indicator in android

I am new to android.I need to show an activity indicator while synchronizing.On clicking a button,i am redirecting to a new activity.In this new activity i am synchronizing.I need to show an activity indicator on button click till it is synchronized.
Synchronizing is effectively a network task And you have to do this on Background ( Async Task) So you can call an AsyncTask in your new Activity
private class SyncOperation extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
#Override
protected String doInBackground(String... params) {
// Synchronize code here
return null;
}
#Override
protected void onPostExecute(String result) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
#Override
protected void onPreExecute() {
if (progressDialog == null) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Synchronizing, please wait...");
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
}
}
}
Now in OnCreate() of new Activity
SyncOperation syncTask=new SyncOperation();
syncTask.execute();
It will show a loader like

ProgressDialog is not shown in the AsyncTask

Document doc = new Obtainer(context, uri).execute().get();
This code in the activity class renders the Obtainer(which extends AsyncTask) which gets the xml document from the url. This is the onPreExecute method:
protected void onPreExecute() {
super.onPreExecute();
System.out.println("Pre execute began");
exception = null;
dialog = new ProgressDialog(context);
dialog.setMessage("Loading started");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
System.out.println("Preexecute end");
dialog.show();
}
context is set in the Constructor:
public Obtainer(Context c, String addr) {
context = c;
address = addr;
}
During the runtime I can see in the console output both "Pre execute began" and "Preexecute end" but the progress dialog is not shown. What is the probleM?
Use this code, it works for me:
class Obtainer extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(App.this); // App - your main activity class
dialog.setMessage("Please, wait...");
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// ...
}
#Override
protected void onPostExecute(Void result) {
dialog.dismiss();
}
}
And in your main activity class method call
new Obtainer().execute();
What Context are you passing when you create your Obtainer (AsyncTask subclass)?
If you are using the Application context via getApplicationContext(), it can not be used to create a Dialog (or any View for that matter). You need to pass it a Context that can create Views.
"If you're in the habit of using your application context (from a call to getApplicationContext(), for example) in places where you need a Context to create views, it's only a matter of time until you find a case where things don't work quite like you would want or expect."
From: https://plus.google.com/107708120842840792570/posts/VTeRBsAeyTi

Progress dialog wont show with async task

I have been searching for an answer for this for some time now. I have an async task that downloads the database needed for my app, while this is downloading my app cant do anything as all the data it references is in this file, i have the app waiting for the file to be downloaded but i am attempting to show a progress dialog so the user knows something is happening while they wait for this to happen.
my code is currently
public class fileDownloader extends AsyncTask<Void, Integer, SQLiteDatabase>
{
private File dbFile;
private ProgressDialog progressDialog;
private Context context;
private SQLiteDatabase database;
private SQLiteDatabase.CursorFactory factory;
public fileDownloader(Context c)
{
super();
context = c;
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(this.context);
progressDialog.setMessage("Downloading Database...");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
#Override
protected SQLiteDatabase doInBackground(Void... v)
{
....
}
#Override
protected void onPostExecute(SQLiteDatabase db1)
{
progressDialog.dismiss();
}
however nothing shows up i have also tried directly calling ProgressDialog.show in the pre execute and moving this to the calling activity with no luck.
please help!
the solution to this was to look at the calling class the UI thread was getting blocked therefor the dialog never showed up.
Hmm - how long does your doInBackground method run? Maybe your dialog is shown, but the time is just too fast for the dialog to show up...
Below code is working fine, I am using it:
private class DownloadHomeSectionData extends
AsyncTask<Void, Void, Boolean> {
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(mainScreen);
progressDialog.setMessage(getResources()
.getString(R.string.loading));
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
#Override
protected Boolean doInBackground(Void... params) {
for (HomeButton homeBtn : appDesigner.getHomeButtons()) {
StorageManager.getInstance().downloadFileSyncronously(
homeBtn.getTab_logo_selected_image());
StorageManager.getInstance().downloadFileSyncronously(
homeBtn.getTab_logo_unselected_image());
}
return null;
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progressDialog.dismiss();
}
}
In your case, I think you may be passing wrong context to ProgressDialog.

Categories

Resources