I want to load datas from a list to gridview with a loading effect using progressbar.Im getting items from a webservice.The problem i face is im unable to dismiss the progress bar even after showing the gridview.I can see the gridview with items but progress bar is still running .What am i doing wrong here.
private void testAsyncTask() {
Log.e("Im in testAsyncTask()", "");
new AsyncTask<Object, Object, Object>() {
#Override
protected void onPreExecute() {
progress_Dialog = ProgressDialog.show(a, "", "Loading");
Log.e("Im in onPreExecute", "");
// super.onPreExecute();
}
#Override
protected Integer doInBackground(Object... params) {
MenuService menuService = new MenuServiceImpl();
PartnerMenuServiceResponse partnerMenu = menuService
.getPartnerMenu();
jewellist = partnerMenu.getMenu().getMenuEntries();
Log.e("Im in doInBackground", "");
System.gc();
return 0;
}
#Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
// super.onPostExecute(result);asd
Log.e("Im in onPostExecute", "");
if (progress_Dialog.isShowing()) {
progress_Dialog.dismiss();
}
ShopGridAdapter adapter = new ShopGridAdapter(ShopGridActivity.this, jewellist);
AllJewelgridView.setAdapter(adapter);
adapter.notifyDataSetChanged();
//AllJewelgridView.setAdapter(new ShopGridAdapter(
// ShopGridActivity.this, jewellist));
if (AllJewelgridView.getCount() <= 0) {
MyAlertDialog.ShowAlertDialog(ShopGridActivity.this, "",
"No data found.", "OK");
}
progress_Dialog.dismiss();
}
}.execute();
}
#Override
protected void onPreExecute() {
progress_Dialog = new ProgressDialog(context);
progress_Dialog.setMessage("Loading...");
progress_Dialog.show();
}
EDIT :
#Override
protected void onPostExecute(Object result) {
Log.e("Im in onPostExecute", ""); <------ ARE YOU ABLE SEE THIS IN logcat ?
progress_Dialog();
}
It may possible onPostExecute() not called. So to confirm check logcat
You need to put some code in your AsyncTask
ProgressDialog progress=null;
progress=ProgressDialog.show(this,"title","loading..").show();//put this code in onPreExecute()
progress.dismiss();//put this code in onPostExecute()
for more ProgressBar while loading ListView (using AsyncTask)
You can write in onPostExecute method may help you.
if ( progress_Dialog != null) {
progress_Dialog.cancel();
}
Related
how to get the result of the function void in asyntask
I've tried like this but the application always stops
I want to implement a progressbar in webview with asyntask when the waiting process
note: I've read this Webview with asynctask on Android
public class MainActivity extends AppCompatActivity {
EditText edInput;
Button btnCari;
WebView webView;
public String dataUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initEvent();
new asynCaller().execute();
}
private void initUI(){
edInput = (EditText) findViewById(R.id.editText);
dataUrl = edInput.getText().toString();
btnCari = (Button) findViewById(R.id.button);
webView = (WebView) findViewById(R.id.webview);
}
private void initEvent() {
btnCari.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String dataUrl = edInput.getText().toString();
dataUrl = dataUrl.isEmpty() ? "google" : dataUrl;
loadWebview("https://" + dataUrl + ".com");
message("Data link is "+dataUrl);
}
});
}
private void message(String pesan){
Toast.makeText(MainActivity.this,pesan, LENGTH_SHORT).show();
}
private boolean checkConnection(){
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnectedOrConnecting();
}
private void statusConnection(){
if (checkConnection()){
message("Device Online");
}else{
message("Device Offline");
}
}
private void loadWebview(String url){
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
}
public class asynCaller extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
#Override
protected void onPreExecute() {
super.onPreExecute();
// progressDialog.setMessage("Loading...");
// progressDialog.show();
message("persiapan");
}
#Override
protected Void doInBackground(Void... params) {
statusConnection();
if (checkConnection()) {
dataUrl = dataUrl.isEmpty() ? "google" : dataUrl;
loadWebview("https://" + dataUrl + ".com");
message("Data link is " + dataUrl);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// progressDialog.dismiss();
message("selesai");
}
}
EDITED
thank's for your help
i change a method doInBackground to onProgressUpdate for showing Webview and work and i get new problem with progress dialog, the progress dialog can't dismiss()
#Override
protected String doInBackground(String... params) {
publishProgress();
return url;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
url = edInput.getText().toString();
progressDialog.show(MainActivity.this,"Pesan","Memuat . . .",true);
}
#Override
protected void onPostExecute(String result) {
if (progressDialog.isShowing()){
progressDialog.dismiss();
}
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
loadWeb(url);
}
}
Change Your doInBackground() return type to String/int
public class asynCaller extends AsyncTask<Void, Void, String> {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
#Override
protected void onPreExecute() {
super.onPreExecute();
// progressDialog.setMessage("Loading...");
// progressDialog.show();
message("persiapan");
}
#Override
protected String doInBackground(String... params) {
statusConnection();
if (checkConnection()) {
dataUrl = dataUrl.isEmpty() ? "google" : dataUrl;
loadWebview("https://" + dataUrl + ".com");
message("Data link is " + dataUrl);
}
return "Your Message";
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// progressDialog.dismiss();
Log.d("Reached_postExe",result);
message("result");
}
}
The following is the code I would use, refactored from your own code. I have taken the liberty of making changes to your messaging to make it more meaningful in the console.
public class AsynCaller extends AsyncTask<Void, Void, String> {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
#Override
protected void onPreExecute() {
super.onPreExecute();
System.out.println("AsynCaller.onPreExecute called");
// progressDialog.setMessage("Loading...");
// progressDialog.show();
message("AsynCaller.onPreExecute called");
}
#Override
protected String doInBackground(Void... params) {
System.out.println("AsynCaller.doInBackground called");
statusConnection();
final String result; // making it final forces it's definition whichever logic flow the code takes, which is good practice for a returned value
if (checkConnection()) {
dataUrl = dataUrl.isEmpty() ? "google" : dataUrl;
final String fullUrl = "https://" + dataUrl + ".com";
loadWebview(fullUrl);
result = "AsynCaller.doInBackground loadWebView called with " + fullUrl;
} else {
result = "AsynCaller.doInBackground checkConnection() is false");
}
// message(result); this is unnecessary as the Toast will appear due to the message() call in onPostExecute
return result;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("AsynCaller.onPostExecute called");
// progressDialog.dismiss();
message(result);
}
}
I would, if I were you, replace the System.out calls with Log.d() so that the console output is only done in debuggable mode and not in any release version of your app. The reference for this is here Log
As a last suggestion I would not have the ProgressDialog being a property of the AsyncTask but instead call methods in MainActivity, as you have done with message() for instance. There are issues, for instance in this case, around possible memory leaks etc. if an object effectively holds a reference to an Activity context and the Activity is destroyed while the object continues to exist, as would be the case for a running AsyncTask.
I am using Progress Bar that only shows up at the time of loading data using Volley but my progress bar freezes till the data loads. Please help. Below is the code that is working fine except the progress bar freezes.
private class GetValue extends AsyncTask<String, String, String> {
ProgressDialog progressDialog=null;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(context, "Loading Data...", "Please Wait");
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
assignValues(mnyr ,mnyr1);
return null;
}
}
private void assignValues(String s1, String s2) {
// TODO Auto-generated method stub
//C.progressStart(context, "Loading Data...", "Please Wait");
final String s = s1;
final List<List<String>> values = new ArrayList<List<String>>();
datas = new ArrayList<CustomData>();
RequestQueue queue = Volley.newRequestQueue(this);
String val = s1 + "/" + s2;
final StringRequest request = new StringRequest(Method.GET, C.EVENTS + val,
new Listener<String>() {
#Override
public void onResponse(String arg0) {
// TODO Auto-generated method stub
JsonParser parent = new JsonParser(arg0);
if(parent.getValue("data") == null){
int length = parent.getArrLength(arg0);
ProgressDialog progressDialog=null; // inside asynTask method
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(uractivity.this, "Wait", "Downloading...");
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
}
Hi everyone i am facing problem in showing loading box on pressing send button using send_Schedule() function. I have the following code.
#Override
public void onClick(View arg0) {
pdialog.setCancelable(true);
pdialog.setMessage("Loading ....");
pdialog.show();
send_Schedule();
}
In send_Schedule() function i am putting delay of 3 secs like this in the following code. but dialog box always shows up after completion of loop.
send_Schedule(){
for(int i=0;i<100;i++){
Log.d("TAG",""+i)
try {
Thread.sleep(3000);
Log.e("----------------", "-----------------");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
pdialog.dismiss();
}
I want to show dialog box while sending data...
Try this..
private class YourTaskLoader extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(YourActivity.this);
progressDialog.setCancelable(false);
progressDialog.setMessage("Importing Messages...!");
progressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// Write you back ground logic here
return null;
}
#Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
super.onPostExecute(result);
}
}
Invoke like in your Activity
new YourTaskLoader().execute();
Check this more info AsyncTask
Could anyone help me on following questions.
1) onPostExecute - Toast.make while in background i am sending HttpRequest.
0nCraeteBunle - execute() ; startNewActivity
showing error. AsycTask# Runtime Exception .
While commenting Http request in background, no error is showed.
here, how can i know that http Request and reply finished , so that i can start my new Activity.
2) how to get HttpParams. Sending from TIBCO BE (As event with properties)
3) What if i am recieving JSONObject, JAVAObject, Integer other than String in onPostExecute. unable to override .
Try this,
protected class GetTask extends AsyncTask<Void, Void, Integer> {
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(MainActivity.this,
"Loading", "Please wait");
}
#Override
protected Integer doInBackground(Void... params) {
// TODO Auto-generated method stub
//call ur HttpRequest
httpRequest();
return 0;
}
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
mHandler.sendEmptyMessage(0);
}
}
Handler mHandler = new Handler() {
public void handleMessage(Message Msg) {
if (Flag) {
//Add ur stuff
}else{
}
And then in ur method set Flag value
public void httpRequest() {
// TODO Auto-generated method stub
String URL ="ADD UR URL";
try {
JSONObject ResponseObject = mAPIService.CallAPI(
YourActivity.this, URL);
String status = ResponseObject.getString("status");
Flag = true;
} catch (Exception err) {
Flag = false;
}
}
I have a problem which I don't understand. I want to show a simple Progress Dialog in Android. So I created an AsyncTask and create the dialog in the constructor. I use the methods onPreExceution to initialise the dialog and the onPostExecute method I destory the dialog. So until now this looks total correct for me. But when I start the App on my Nexus 7 the dialog doesn't show up till the job is done. So it shows up for a half of a second at the end of the job... What am I doing wrong?
Thank you for your help ;)
public class ParseHTMLCodeNew extends AsyncTask<String, Void, String> {
ProgressDialog dialog;
public ParseHTMLCodeNew(Context context) {
dialog = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
//einrichten des Wartedialogs
dialog.setTitle("Bitte warten!");
dialog.setMessage("Die Kommentare werden vom Server geladen.");
dialog.show();
}
#Override
protected String doInBackground(String params) {
InputStream is = null;
String data = "";
try
{
URL url = new URL( params[0] );
is = url.openStream();
data = new Scanner(is).useDelimiter("//html//").next();
}
catch ( Exception e ) {
e.printStackTrace();
}
return data;
}
#Override
protected void onPostExecute(String result) {
//Dialog beenden RSS Feed ist fertig geparst
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
}
UPDATE
This is my new AsyncTask:
public class ParseHTMLCodeNew extends AsyncTask<String, String, String> {
ProgressDialog dialog;
private final OnCompleteTaskListener onCompleteTaskListener;
public interface OnCompleteTaskListener {
void onComplete(String data);
}
public ParseHTMLCodeNew(Context context, OnCompleteTaskListener taskListener) {
onCompleteTaskListener = taskListener;
dialog = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
//einrichten des Wartedialogs
dialog.setTitle("Bitte warten!");
dialog.setMessage("Die Kommentare werden vom Server geladen.");
dialog.show();
}
#Override
protected String doInBackground(String... params) {
InputStream is = null;
String data = "";
try
{
URL url = new URL( params[0] );
is = url.openStream();
data = new Scanner(is).useDelimiter("//html//").next();
}
catch ( Exception e ) {
e.printStackTrace();
}
return data;
}
#Override
protected void onPostExecute(String result){
onCompleteTaskListener.onComplete(result);
//Dialog beenden RSS Feed ist fertig geparst
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
}
And i am calling it this way:
new ParseHTMLCodeNew(this,new OnCompleteTaskListener() {
#Override
public void onComplete(String data) {
gData = data;
}
}).execute(url);
As i commented on your post, data has no value.
If you calling this code so:
String data = new ParseHTMLCodeNew(CommentActivity.this).execute(url).get();
Then you do not really see your dialogue because there is a blocking UI.
Method get() waits if necessary for the computation to complete, and then retrieves its result.
Call so:
new ParseHTMLCodeNew(CommentActivity.this).execute(url);
and the result of the work is handled directly in the AsyncTask.
If you need to transfer the data to the main thread, you should tell him that the task was completed.
Wat is the simple code, I just added OnCompleteTaskListener interface
public class ParseHTMLCodeNew extends AsyncTask<String, Void, String> {
private final OnCompleteTaskListener onCompleteTaskListener;
private ProgressDialog dialog;
public interface OnCompleteTaskListener {
void onComplete(String data);
}
public ParseHTMLCodeNew(Context context, OnCompleteTaskListener taskListener) {
onCompleteTaskListener = taskListener;
dialog = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
// einrichten des Wartedialogs
dialog.setTitle("Bitte warten!");
dialog.setMessage("Die Kommentare werden vom Server geladen.");
dialog.show();
}
#Override
protected String doInBackground(String... params) {
StringBuilder sb = new StringBuilder();
// your code here
try {
for (int i = 0; i < 100; i++) {
Thread.sleep(100);
sb.append(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return sb.toString();
}
#Override
protected void onPostExecute(String result) {
// Dialog beenden RSS Feed ist fertig geparst
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
onCompleteTaskListener.onComplete(result);
}
}
And the example of a call
new ParseHTMLCodeNew(this,new OnCompleteTaskListener() {
#Override
public void onComplete(String data) {
Toast.makeText(CommentActivity.this, data, Toast.LENGTH_LONG).show();
}
}).execute("your_url");
Be careful, this code can produce errors when you rotate your Phone.
When Activity destroyed but task is performed:
- progress dialog will close and will not open again
- local variable to dialog or context is incorrect.
If the operation is performed for a long time can make it through the of the services?
I've wrote a code that get data from online database and populate that data in lisview here is the part of my code hope that help !
class LoadMyData extends AsyncTask<String, String, String> {
//Before starting background thread Show Progress Dialog
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getParent());
pDialog.setMessage("Loading. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
//Your code here
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting the data
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// In my case use my adapter to display the data in a listview
adapter = new MyAdaper();
list.setAdapter(adapter);
}
});
}
}
Progress dialog should be shown from UI thread
runOnUiThread(new Runnable() {
public void run() {
dialog.setTitle("Bitte warten!");
dialog.setMessage("Die Kommentare werden vom Server geladen.");
dialog.show();
}});