You need to set default visibility of progressBar
gone.and onPreExecute()
set Visible and onPostExecute()
set gone.
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_centerInParent="true"
android:id="@+id/progressbar_loading"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listViewGameResults"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dip"
android:layout_below="@+id/upperstrip"
android:layout_above="@+id/ivDownStrip" />
your Activity should look like this
public class demo extends Activity{
private ListView listViewGameResults;
protected View dialogLayout;
protected ArrayList<Game> listGames;
progressBar progress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adresults);
progress=(ProgressBar)findViewByid(R.id.progressbar_loading);
GameResultsLoader gameResultsLoader = new GameResultsLoader(this);
gameResultsLoader.execute();
}
}
Use one separate class for AsyncTask
public class GameResultsLoader extends AsyncTask<Void, Void, Void> {
private GameResultsAdapter adapter;
Demo demo;
public GameResultsLoader(Demo demo) {
this.demo=demo;
}
@Override
protected void onPreExecute() {
demo.progress.setvisibility(View.Visible);
}
@Override
protected Void doInBackground(Void... params) {
try {
listGames = GameResultsCache.getInstance().getGameResults();
adapter = new GameResultsAdapter(getBaseContext(), listGames);
listViewGameResults = (ListView)findViewById(R.id.listViewGameResults);
}
catch (Exception e) {
// TODO Auto-generated catch block
finish();
}
return null;
}
@Override
protected void onPostExecute(Void res) {
listViewGameResults.setAdapter(adapter);
listViewGameResults.setDivider(null);
listViewGameResults.setDividerHeight(0);
ProgressBar pb = (ProgressBar)findViewById(R.id.progressbar_loading);
demo.progress.setVisibility(View.GONE);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…