在getview两个异步调用(),我在做这个吧?我在、两个、getview

2023-09-07 23:51:28 作者:情痞

所以,我有一个自定义GalleryAdapter,我流从网页中的图像与通过Ajax调用异步ImageLoading库(LIB名字是Aquery)

随着图像的,我从网络流,我有一个相应的音频轨道也与每一个形象,因此有被异步调用时加载图像的同时

即。

  

图像文件 - > http://myserver.com/img1.png

     

声轨 - > http://myserver.com/img1.mp3

点号1,我做我的自定义里面这整件事 ImageAdapter 。要我打电话 从我的 getView 方法中的另一个线程?

我在做什么 - >的 getView()方法获取 所谓的 2次我可以控制它被调用一次,因为它不应该因为赛道被打得多 根据时间上的电话吗?

这是我的自定义适配器类

 私有类MainImageAdapter扩展ArrayAdapter<字符串> {

        名单<字符串> pagedImages;
        私人上下文的背景下;

        公共MainImageAdapter(上下文的背景下,INT渣油,列表和LT;字符串>对象){
            超(背景下,渣油,对象);
            this.context =背景;
            this.pagedImages =物体;
        }


           @覆盖
            公众诠释getCount将(){
                // TODO自动生成方法存根
                返回pagedImages.size();
            }

            @覆盖
            公共字符串的getItem(INT位置){
                // TODO自动生成方法存根
                返回pagedImages.get(位置);
            }

            @覆盖
            众长getItemId(INT位置){
                // TODO自动生成方法存根
                返回pagedImages.get(位置).hash code();
            }

        @燮pressWarnings(德precation)
        公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){

            视图V;
            如果(convertView == NULL){
                V = LayoutInflater.from(上下文).inflate(R.layout.imagebook,父母,假);
            } 其他 {
                V = convertView;
            }

            的System.out.println(getview调用+位置);

            最后ImageView的IM =(ImageView的)v.findViewById(R.id.pagedimage);
            positionMusic =位置;

            aq.id(IM)在图像配(pagedImages.get(位置),真正的,真实的,0,0,新BitmapAjaxCallback(){
                @覆盖
                公共无效回调(字符串URL,ImageView的第四位图BM,AjaxStatus状态){
                        iv.setImageBitmap(BM);

                        //这应该只调用一次不知何故
                        MPLAYER = MediaPlayer.create(MainActivity.this,Uri.parse(pagedImages.get(位置).replace(PNG,.MP3)));
                        mPlayer.start();
                }
            });


            返回伏;
        }
    }
 

解决方案   

在getview两个异步调用(),我在做这个吧?

Spring 中基于 async 注解实现方法的异步调用

没有。

  

我做这件事在我的自定义ImageAdapter。我去找另一个线程从我getView方法?

Aquery开始另一个线程来完成下载,所以你只能复制code,他们已经。 BitmapAjaxCallback.callback 当线程完成做其工作时调用。

  

在getView()方法被调用2次我可以控制它被调用一次,因为它不应该作为跟踪获取基于其呼叫玩过多次?

getView 已被调用两次。首先调用将返回查看无需从网络上下载的图像。第二呼叫是后的图像被下载,并且可以附加。只有当你的形象已在内存中缓存 getView 将被调用一次。你不想要的是 BitmapAjaxCallback.callback 叫了两声。

另外要在主线程下载MP3后的图像完成下载。他们都应该下载在单独的线程。同时,

解决方案主张:

正如你适配器势必活动背景和图像和MP3的下载和开始播放不应该束缚该shortliving情况下,将其移动到应用程序上下文或服务适配器唯一的请求的事情要做,并从缓存中,而应放在应用程序情况下获取图像。也许Aquery有一定的缓存就可以使用。

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

解决方案

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.

 
精彩推荐
图片推荐