2014-11-23 66 views
0

我編寫了一個使用ImageLoaderConfiguration庫將圖像下載到Bitmap對象中的活動。這是行得通。android - 在適配器中獲取項目(位置)返回null

我的下一個階段是創建5個GalleryItem對象並將Bitmap放入這些對象中。

最後,我創建了一個擴展ArrayAdapter的GalleryItemAdapter對象。

問題是,在函數getView中,getItem(position)返回null。

下面是代碼:

mainActivity:

package com.example.downloadimages; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 

import com.nostra13.universalimageloader.core.ImageLoader; 
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; 
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener; 

import android.graphics.Bitmap; 
import android.widget.ArrayAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 

public class MainActivity extends ActionBarActivity { 

    public String mUrl; 
    public Byte [] mImageBytes; 
    GalleryItem [] mGalleryItem; 
    Bitmap mBitmap; 
    public GridView mGridView; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
       setContentView(R.layout.image_view_in_layout); 
       mGridView = (GridView) findViewById(R.id.gridView); 
       mUrl = "http://www.royalcanin.in/var/royalcanin/storage/images/breeds/cat-breeds/norwegian-forest-cat/19311843-15-eng-GB/norwegian-forest-cat_cat_breed_cat_picture.jpg"; 

       ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this) .build(); 
       ImageLoader.getInstance().init(config); 
       ImageLoader.getInstance().loadImage(mUrl, new SimpleImageLoadingListener(){ 
        @Override 
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
         mBitmap = loadedImage; 
         mGalleryItem = new GalleryItem[5]; 
         for(GalleryItem gi : mGalleryItem) 
         { 
          gi = new GalleryItem(); 
          gi.setBitmap(mBitmap); 
         } 
         setupAdapter(); 
        } 
       }); 
     } 

     void setupAdapter() 
     { 
      if (mGridView == null) return; 
      mGridView.setAdapter(new GalleryItemAdapter(mGalleryItem)); 
     } 

     private class GalleryItemAdapter extends ArrayAdapter<GalleryItem> 
     { 
      public GalleryItemAdapter(GalleryItem[] mGalleryItem) 
      { 
       super(getApplicationContext(), 0, mGalleryItem); 
      } 

      @Override 
      public View getView(int position, View convertView, ViewGroup parent) 
      { 
       convertView = (ImageView)findViewById(R.id.imageView1); 
       GalleryItem item = getItem(position); 
       ((ImageView) convertView).setImageBitmap(item.getBitmap()); 
       return convertView; 
      } 
     } 
} 

類GalleryItem:

public class GalleryItem { 

    Bitmap bitmap; 

    public Bitmap getBitmap() { 
     return bitmap; 
    } 

    public void setBitmap(Bitmap bitmap) { 
     this.bitmap = bitmap; 
    } 
} 

image_view_in_layout:

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

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <GridView 
     android:id="@+id/gridView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:columnWidth="120dp" 
     android:numColumns="auto_fit" 
     android:stretchMode="columnWidth" > 

    </GridView> 
    </LinearLayout 

謝謝!

回答

0

您的適配器代碼是錯誤的。您正試圖在您的活動xml中將位圖設置爲ImageView。您需要在適配器中創建新的ImageView並將其返回。你可以看看這裏https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

+0

我改變了我的getView方法代碼,它仍然劑量工作。這是新的代碼:GalleryItem item = getItem(position); \t \t \t \t convertView = LayoutInflater.from(getContext())。inflate(R.layout.image_view_in_layout,parent,false); \t \t \t \t ImageView im =(ImageView)convertView.findViewById(R.id.imageView1); \t \t \t \t im.setImageBitmap(item.getBitmap()); \t \t \t \t return convertView; – 2014-11-23 22:10:32

+1

我唯一能想到的其他事情就是把每個循環換成正常循環。 – 2014-11-23 22:21:16

+0

它的工作原理!可能,不可能在每個循環中更改a中的指針的目標。 – 2014-11-23 22:58:56

相關問題