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.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
adapter = dbInit();
button = (Button)findViewById(R.id.progressDialog);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
showDialog(PROGRESS_DIALOG);
}
});
}finally{
adapter.close();
}
}
protected Dialog onCreateDialog(int id){
switch(id){
case PROGRESS_DIALOG:
progressDialog = new ProgressDialog(ProgressBarSample.this);
progressDialog.setMessage("loading message...");
thread = new MyThread(handler);
thread.start();
return progressDialog;
default:
return null;
}
}
private MyDbAdapter dbInit(){
adapter = new MyDbAdapter(this);
adapter.open();
adapter.deleteMyTable();
adapter.createMyTable();
return adapter;
}
final Handler handler = new Handler(){
public void handleMessage(Message msg){
int status = msg.getData().getInt("status");
if (status >= 0){
progressDialog.setMessage("Downloading data part2...");
dismissDialog(PROGRESS_DIALOG);
}
}
};
public class MyThread extends Thread{
private Handler myHandler;
int downloadStatus;
final String dUrl = MY_XML_URL;
public MyThread(Handler handler){
this.myHandler = handler;
downloadStatus = -1;
}
public void run(){
Message msg = myHandler.obtainMessage();
Bundle b = new Bundle();
downloadStatus = downloadTables(dUrl);
b.putInt("status", downloadStatus);
msg.setData(b);
myHandler.sendMessage(msg);
}
public int downloadTables(String xmlUrl){
int downloadStatus = 0;
try{
URL url = new URL(xmlUrl);
SAXParserFactory factory = SAXParserFactory.newInstance
();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
MyHandler handler = new MyHandler(adapter);
reader.setContentHandler(handler);
reader.parse(new InputSource(url.openStream()));
}catch(Exception e){
Log.e("Error: ", "" + e.getMessage());
System.exit(0);
}finally{
downloadStatus = 1;
}
return downloadStatus;
}
}
}
but when I run this i get the following error.
I/ActivityManager( 57): Starting activity: Intent
{ act=android.intent.action.MAIN cat=
[android.intent.category.LAUNCHER] flg=0x10200000
cmp=com.achie.test/.ProgressBarSample }
I/ActivityManager( 57): Start proc com.achie.test for activity
com.achie.test/.ProgressBarSample: pid=548 uid=10024 gids={3003}
D/ddm-heap( 548): Got feature list request
I/ActivityManager( 57): Displayed activity
com.achie.test/.ProgressBarSample: 1235 ms (total 1235 ms)
E/Error: ( 548): database not open
I/AndroidRuntime( 548): AndroidRuntime onExit calling exit(0)
I/WindowManager( 57): WIN DEATH: Window{43be4df0 com.achie.test/
com.achie.test.ProgressBarSample paused=false}
I/ActivityManager( 57): Process com.achie.test (pid 548) has died.
I/WindowManager( 57): WIN DEATH: Window{43c94128 Downloading Data
paused=false}
W/UsageStats( 57): Unexpected resume of com.android.launcher while
already resumed in com.achie.test
W/InputManagerService( 57): Got RemoteException sending setActive
(false) notification to pid 548 uid 10024
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,
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en