Progress Dialog pass context or adapter to thread

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Progress Dialog pass context or adapter to thread

Postby achie1266 » Thu Nov 19, 2009 1:10 am

I am trying to display a progress bar while download data and store it in my database.
My problem is that I need to pass the database adapter or the context to the thread.

This is what I have tried.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.achie.test;
  3.  
  4.  
  5.  
  6. import java.net.URL;
  7.  
  8.  
  9.  
  10. import javax.xml.parsers.SAXParser;
  11.  
  12. import javax.xml.parsers.SAXParserFactory;
  13.  
  14.  
  15.  
  16. import org.xml.sax.InputSource;
  17.  
  18. import org.xml.sax.XMLReader;
  19.  
  20.  
  21.  
  22. import android.app.Activity;
  23.  
  24. import android.app.Dialog;
  25.  
  26. import android.app.ProgressDialog;
  27.  
  28. import android.os.Bundle;
  29.  
  30. import android.os.Handler;
  31.  
  32. import android.os.Message;
  33.  
  34. import android.util.Log;
  35.  
  36. import android.view.View;
  37.  
  38. import android.view.View.OnClickListener;
  39.  
  40. import android.widget.Button;
  41.  
  42.  
  43.  
  44. public class ProgressBarSample extends Activity{
  45.  
  46.        
  47.  
  48.         static final int PROGRESS_DIALOG = 0;
  49.  
  50.         Button button;
  51.  
  52.         ProgressDialog progressDialog;
  53.  
  54.         MyThread thread;
  55.  
  56.        
  57.  
  58.         MyDbAdapter adapter;
  59.  
  60.        
  61.  
  62.     /** Called when the activity is first created. */
  63.  
  64.     @Override
  65.  
  66.     public void onCreate(Bundle savedInstanceState) {
  67.  
  68.         super.onCreate(savedInstanceState);
  69.  
  70.         setContentView(R.layout.main);
  71.  
  72.         try{
  73.  
  74.         adapter = dbInit();
  75.  
  76.         button = (Button)findViewById(R.id.progressDialog);
  77.  
  78.         button.setOnClickListener(new OnClickListener(){
  79.  
  80.                 public void onClick(View v){
  81.  
  82.                         showDialog(PROGRESS_DIALOG);
  83.  
  84.                 }
  85.  
  86.         });
  87.  
  88.         }finally{
  89.  
  90.                 adapter.close();
  91.  
  92.         }
  93.  
  94.     }
  95.  
  96.    
  97.  
  98.     protected Dialog onCreateDialog(int id){
  99.  
  100.         switch(id){
  101.  
  102.         case PROGRESS_DIALOG:
  103.  
  104.                 progressDialog = new ProgressDialog(ProgressBarSample.this);
  105.  
  106.                 progressDialog.setMessage("loading message...");
  107.  
  108.                
  109.  
  110.                 thread = new MyThread(handler);
  111.  
  112.                 thread.start();
  113.  
  114.                
  115.  
  116.                 return progressDialog;
  117.  
  118.         default:
  119.  
  120.                 return null;
  121.  
  122.         }
  123.  
  124.     }
  125.  
  126.    
  127.  
  128.     private MyDbAdapter dbInit(){
  129.  
  130.         adapter = new MyDbAdapter(this);
  131.  
  132.         adapter.open();
  133.  
  134.         adapter.deleteMyTable();
  135.  
  136.         adapter.createMyTable();
  137.  
  138.         return adapter;
  139.  
  140.     }
  141.  
  142.    
  143.  
  144.     final Handler handler = new Handler(){
  145.  
  146.         public void handleMessage(Message msg){
  147.  
  148.                 int status = msg.getData().getInt("status");
  149.  
  150.                 if (status >= 0){
  151.  
  152.                         progressDialog.setMessage("Downloading data part2...");
  153.  
  154.                         dismissDialog(PROGRESS_DIALOG);
  155.  
  156.                 }
  157.  
  158.         }
  159.  
  160.        
  161.  
  162.     };
  163.  
  164.    
  165.  
  166.     public class MyThread extends Thread{
  167.  
  168.         private Handler myHandler;
  169.  
  170.         int downloadStatus;
  171.  
  172.         final String dUrl = MY_XML_URL;
  173.  
  174.        
  175.  
  176.         public MyThread(Handler handler){
  177.  
  178.                 this.myHandler = handler;
  179.  
  180.                 downloadStatus = -1;
  181.  
  182.         }
  183.  
  184.        
  185.  
  186.         public void run(){
  187.  
  188.                 Message msg = myHandler.obtainMessage();
  189.  
  190.                 Bundle b = new Bundle();
  191.  
  192.                 downloadStatus = downloadTables(dUrl);
  193.  
  194.                         b.putInt("status", downloadStatus);
  195.  
  196.                         msg.setData(b);
  197.  
  198.                         myHandler.sendMessage(msg);
  199.  
  200.         }
  201.  
  202.  
  203.  
  204.         public int downloadTables(String xmlUrl){
  205.  
  206.                 int downloadStatus = 0;
  207.  
  208.                 try{
  209.  
  210.                         URL url = new URL(xmlUrl);
  211.  
  212.                         SAXParserFactory factory = SAXParserFactory.newInstance();
  213.  
  214.                         SAXParser parser = factory.newSAXParser();
  215.  
  216.                         XMLReader reader = parser.getXMLReader();
  217.  
  218.                         MyHandler handler = new MyHandler(adapter);
  219.  
  220.                         reader.setContentHandler(handler);
  221.  
  222.                         reader.parse(new InputSource(url.openStream()));
  223.  
  224.                 }catch(Exception e){
  225.  
  226.                         Log.e("Error: ", "" + e.getMessage());
  227.  
  228.                         System.exit(0);
  229.  
  230.                 }finally{
  231.  
  232.                         downloadStatus = 1;
  233.  
  234.                 }
  235.  
  236.                 return downloadStatus;
  237.  
  238.         }
  239.  
  240.        
  241.  
  242.     }
  243.  
  244. }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


but when I run this i get the following error.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. I/ActivityManager(   57): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.achie.test/.ProgressBarSample }
  3.  
  4. I/ActivityManager(   57): Start proc com.achie.test for activity com.achie.test/.ProgressBarSample: pid=548 uid=10024 gids={3003}
  5.  
  6. D/ddm-heap(  548): Got feature list request
  7.  
  8. I/ActivityManager(   57): Displayed activity com.achie.test/.ProgressBarSample: 1235 ms (total 1235 ms)
  9.  
  10. E/Error:  (  548): database not open
  11.  
  12. I/AndroidRuntime(  548): AndroidRuntime onExit calling exit(0)
  13.  
  14. I/WindowManager(   57): WIN DEATH: Window{43be4df0 com.achie.test/com.achie.test.ProgressBarSample paused=false}
  15.  
  16. I/ActivityManager(   57): Process com.achie.test (pid 548) has died.
  17.  
  18. I/WindowManager(   57): WIN DEATH: Window{43c94128 Downloading Data paused=false}
  19.  
  20. W/UsageStats(   57): Unexpected resume of com.android.launcher while already resumed in com.achie.test
  21.  
  22. W/InputManagerService(   57): Got RemoteException sending setActive(false) notification to pid 548 uid 10024
  23.  
  24.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


It looks like i cannot pass the adapter from the thread as I am doing as its running as a separate thread and have to use handler to pass it. Can anyone let me know how to do this.

thank you,
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 9 guests