ListView项目不从URL加载图像

时间:2015-04-26 03:12:15

标签: android listview android-listview

我有一个自定义BaseAdapter,它使用异步任务将图像加载到来自网址的自定义列表视图中,但图像未显示。这是适配器:

private class HorizontalAdapter extends BaseAdapter{

        public HorizontalAdapter() {
            super();
            final Checkpoint current = localCheckpoints[checkpointPosition];
            urls[0] = current.getGallery()[0].getUrl();
            urls[1] = current.getYoutube();
            urls[2] = current.getSoundcloud();
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Object getItem(int position) {
            return urls[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            String urlToLoad = urls[position];

            if(convertView == null){
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.horizontal_listview_item,parent,false);
            }

            ImageView imageView = (ImageView)convertView.findViewById(R.id.cp_image);
            FrameLayout container = (FrameLayout)convertView.findViewById(R.id.youtubeShower);
            Button playButton = (Button)convertView.findViewById(R.id.playStop);

            playButton.setVisibility(View.VISIBLE);

            if(position == 0){
                Log.d("horizontalsv","loading image with url: " + urlToLoad);
                new loadImage(imageView).execute(urlToLoad);
                //container.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.VISIBLE);


            }
            else if(position == 1){
                container.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.INVISIBLE);
                String videoId = extractYTId(urlToLoad);
                PlayYoutubeVideFragment youtubeFrag = PlayYoutubeVideFragment.newInstance(videoId);
                myContext.getSupportFragmentManager().beginTransaction().replace(R.id.youtubeShower, youtubeFrag).commit();

            }else{



            }

            return convertView;
        }
    }

,这是listview项目的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"

    >

    <FrameLayout
        android:id="@+id/youtubeShower"
        android:layout_width="match_parent"
        android:layout_height="288dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">

    </FrameLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/cp_image"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="87dp"
        android:text="Play"
        android:id="@+id/playStop"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

以下是我加载图片的方式:

private class loadImage extends  AsyncTask<String,Void,Bitmap>{

        ImageView target;

        public  loadImage(ImageView imageViewToLoad){
            target = imageViewToLoad;
            if(imageViewToLoad == null){
                Log.e("in loadImage","imageview is null");
            }else{
                Log.d("in loadImage","imageView is there");
            }
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            String urlString = params[0];

            try {
                URL url = new URL(urlString);
                try {
                    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    return  bmp;
                }catch(IOException e){
                    Log.e("IOException",e.getMessage());
                    return  null;
                }

            }catch (MalformedURLException e){
                Log.e("URL ERROR", "url = " + urlString + " and error = " + e.getMessage());
                return  null;
            }



        }

        @Override
        protected void onPostExecute(Bitmap o) {
            super.onPostExecute(o);
            if(o != null){
                Log.d("in loadimage","bitmap is not null");
                target.setImageBitmap(o);
                //lvTest.setAdapter(new HorizontalAdapter());
            }else{
                Log.e("in loadimage","bitmap is null");
                target.setImageBitmap(null);
                target.setBackgroundColor(Color.WHITE);
            }
        }
    }

是否有任何原因导致图像无法加载?位图在加载图像asynctask中返回为NOT null,因此它应该可以工作。

1 个答案:

答案 0 :(得分:0)

使用Picasso库,它可以解决您在异步任务中遇到的所有问题。 http://square.github.io/picasso/