ProgressBar when loading a ListView (using AsyncTask)

I try to show a progress bar when loading a custom ListView, and then hide it. I am using the ASync task, but for some reason - the content view is not installed, and the previous layout view gets stuck until all the contents of the list are loaded.

Here is my code:

private ListView listViewGameResults; protected View dialogLayout; protected ArrayList<Game> listGames; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.adresults); GameResultsLoader gameResultsLoader = new GameResultsLoader(); gameResultsLoader.execute(); } private class GameResultsLoader extends AsyncTask<Void, Void, Void> { private GameResultsAdapter adapter; public GameResultsLoader() { } @Override protected void onPreExecute() { } @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); pb.setVisibility(View.GONE); } } 

ProgressBar and ListView in my layout:

 <ProgressBar style="?android:attr/progressBarStyle" android:layout_centerInParent="true" android:id="@+id/progressbar_loading" 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" /> 
+4
source share
3 answers

You need to set the default visibility 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); } } 
+4
source

if you use asnkTask, which works in the main thread, so the application is much more load.so the best solution uses Service.class. downlaod this example Check this link, everyone has a solution, and download it: you should also get a list from renewing this application. https://github.com/PankajSavaliyaGit/Upload-List

+1
source

you need to write code to run progressDialog in the preExecute method and release it in postExecute.

 @Override protected void onPreExecute() { proDialog = new ProgressDialog(context); proDialog.setTitle("App name"); proDialog.setMessage("Loding..."); proDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //proDialog.setIcon(R.drawable.) proDialog.setCancelable(true); proDialog.show(); } @Override protected void onPostExecute(Void res) { proDialog.dismiss(); } 
0
source

Source: https://habr.com/ru/post/1432464/


All Articles