Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

android - 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" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);            
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...