所以我有一个主Activity,我有一个按钮(ImageView)和一个进度条。 当我单击该按钮时,我想执行一系列从远程数据库下载一些数据的AsyncTasks。我想在单击按钮时启动不确定的进度条,并在最后一次AsyncTask完成时隐藏它。我能这样做吗?
所以我有
我不能在活动中包含所有asyncTask类,所以我可以直接引用progressBar,那么如何从onPostExecute中的最后一个AsyncTask类访问进度条以隐藏它?
所以我的mainActivity看起来像:
ImageView upd = (ImageView) findViewById(R.id.iv_update);
upd.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
new AsyncTask1(getApplicationContext()).execute();
new AsyncTask2(getApplicationContext()).execute();
new AsyncTask3(getApplicationContext()).execute();
new AsyncTask4(getApplicationContext()).execute();
new AsyncTask5(getApplicationContext()).execute();
new AsyncTask6(getApplicationContext()).execute();
new AsyncTask7(getApplicationContext()).execute();
}
});
AsyncTask的一个例子(它们都相似)
public class AsyncTask1 extends AsyncTask<String, Integer, Double> {
private Context mContext;
InputStream is = null ;
String result = "";
public AsyncTask1 (Context context){
mContext = context;
}
public void insSQLite(Integer iduzer, String tip_uzer, String nume, String parola, String numecomplet,
String email, String telefon, String idgrup, String den_grup, String idcoord, String nume_coord, String nume_coordfull ) {
SQLiteDatabase db = new myDbHelper(mContext).getWritableDatabase();
String sql = "";
sql = "select iduzer from uzeri where trim(tip_uzer)='"+tip_uzer+"' and trim(numecomplet)='"+numecomplet+"'";
Cursor cursor = db.rawQuery(sql, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
Log.e("... It exists ...", Integer.toString(cursor.getInt(0)));
sql = "update uzeri set iduzer=" + iduzer + ",parola='" + parola + "',email='" + email + "',telefon='" + telefon + "', idgrup=" + idgrup + ", den_grup='" + den_grup + "', idcoord=" + idcoord + ", nume_coord='" + nume_coord + "', nume_coordfull='" + nume_coordfull + "' where iduzer=" + Integer.toString(cursor.getInt(0));
db.execSQL(sql);
}
}else{
sql = "insert into uzeri (iduzer,tip_uzer,nume,parola,numecomplet,email,telefon, idgrup,den_grup,idcoord,nume_coord,nume_coordfull) values (" +
String.valueOf(iduzer) + ",'" + tip_uzer + "','" + nume + "','" + parola + "','" + numecomplet + "','" +
email + "','" + telefon + "'," + idgrup + ",'" + den_grup +"'," + idcoord + ",'" + nume_coord + "','" + nume_coordfull + "')";
db.execSQL(sql);
}
db.close();
}
@Override
protected Double doInBackground(String... params) {
postData("0");
return null;
}
protected void onPostExecute(Double v){
if (!String.valueOf(result).trim().equals("null")) {
try {
JSONArray Jarray = new JSONArray(result);
for (int i = 0; i < Jarray.length(); i++) {
JSONObject Jasonobject = null;
Jasonobject = Jarray.getJSONObject(i);
Integer iduzer = Jasonobject.getInt("iduser");
String tip_uzer = Jasonobject.getString("tip_uzer");
String nume = Jasonobject.getString("name");
String pass = Jasonobject.getString("pass");
String numecomplet = Jasonobject.getString("namecomplete");
String email = Jasonobject.getString("email");
String telefon = Jasonobject.getString("phone");
String idgrup = Jasonobject.getString("idgroup");
String den_grup = Jasonobject.getString("den_group");
String idcoord = Jasonobject.getString("idcoord");
String nume_coord = Jasonobject.getString("nume_coord");
String nume_coordfull = Jasonobject.getString("name_coordfull");
insSQLite(iduzer, tip_uzer, nume, pass, namecomplete, email, telefon, idgrup,den_grup,idcoord,nume_coord,nume_coordfull);
}
} catch (Exception e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
}else{
Log.e("ASYNC UZ:","There are no users on the server");
}
}
protected void onProgressUpdate(Integer... progress){
// pb.setProgress(progress[0]);
}
public void postData(String valueIWantToSend) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myserver.com/getusers.php");
final GlobalClass globalVariable = (GlobalClass) mContext;
String uzid = globalVariable.getUzer();
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
nameValuePairs.add(new BasicNameValuePair("uzeru", uzid));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity httpEntity = response.getEntity();
is = httpEntity.getContent();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){ sb.append(line+"\n"); }
is.close();
result=sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
}
谢谢
答案 0 :(得分:0)
我会与你分享一个想法。您可以设置如下变量:
int taskCount = 0;
然后,在每个Asynctask的onPostExecute
方法中,您可以添加taskCount
并测试taskCount
是否获得7值:
taskCount++; //WARN: here you maybe need java synchronized.
if(taskCount == 7){
//all task is done , just do your work.
//hide the progressbar.
}
希望这个想法可以给你一些提示。