Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressBar while loading ListView (using AsyncTask)

I'm trying to show a progress bar when loading a custom ListView and afterwards - hide it. I'm using ASync task, but for some reason - The content view is not set and the previous layout view is stuck until all the list view is 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" />
like image 371
ohadinho Avatar asked Sep 05 '12 11:09

ohadinho


2 Answers

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);            
        }
    }
like image 141
Dilip Avatar answered Nov 15 '22 11:11

Dilip


if your using asnkTask that is work on main Thread so application is much more load.so better solution is using Service.class. downlaod this example Check this link all are solution are there and download it: you have to also get list from relaunch this application. https://github.com/PankajSavaliyaGit/Upload-List

like image 36
Pankaj Savaliya Avatar answered Nov 15 '22 12:11

Pankaj Savaliya