Android progress dialog while getting data from sqlite
I tried to implement threads, asynctask from examples on the we开发者_运维技巧b but none of them run the way I want.
I have a class to send data to a web server. It contains 3 tasks: -grab an id -get data from sql and build a json file -send the json file
I'd like to implement a progress dialog or progressbar to show progress to the user waiting.
The progressdialog I last tested to show data grabbing from sqlite was based on Progress Bar Example. Trouble is that when I try to generate my file more than 2 times, the thread is not starting...
So whats's the best to be done in my case ? I have a spinner to select what to send and a button to send.
When button is clicked, basically I have:
HttpResponse response = GetChantier(commentaire);
//Checking response
if( response != null)
{
String _response=EntityUtils.toString(response.getEntity());
int chantier_serveur = Integer.parseInt(_response.replaceAll("[\n\r]", ""))
String fichier = DonneesToJson(db,chantier, chantier_serveur);
HttpResponse response = SendJson ( chantier, fichier);
}
I'd need to implement a progress for DonneesToJson and SendJson.
DonnesToJson grabs a cursor then build a json file while iterating the cursor. SendJson is an HttpPost that sends a file and 2 fields.
I'm really new to java programming and threads. Any help appreciated.
Regards.
Use Async task for doing Background task as follows.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Asyn_Task().execute("name","title");
}
class Asyn_Task extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(Facebook_Post_View.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected Void doInBackground(String... param) {
// TODO Auto-generated method stub
name=params[0]; //Like
title=params[1];
// Do your all Stuffs
return null;
}
@Override
protected void onPostExecute(Void result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
In these at position 0 name will available and at position 1 title will available.Similarly you can pass values as you like..
For further reference check this Async Task
精彩评论