Two async calls in getview(), am I doing this right?

五迷三道 提交于 2019-12-11 09:53:35

问题


So, I have a custom GalleryAdapter where I stream images from web with the help of an Async ImageLoading Library via ajax calls (lib name is Aquery).

With the images those I stream from web, I have a corresponding audio track also, with every image and hence that has to be called asynchronously when the image is loaded same time.

i.e.

image file -> http://myserver.com/img1.png

sound track -> http://myserver.com/img1.mp3

  • Point number 1 , I do this whole thing inside my custom ImageAdapter. Shall I call another thread from within my getView method ?

  • What I am doing -> The getView() method gets called 2 times can I control it to get called only once because it shouldn't as the track gets played multiple times based on its call?

This is my Custom Adapter class

private class MainImageAdapter extends ArrayAdapter<String> {

        List<String> pagedImages;
        private Context context;

        public MainImageAdapter(Context context,int resid, List<String> objects ) {
            super( context, resid, objects );
            this.context=context;
            this.pagedImages=objects;
        }


           @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return pagedImages.size();
            }

            @Override
            public String getItem(int position) {
                // TODO Auto-generated method stub
                return pagedImages.get(position);
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return pagedImages.get(position).hashCode();
            }

        @SuppressWarnings("deprecation")
        public View getView(final int position, View convertView, ViewGroup parent) {

            View v;
            if ( convertView == null ) {
                v = LayoutInflater.from(context).inflate(R.layout.imagebook, parent, false);
            } else {
                v = convertView;
            } 

            System.out.println("getview called with"+position);

            final ImageView im=(ImageView) v.findViewById(R.id.pagedimage);
            positionMusic=position;

            aq.id(im).image(pagedImages.get(position), true, true, 0, 0, new BitmapAjaxCallback(){
                @Override
                public void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status){
                        iv.setImageBitmap(bm);

                        //This should be called only once somehow
                        mPlayer = MediaPlayer.create(MainActivity.this, Uri.parse(pagedImages.get(position).replace(".png", ".mp3")));
                        mPlayer.start();
                }
            });


            return v;
        }
    }

回答1:


Two async calls in getview(), am I doing this right?

No.

I do this whole thing inside my custom ImageAdapter. Shall I call another thread from within my getView method ?

Aquery is starting another thread to do the download, so you would only duplicate the code they have already. BitmapAjaxCallback.callback is called when that thread finishes doing its job.

The getView() method gets called 2 times can I control it to get called only once because it shouldn't as the track gets played multiple times based on its call?

getView has to be called twice. First call will return View without an image downloaded from the network. Second call is after the image is downloaded and can be attached. Only if you have the image in memory cache already getView will be called once. What you don't want is BitmapAjaxCallback.callback called twice.

Also you are downloading mp3 on main thread after image has finished downloading. They should both be downloaded on separate threads. And simultaneously.

Solution proposition:

As you Adapter is bound to Activity context and downloading of image and mp3 and starting playback should not be bound to that shortliving context, move it to Application context or Service and from Adapter only request things to be done and get image from a cache, which should be put in Application context. Maybe Aquery has some cache you can use.



来源:https://stackoverflow.com/questions/16456812/two-async-calls-in-getview-am-i-doing-this-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!