Android getView和ImageAdapter

时间:2011-11-15 18:52:33

标签: android image gallery

我想实现那种gallery,但不同的是远程获取图像。 我们的想法是仅在下载图像时更新视图。 =>我只有在转到onComplete时才需要更新视图。

请在下面找到我的代码:

public class displayAlbums extends Activity {


    private static String URL_GRAPH_API = "https://graph.facebook.com/";
    private static final String TAG = "displayAlbums";
    private ArrayList<Bitmap> thumbnails = new ArrayList<Bitmap>();
    private ArrayList<URI> thumbnails_uri = new ArrayList<URI>();
    private ImageAdapter imageAdapter;
    private int imageCounter = 0;
    public boolean[] thumbnailsselection;
    public Albums album;
    private Context displayAlbums_context;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galleryitem);
        this.displayAlbums_context = this;
        Log.d(TAG, "on create");    
        findViewById(R.id.itemCheckBox).setVisibility(View.GONE);
        final GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);        
        populateThumbnailsList();




        processThumbnailsToDownload(thumbnails_uri, new PictureRequestListener() {

            @Override
            public void onComplete(Bitmap bitmap) {

                if(imageAdapter == null) {
                    Log.d(TAG, "image adapter is null");
                    imageAdapter = new ImageAdapter(displayAlbums_context);
                    imagegrid.setAdapter(imageAdapter); 
                }
                imagegrid.setId(imageCounter);
                thumbnails.add(bitmap);     
                imageCounter++;
                //imageAdapter.notifyDataSetChanged();
            }
        }); 

    }




    public void processThumbnailsToDownload(ArrayList<URI> thumbnails_uri, final FbRequestListener listener)  {     
        Log.v(TAG,"processThumbnailsToDownload CALLED");
        for(URI thumbnail_uri : thumbnails_uri ) { 
            new HttpConnection(ConnectionManager.getConnectionHandler(listener)).bitmap(thumbnail_uri);
        }
    }



    public void populateThumbnailsList() {
        URI uri = null;

        for(final Albums album : LoadObjects.albums  ) {    
            Bundle parameters = new Bundle();
            parameters.putString("access_token", FbpicturesActivity.mFacebook.getAccessToken());            
            try {
                uri = new URI(URL_GRAPH_API + album.getAid() + "/picture" + "?" +Util.encodeUrl(parameters) );
                thumbnails_uri.add(uri);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }   
        }
    }



public class ImageAdapter extends BaseAdapter {


        private LayoutInflater mInflater;
        private Context myContext;

        public ImageAdapter(Context displayAlbums_context) {
            Log.v(TAG,"IMAGE ADAPTER");
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        }

        public int getCount() {
            Log.d(TAG,"thumnail_uri size"+thumbnails_uri.size());
            return thumbnails_uri.size();
        }

        public Object getItem(int position) {
            return position;
        }

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

        public View getView(final int position, View convertView, ViewGroup parent) {
            Log.v(TAG,"GET VIEW CALLED");
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.galleryitem, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);               
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }





            //holder.checkbox.setId(position);
            //holder.imageview.setId(position);
            Log.d(TAG,"thumnail size"+thumbnails.size());
            Log.d(TAG,"thumnail position"+thumbnails.get(position));

            holder.imageview.setImageBitmap(thumbnails.get(position));
            holder.checkbox.setChecked(true);
            holder.id = position;   

            return convertView; 


        }
    }

}   


class ViewHolder {
    ImageView imageview;
    CheckBox checkbox;
    int id;
}

galleryItem.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <Button android:id="@+id/selectBtn"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Select" android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:minWidth="200px" />

    <GridView android:id="@+id/PhoneImageGrid"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:numColumns="auto_fit" android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp" android:columnWidth="90dp"
        android:stretchMode="columnWidth" android:gravity="center"
        android:layout_above="@id/selectBtn" />

    <ImageView android:id="@+id/thumbImage" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_centerInParent="true" />

    <CheckBox android:id="@+id/itemCheckBox" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

请注意我添加了一些Log.v以了解何时调用getView:

=&GT; http://pastebin.com/L9PYBYDa

问题:

1)为什么getView被调用4次?

2)为什么holder.imageview.setImageBitmap(thumbnails.get(position))返回java.lang.IndexOutOfBoundsException:索引1无效,大小是1?

3)您对使用远程图像实现我的图库有什么建议吗?

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

1)getView将被调用至少(numberOfVisibleRows + 1或+2)次。我说至少是因为它可以被系统想要多次调用。无法保证。

2)因为缩略图小于2或为空。你必须调试才能看到

3)检查this