Problem with ProgressDialog

Put your problem here if it does not fit any of the other categories.

Problem with ProgressDialog

Postby nico » Sun Jan 13, 2008 1:36 pm

Actually it's not the problem with ProgressDialog, but it's my problem for not being able to use it :lol:
Here's my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.nico.ProgressDialogTest;
  2.  
  3.  
  4.  
  5. import java.util.ArrayList;
  6.  
  7. import java.util.List;
  8.  
  9.  
  10.  
  11. import android.app.ListActivity;
  12.  
  13. import android.app.ProgressDialog;
  14.  
  15. import android.os.Bundle;
  16.  
  17. import android.view.View;
  18.  
  19. import android.widget.ArrayAdapter;
  20.  
  21. import android.widget.ListView;
  22.  
  23.  
  24.  
  25. public class ProgressDialogTest extends ListActivity {
  26.  
  27.         ProgressDialog myProgressDialog = null;
  28.  
  29.         private List<String> directoryEntries = new ArrayList<String>();
  30.  
  31.         private ProgressDialog p;
  32.  
  33.  
  34.  
  35.         @Override
  36.  
  37.         public void onCreate(Bundle icicle) {
  38.  
  39.                 super.onCreate(icicle);
  40.  
  41.                 directoryEntries.add("Click me");
  42.  
  43.                 directoryEntries.add("Click me too");
  44.  
  45.                 ArrayAdapter<String> directoryList = new ArrayAdapter<String>(
  46.  
  47.                                 ProgressDialogTest.this, R.layout.file_row,
  48.  
  49.                                 directoryEntries);
  50.  
  51.                 setListAdapter(directoryList);
  52.  
  53.         }
  54.  
  55.  
  56.  
  57.         private void createList() {
  58.  
  59.                 // TODO Auto-generated method stub
  60.  
  61.                 p = ProgressDialog.show(ProgressDialogTest.this, "Please wait..", "Loading list..", true);
  62.  
  63.                 new Thread(){
  64.  
  65.                         public void run(){
  66.  
  67.                                 //do some extreme work before creating list
  68.  
  69.                                 try {
  70.  
  71.                                         sleep(5000);
  72.  
  73.                                 } catch (InterruptedException e) {
  74.  
  75.                                         // TODO Auto-generated catch block
  76.  
  77.                                         e.printStackTrace();
  78.  
  79.                                 }
  80.  
  81.                                 directoryEntries.clear();
  82.  
  83.                                 directoryEntries.add("Android");
  84.  
  85.                                 directoryEntries.add("Developer");
  86.  
  87.                                 directoryEntries.add("Challenge");
  88.  
  89.                                 p.dismiss();
  90.  
  91.                         }
  92.  
  93.                 }.start();
  94.  
  95.                 ArrayAdapter<String> directoryList = new ArrayAdapter<String>(
  96.  
  97.                                 ProgressDialogTest.this, R.layout.file_row,
  98.  
  99.                                 directoryEntries);
  100.  
  101.                 setListAdapter(directoryList);
  102.  
  103.         }
  104.  
  105.        
  106.  
  107.         protected void onListItemClick(ListView l, View v, int position, long id) {
  108.  
  109.                 createList();
  110.  
  111.         }
  112.  
  113. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


what i want to do is to do something heavy before updating the list, thus i use the ProgressDialog while the work is in progress. But using above code, the list is not updated. I believe the problem is because the method setListAdapter(directoryList) is called before the content of the list updated. The problem is i don't know how to solve this problem :oops:

Please help, everyone :wink:
nico
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Nov 27, 2007 10:33 am
Location: Indonesia

Top

Postby plusminus » Sun Jan 13, 2008 2:00 pm

Hello nico,

yes, you are 100% right. Your program splits to another thread and things get indeterministic (by chance it could work as you posted it ;) )
Moving the following lines to the end of the run()-method of the new Thread should do what you want to.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.           ArrayAdapter<String> directoryList = new ArrayAdapter<String>(
  2.                     ProgressDialogTest.this, R.layout.file_row,
  3.                     directoryEntries);
  4.           setListAdapter(directoryList);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

Perhaps you have to place a "ProgressDialogTest.this." before setListAdapter(...);

Tell us if it solve your problem :).

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby nico » Sun Jan 13, 2008 2:33 pm

hi plusminus,

i did that before but it returned error.

i was thinking of using thread synchronizing, but didn't work, i never good with that synchronized stuff :D :P

any idea?
Attachments
errorr.JPG
errorr.JPG (13.24 KiB) Viewed 4374 times
nico
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Nov 27, 2007 10:33 am
Location: Indonesia

Postby plusminus » Sun Jan 13, 2008 3:08 pm

Hello nico,

aaawww of course :D forgot that point.

:idea: Solution: Taken from the :src: PizzaTimer-Tutorial: (will be really doable to adopt for your problem. Important is that you get the idea behind :!: ):
:idea: The "problem" is, that for security reasons, only the Thread that created the View is allowed to do sth. with it, like redrawing it with an invalidate();-call. So we need an Object 'in' the GUI-Thread that receives messages that mean like: "Hey 'Main-Thread' update your gui!". We will use a so called Handler for that.

  • There will be a Thread, that sends a message that means "UPDATEYOURGUI" to the Handler.
  • In the GUI-Thread, when the Handler receives the Message("UPDATEYOURGUI") it causes an invalidate(); to all participating Views.
Image

:arrow: If this is still not clear, feel free to ask your question :!:


See the :src: PizzaTimer-Tutorial for actual code :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby kaobiore » Sun Jan 13, 2008 3:13 pm

Hi!

I had exactly the same problem but I guess I've found a solution for it here :wink:

So, just send a "message" out of the created thead (process-dialog) to the (let's call it) main-thread (through a handler).

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     protected static final int GUIUPDATEIDENTIFIER = 0x101;
  2.  
  3.     Handler myViewUpdateHandler = new Handler(){
  4.         // @Override
  5.         public void handleMessage(Message msg) {
  6.              switch (msg.what) {
  7.                   case MainClass.GUIUPDATEIDENTIFIER:
  8.                        // Do some GUI Stuff here
  9.                        break;
  10.              }
  11.              super.handleMessage(msg);
  12.         }
  13.    };    
  14.  
  15.     private void somethingToWaitFor() {
  16.        this.myProgressDialog = ProgressDialog.show(MainClass.this,
  17.                 "Please wait", "...", true);
  18.        
  19.        new Thread() {
  20.             public void run() {
  21.                 try {
  22.                                         Thread.sleep(1500);
  23.                                 } catch (InterruptedException e) {
  24.                                         // nothing
  25.                                 }
  26.                                 // Close Progress-Dialog
  27.                 MainClass.this.myProgressDialog.dismiss();
  28.                
  29.                 // Send message to main-thread - update gui
  30.                 Message m = new Message();
  31.                 m.what = MainClass.GUIUPDATEIDENTIFIER;
  32.                 MainClass.this.myViewUpdateHandler.sendMessage(m);
  33.             }
  34.        }.start();
  35.     }
  36.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


I don't know if there might be a better solution but it works fine for me :D

Cheers
-kao-
kaobiore
Freshman
Freshman
 
Posts: 8
Joined: Sat Dec 29, 2007 11:21 am
Location: Vienna / Austria

Postby plusminus » Sun Jan 13, 2008 3:20 pm

:lol: two answers at the same time, I was a bit faster :P

But thanks for your response @kaobiore.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby nico » Sun Jan 13, 2008 5:35 pm

plusminus wrote::lol: two answers at the same time, I was a bit faster :P

But thanks for your response @kaobiore.

Regards,
plusminus


i should be the one who thanks you both, it works! and i understand now, thankyou very much :wink:
i really love this forum 8) :wink:
nico
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Nov 27, 2007 10:33 am
Location: Indonesia

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 12 guests