如何在onPreExecute()中从AsyncTask渲染ArrayList <imageview>?</imageview>

时间:2013-03-07 04:30:01

标签: android json arraylist android-asynctask android-imageview

我是新手,也是Android开发的新手。

我正在尝试使用ViewPager制作幻灯片库。

这是我的主要活动:

public class HomeActivity extends AbstractActivity {

    ArrayList<ImageView> imagens = new ArrayList<ImageView>();
    ViewPager slideshow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        slideshow = (ViewPager) findViewById(R.id.slideshow);

        // inicia a task json
        new aTask(this).execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }

    public void getResult(String result) {
        Gson gson = new Gson();
        Home home = gson.fromJson(result, Home.class);

        imagens = new ArrayList<ImageView>();

        for (Webdoor webdoor : home.webdoors) {
            try {
                ImageView img = new ImageView(getApplicationContext());
                Bitmap bitmap = BitmapFactory
                        .decodeStream((InputStream) new URL(webdoor.imagem)
                                .getContent());
                img.setImageBitmap(bitmap);

                imagens.add(img);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private class aTask extends AsyncTask<Void, Void, ArrayList<ImageView>> {

        private HomeActivity homeActivityRef;

        public aTask(HomeActivity homeActivity) {
            this.homeActivityRef = homeActivity;
        }

        // inicio da task
        @Override
        protected void onPreExecute() {
        }

        // execução da tarefa em background
        @Override
        protected ArrayList<ImageView> doInBackground(Void... params) {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(
                    new StringHttpMessageConverter());
            String result = restTemplate.getForObject(
                    "http://api.modahub.com.br/home", String.class,
                    "SpringSource");

            Gson gson = new Gson();
            Home home = gson.fromJson(result, Home.class);

            ArrayList<ImageView> imagens = new ArrayList<ImageView>();

            for (Webdoor webdoor : home.webdoors) {
                try {
                    ImageView img = new ImageView(getApplicationContext());
                    Bitmap bitmap = BitmapFactory
                            .decodeStream((InputStream) new URL(webdoor.imagem)
                                    .getContent());
                    img.setImageBitmap(bitmap);

                    imagens.add(img);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            publishProgress(); // invoca o onProgressUpdate()
            return imagens;
        }

        // callback invocado pelo publishProgress()
        @Override
        protected void onProgressUpdate(Void... values) {
        }

        // callback executado no final da task
        @Override
        protected void onPostExecute(ArrayList<ImageView> aImagens) {
            // This variable aImagens is full of images!
            // How I put this on the screen =(
        }
    }
}

如何在屏幕中呈现ImageView中的onPostExecute()列表? 我需要Fragment还是Loader

1 个答案:

答案 0 :(得分:1)

如果您想在ViewPager.than中制作ImageGallery 所有进入ImageView的图像都绑定在一个适配器中,您可以在View寻呼机中绑定适配器,请参阅link