Search code examples
androidandroid-asynctaskviewflipper

Fill ViewFlipper in AsyncTask


I am trying to load images into my FlipView using AsyncTask but Im not sure how to continue. I need to use AsyncTask because the application is force closing since I have 20 images to load from the drawables.

private class asyncImage extends AsyncTask<Void, Void, Void>{
        int i;
        @Override
        protected Void doInBackground(Void... params) {
            for (i=0;i<imageID.length;i++){
                image = new ImageView(getBaseContext());
                image.setId(i);
                image.setImageResource(imageID[i]);
                image.setBackgroundDrawable(getResources().getDrawable(R.drawable.border));
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                FlipV.addView(image);
            }
            return null;
        }

    protected void onPostExecute(Void result){

        image.setBackgroundDrawable(getResources().getDrawable(R.drawable.border));
        image.setScaleType(ImageView.ScaleType.FIT_XY);
        for(int x=0;x<imageID.length;x++){
            FlipV.addView(???); //How to add images into FlipView?
        }
    }

}

Im stuck in adding the images to the FlipView. Any help?


Solution

  • As I have mentioned from my comment I've already solved my problem by using an imageview array instead ang loading the images in the doInBackground and adding the imageviews in the viewflipper in the postExecute. I've also added a progress dialog to show a loading time instead of a blank screen in my app. Thanks to everyone that replied.

    private class asyncImage extends AsyncTask<Void, Void, Void>{
            int i;
            @Override
            protected Void doInBackground(Void... params) {
                for (i=0;i<imageID.length;i++){
                    image[i] = new ImageView(getBaseContext());
                    image[i].setImageResource(imageID[i]);
                    image[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.border));
                    image[i].setScaleType(ImageView.ScaleType.FIT_XY);
                }
                return null;
            }
    
            protected void onPostExecute(Void result){
                for (int x=0;x<imageID.length;x++){
                FlipV.addView(image[x]);
                }
                count = 1;
                progDialog.dismiss();
                txItem.setText(Integer.toString(count) + "/" + (imageID.length));
            }
        }