我正在使用异步任务在我的列表视图中重新加载数据,并在我的活动中设置进度条以显示它正在加载。但问题是,如果我开始一个新活动并返回并且没有完成加载,则不会显示进度条。如何设置进度条以继续加载?我忘了提及,在我返回活动后,异步任务也没有刷新我的列表视图。这是我正在使用的代码:
public class MyCollectionsTask extends AsyncTask <Context, Integer, Void> {
@Override
protected Void doInBackground(Context... arrContext) {
JsonCollection jsonColl = new JsonCollection();
Log.e("","DO IN BACKGROUND");
for(int i=0; i<arrContext.length; i++) {
jsonColl.executeInsert(arrContext[i]);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
Log.e("","ON PROGRESS UPDATE");
for (int i=0; i<progress.length; i++){
progressBar.setProgress(i);
}
}
@Override
protected void onCancelled() {
Log.e("","ON CANCELLED");
progressBar.setVisibility(View.GONE);
sync.setImageResource(R.drawable.sync_icon2);
}
@Override
protected void onPreExecute()
{
Log.e("","ON PRE EXECUTE");
}
@Override
protected void onPostExecute(Void v) {
Log.e("","ON POST EXECUTE");
SystemDatabaseHelper newdbHelper = new SystemDatabaseHelper(MyCollectionList.this, null, 1);
newdbHelper.initialize(MyCollectionList.this);
String genresSQLforID = "SELECT DISTINCT g.genre_id AS genreID, gl.title AS Title " +
"FROM genres AS g, genre_lang AS gl " +
"WHERE gl.genre_id = g.genre_id AND gl.lang_code ='" + langCode + "'"+" ORDER BY gl.title";
Cursor genresCursor = newdbHelper.executeSQLQuery(genresSQLforID);
if (genresCursor.getCount() == 0) {
genresCursor.close();
} else {
genresCursor.moveToFirst();
GenID = Integer.parseInt(genresCursor.getString(genresCursor.getColumnIndex("genreID")));
}
getCollectionsForGenre(GenID);
final SharedPreferences isLogged = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = isLogged.edit();
editor.putBoolean("getProgBarCollection", false);
editor.commit();
progressBar.setVisibility(View.GONE);
sync.setImageResource(R.drawable.sync_icon2);
}
}
答案 0 :(得分:1)
当你从另一个活动返回时,它只是恢复活动,它不会进入onCreate方法()。您需要做的是使用 startActivityForResult()从asyntask活动中启动另一个活动,并覆盖asyntask活动中的onActivityResult方法。 并在 onActivityResult()中再次执行asyntask。你做得更好,而不是在Onresume中调用asyntask,因为在其他情况下它也会像Onresume一样进入Onresume,就像按下home键再去一次