progress dialog doesnt work while listing data with listAdapter
i think its all about sending activity to myAdapter
final myAdapter adapter = new myAdapter(rss_list_activity.this, liste);
final ProgressDialog dialog = ProgressDialog.show(this, "Title",
"Message", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
getListFromXml();
lv.setAdapter(adapter);
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
here's some information on threading, i would use an AsyncTask if I were you:
http://developer.android.com/resources/articles/painless-threading.html
Something similar to this should work:
LoadListTask tsk = new LoadListTask();
tsk.execute((Void) null);
with this somewhere
private class LoadListTask extends AsyncTask<Void, Void, List<theType>>
{
final ProgressDialog dialog;
#Override
protected void onPreExecute()
{
super.onPreExecute();
dialog = ProgressDialog.show(MainClass.this, "Title", "Message", true);
}
#Override
protected void onPostExecute(List<theType> result)
{
super.onPostExecute(result);
final myAdapter adapter = new myAdapter(rss_list_activity.this, result);
lv.setAdapter(adapter);
dialog.dismiss();
}
#Override
protected List<theType> doInBackground(Void... params)
{
return getListFromXml();
}
}
use dialog.show(); to show dialog. you are directly dismissing the dialog using dialog.dismiss();
Related
I want to show a progressDialog on my page when the user clicks on the button.On click of button i am sorting my results that is a List.Now how can we show a progressDialog on click of a button.Please suggest me
This function i am using to sort that data :
public void sortByDate(View v) {
Collections.sort(tripParseData.getDetails());
setData(tripParseData);
}
After #Monica Suggestion
public void sortByDate(View v) {
new LoadData().execute();
}
class LoadData extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
Collections.sort(tripParseData.getCoroprateBookingDetails());
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
setApprovalDetailsData(tripParseData);
}
}
you have to call progressdiolog.show before the long calculation starts and then the calculation has to run in a separate thread. A soon as this thread is finished, you have to call pd.dismiss() to close the prgoress dialog.
here you can see an example:
the progressdialog is created and displayed and a thread is called to run a heavy calculation:
Override
public void onClick(View v) {
pd = ProgressDialog.show(lexs, "Search", "Searching...", true, false);
Search search = new Search( ... );
SearchThread searchThread = new SearchThread(search);
searchThread.start();
}
and here the thread:
private class SearchThread extends Thread {
private Search search;
public SearchThread(Search search) {
this.search = search;
}
#Override
public void run() {
search.search();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
displaySearchResults(search);
pd.dismiss();
}
};
}
Don't forget to vote me up :)
Paste This Class in ur activity and call new LoadData().execute to start the prgress dialog :
class LoadData extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog pg=new ProgressDialog(ChartsActivity.this);
pg=pg.show(ChartsActivity.this, "Loding", "Plz Wait...");
}
#Override
protected Void doInBackground(Void... params) {
sortByDate();
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pg.dismiss();
}
}
I have not tested it but I think it can help you.
Declare these two objects in your class and member variables.
Thread thread = null;
ProgressDialog bar = null;
Use this logic on your button click listener it can work in your case. I have not tested it but It will give you Idea how things should work. you can also use handlemessage in place of runonuithread
if (thread == null
|| (thread != null && !thread.isAlive())) {
thread = new Thread(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//DISPLAY YOUR PROGRESS BAR HERE.
bar = ProgressDialog.show(getApplicationContext(), "LOADING..", "PLEASE WAIT..");
}
});
// SORT COLLECTION HERE
runOnUiThread(new Runnable() {
#Override
public void run() {
//CLOSE YOUR PROGRESS BAR HERE.
bar.dismiss();
}
});
}
});
thread.start();
}
Hope this will help you.
try to use AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html
Sample code:
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
//Show UI
//Start your progress bar
showProgress();
}
#Override
protected Void doInBackground(Void... arg0) {
// do your sorting process
return null;
}
#Override
protected void onPostExecute(Void result) {
//Show UI
//dismiss your progress bar
hideProgress();
}
};
task.execute((Void[])null);
Show and hide progress code
public void showProgress() {
progressDialog = ProgressDialog.show(this, "",
"Loading. Please wait...");
progressDialog.setCancelable(false);
}
public void hideProgress() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
I've been to get rid of a ProgressDialog for some time now. After some googling and reading through questions on stackoverflow I round that I can only run ProgressDialog.dismiss() from the UI Thread. After some more reading I learned I need to create a handler and attach it to the UI Thread like this: new Handler(Looper.getMainLooper()); but alas, the stubborn ProgressDialog still refuses to die.
Here's what my code looks like:
/* Members */
private ProgressDialog mProgressDialog;
private Handler mHandler;
/* Class' constructor method */
public foo() {
...
this.mHandler = new Handler(Looper.getMainLooper());
...
}
/* The code that shows the dialog */
public void startAsyncProcessAndShowLoader(Activity activity) {
...
mProgressDialog = new ProgressDialog(activity, ProgressDialog.STYLE_SPINNER);
mProgressDialog.show(activity, "Loading", "Please wait...", true, false);
doAsyncStuff(); // My methods have meaningful names, really, they do
...
}
/* After a long process and tons of callbacks */
public void endAsyncProcess() {
...
mHandler.post(new Runnable() {
#Override
public void run() {
Log.d(TAG, "Getting rid of loader");
mProgressDialog.dismiss();
mProgressDialog = null;
Log.d(TAG, "Got rid of loader");
}
});
...
}
This doesn't seem to work, debugging shows that certain members of the PorgressDialog (mDecor) are null. What am I missing?
You should use the AsyncTask to do your async task:
AsyncTask task = new AsyncTask<Void, Void, Void>() {
private Dialog mProgressDialog;
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(activity, ProgressDialog.STYLE_SPINNER);
mProgressDialog.show(activity, "Loading", "Please wait...", true, false);
}
protected Void doInBackground(Void... param) {
doAsyncStuff(); // My methods have meaningful names, really, they do
return null;
}
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
}
};
task.execute(null);
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
Log.d(TAG, "Got rid of loader");
}
});
Try this:
public static final int MSG_WHAT_DISMISSPROGRESS = 100;
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg){
swtich(msg.what){
case MSG_WHAT_DISMISSPROGRESS:
mProgressDialog.dismiss();
break;
}
}
}
public void endAsyncProcess(){
handler.obtainMessage(MSG_WHAT_DISMISSPROGRESS).sendToTarget();
}
I am using AsyncTask and fetch twitter feed asynchronously to display in a listView. It works fine Then for refreshing the listview I put the asyncTask call into a Runnable's Run() to load it after a specific interval. The feed loads after specific time interval but the listview is not updating. I use notifyDataSetChanged() inside the asyncTask but it is not working.
This is my trying code:
public class Twitter7FeedActivity extends Activity {
LazyAdapter adapter;
ListView list;
JSONObject jsonObj = null;
JSONArray jsonArray = null;
#SuppressLint("SimpleDateFormat")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_twitter7_feed);
callAsynchronousTask();
}
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
new GetFeedTask().execute(CommonUtils.BEARER_TOKEN,
CommonUtils.URL);
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 50000);
}
protected class GetFeedTask extends AsyncTask<String, Void, String> {
private ProgressDialog dialog = new ProgressDialog(
Twitter7FeedActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
#Override
protected String doInBackground(String... params) {
// related code
}
#Override
protected void onPostExecute(String jsonText) {
// related code
adapter = new LazyAdapter(Twitter7FeedActivity.this,
// tweets);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
Have you tried moving this line:
list = (ListView) findViewById(R.id.list);
after your setContentView in your onCreate method?
// related code adapter = new LazyAdapter(Twitter7FeedActivity.this, tweets);
uncomment this code which is in onPostExecute() method. I think it may help you out. thanks...
You can do the work mentioned in your question using only a Handler. Something like this:
public class Twitter7FeedActivity extends Activity {
LazyAdapter adapter;
ListView list;
JSONObject jsonObj = null;
JSONArray jsonArray = null;
#SuppressLint("SimpleDateFormat")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_twitter7_feed);
callAsynchronousTask();
}
public void callAsynchronousTask() {
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
ProgressDialog dialog;
public void run() {
try {
//Show your dialog here
runOnUiThread(new Runnable() {
public void run() {
runnable.this.dialog = new ProgressDialog( Twitter7FeedActivity.this);
runnable.this.dialog.setMessage("Please wait");
runnable.this.dialog.show();
}
});
//Do your AsyncTask's doInBackground work here
//Update the UI with runOnUiThread or using the Ui threa handler
runOnUiThread(new Runnable() {
public void run() {
// related code adapter = new LazyAdapter(Twitter7FeedActivity.this, tweets);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
//Then post the thread to excecute after 50000 milliseconds.
handler.postDelayed(runnable, 50000);
} catch (Exception e)
{ }
}
};
handler.post(runnable);
}
}
This way you avoid using an AsyncTask inside a TimerTask.
I am parsing an XML from a URL and showing it. I want to show ProgressDialog while my XML Parser Method parse the XML and get it ready to show. The problem I am facing is that no data is shown after ProgressDialog rather it shows blank screen.
Here is my code:
ArrayList<XMLItem> items= new ArrayList<XMLItem>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mydata);
adapter= new MyAdapter(this, R.layout.customcell, items);
listView= (ListView) findViewById(id.ListView01);
listView.setAdapter(adapter);
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
Thread thread=new Thread(new Runnable(){
public void run(){
doTheAutoRefresh();
runOnUiThread(new Runnable(){
public void run() {
if(dialog.isShowing())
dialog.dismiss();
adapter.notifyDataSetChanged(); //this line throws an exception and if i comment this line, blank screen is shown
}
});
}
});
thread.start();
}
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
public void run(){
loadData(); // this is where you put your refresh code
doTheAutoRefresh();
}
}, 15000);
}
private void loadData(){
Document doc;
try {
doc = XMLReader.parseURL("my url to parse xml");
NodeList nl = doc.getElementsByTagName("l");
for(int i=0; i<nl.getLength(); i++){
NamedNodeMap np = nl.item(i).getAttributes();
String t= np.getNamedItem("htn").getNodeValue();
String sT= np.getNamedItem("atn").getNodeValue();
XMLItem item= new XMLItem(t, sT);
items.add(item);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This code shows ProgressDialog perfectly, but after progress dialog, it shows blank screen. Please help me sorting out this problem.
Thanks
private class xyz extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(tranning.this);
#Override
protected void onPreExecute() {
this.dialog.setMessage("Please Wait...");
this.dialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// do your load data function part here.
// just populate the data structures that are necessary
// for the adapter.
return null;
}
#Override
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
// notify adapter here.
adapter.notifyDataSetChanged();
refreshTimer(); // to do the same thing after 15 seconds.
}
}
}
now call the asycTask for the first time in your onCreate() probably.
new xyz().execute()
make a function to put a timer for refresh, we do that by using postDelayed.
private void refreshTimer(){
handler.postDelayed( runner , 15000);
}
private Runnable runner = new Runnable(){
public void run(){
new xyz().execute();
}
}
i think you need to implement AsyncTask ::
private class xyz extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(tranning.this);
#Override
protected void onPreExecute() {
this.dialog.setMessage("Please Wait...");
this.dialog.show();
// put your code which preload with processDialog
}
#Override
protected Void doInBackground(Void... arg0) {
// put your code here
return null;
}
#Override
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
and use this in main ::
new xyz().execute();
add a function showData() and call it after dismissing the dialog
ArrayList<XMLItem> items= new ArrayList<XMLItem>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mydata);
listView= (ListView) findViewById(id.ListView01);
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
Thread thread=new Thread(new Runnable(){
public void run(){
loadData();
runOnUiThread(new Runnable(){
public void run() {
if(dialog.isShowing()){
dialog.dismiss();
showData(); //////////////////////////////////////
}
}
});
}
});
thread.start();
}
private void showData(){
adapter= new MyAdapter(this, R.layout.customcell, items);
listView.setAdapter(adapter);
}
private void loadData(){
Document doc;
try {
doc = XMLReader.parseURL("my url to parse xml");
NodeList nl = doc.getElementsByTagName("l");
for(int i=0; i<nl.getLength(); i++){
NamedNodeMap np = nl.item(i).getAttributes();
String t= np.getNamedItem("htn").getNodeValue();
String sT= np.getNamedItem("atn").getNodeValue();
XMLItem item= new XMLItem(t, sT);
items.add(item);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I don't understand the following error:
Activity [myActivity] has leaked window
com.android.internal.policy.impl.PhoneWindow$DecorView#4050c3f8 that was
originally added here
Here is my code:
private ProgressDialog progression;
private Handler handler;
private Thread thread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Conteneur général
ui = new RelativeLayout(this);
progression = ProgressDialog.show(this, "SwissParl", "Init", true);
handler = new Handler() {
public void handleMessage(Message msg) {
progression.dismiss();
}
};
thread = new Thread() {
public void run() {
firstMethod();
SecondOne();
andTheLast();
handler.sendEmptyMessage(0);
}
};
thread.start();
setContentView(ui);
}
Apparently, the problem is on the line where I instanciate my ProgressDialog...
Any idea?
That happens normally when the activity is destroyed when showing a dialog, for example rotation or finishing activities while showing dialogs.
Try adding a dismiss dialog onPause of activity.
I'm not sure what the problem is, but i suggest you use an AsyncTask
Something like this
private class MyBackgroundTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(ParentActivity.this);
#Override
protected Void doInBackground(Void... param) {
firstMethod();
SecondOne();
andTheLast();
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(dialog.isShowing())
dialog.dismiss();
}
#Override
protected void onPreExecute() {
super.onPreExecute();
//dialog.setCancelable(false); //depending...
dialog.setMessage("Please Wait...");
dialog.show();
}
}
To execute it, call new MyBackgroundTask().execute() inside onCreate()
It's far cleaner than using a handler,thread combination...
private ProgressDialog progression;
private Handler handler;
private Thread thread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Conteneur général
ui = new RelativeLayout(this);
setContentView(ui);
progression = ProgressDialog.show(this, "SwissParl", "Init", true);
handler = new Handler() {
public void handleMessage(Message msg) {
progression.dismiss();
}
};
thread = new Thread() {
public void run() {
firstMethod();
SecondOne();
andTheLast();
handler.sendEmptyMessage(0);
}
};
thread.start();
}
You are trying to show a ProgressDialog before adding the main view to the activity.Use the setContentView before you start the ProgressDialog