使用BaseAdapter o返回GridView

时间:2013-05-06 06:30:54

标签: android android-gridview baseadapter

我尝试在开源项目之上构建我的应用程序。 基本功能是使用GridView显示几个图像。 以下是代码段。注释行是原始代码。在原始代码中,Images.imageThumbUrls是url的字符串数组,大小(Images.imageThumbUrls.length)为99。 我想用我自己的版本替换原始网址,我做了。但是有一个错误。请参阅日志。

根据日志,getView的限制似乎是99(原始代码)。除了改变getCount之外,还有什么我需要做的吗?

createView方法中:

        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (mAdapter.getNumColumns() == 0) {
                        final int numColumns = (int) Math.floor(
                                mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                        if (numColumns > 0) {
                            final int columnWidth =
                                    (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                            mAdapter.setNumColumns(numColumns);
                            mAdapter.setItemHeight(columnWidth);
                            if (BuildConfig.DEBUG) {
                                Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                            }
                        }
                    }
                }
            });

ImageAdapter类:

private class ImageAdapter extends BaseAdapter {        
public int getCount() {
        //return Images.imageThumbUrls.length + mNumColumns;
        return DataStore.photosInfoOfTag.total + mNumColumns;
}
    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        // First check if this is the top row
        if (position < mNumColumns) {
            if (convertView == null) {
                convertView = new View(mContext);
            }
            // Set empty view with height of ActionBar
            convertView.setLayoutParams(new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight));
            return convertView;
        }

        // Now handle the main ImageView thumbnails
        ImageView imageView;
        if (convertView == null) { // if it's not recycled, instantiate and initialize
            imageView = new RecyclingImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(mImageViewLayoutParams);
        } else { // Otherwise re-use the converted view
            imageView = (ImageView) convertView;
        }

        // Check the height matches our calculated column width
        if (imageView.getLayoutParams().height != mItemHeight) {
            imageView.setLayoutParams(mImageViewLayoutParams);
        }

        // Finally load the image asynchronously into the ImageView, this also takes care of
        // setting a placeholder image while the background thread runs
        Photo photo = DataStore.photosInfoOfTag.photo.get(position - mNumColumns);
        String photoUrl = DataStore.imageUrlPart1 + Integer.toString(photo.getFarm()) +
                          DataStore.imageUrlPart2 + photo.getServer() + "/" + 
                          photo.getId() + "_" + photo.getSecret() +
                          DataStore.imageUrlPart3;
        mImageFetcher.loadImage(photoUrl, imageView);
        //mImageFetcher.loadImage(Images.imageThumbUrls[position - mNumColumns], imageView);
        return imageView;
    }

    public void setItemHeight(int height) {
        if (height == mItemHeight) {
            return;
        }
        mItemHeight = height;
        mImageViewLayoutParams =
                new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
        mImageFetcher.setImageSize(height);
        notifyDataSetChanged();
    }

    public void setNumColumns(int numColumns) {
        mNumColumns = numColumns;
    }
}
  

05-06 01:04:39.736:E / AndroidRuntime(16356):致命异常:主

     

05-06 01:04:39.736:E / AndroidRuntime(16356):
  java.lang.IndexOutOfBoundsException:索引100无效,大小为100

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   java.util.ArrayList.get(ArrayList.java:304)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   com.example.android.bitmapfun.ui.ImageGridFragment $ ImageAdapter.getView(ImageGridFragment.java:292)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.widget.AbsListView.obtainView(AbsListView.java:2143)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.widget.GridView.makeAndAddView(GridView.java:1341)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.widget.GridView.makeRow(GridView.java:341)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.widget.GridView.fillDown(GridView.java:283)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.widget.GridView.fillGap(GridView.java:243)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.widget.AbsListView $ FlingRunnable.run(AbsListView.java:4087)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.view.Choreographer.doCallbacks(Choreographer.java:562)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.view.Choreographer.doFrame(Choreographer.java:531)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.os.Handler.handleCallback(Handler.java:725)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.os.Handler.dispatchMessage(Handler.java:92)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.os.Looper.loop(Looper.java:137)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   android.app.ActivityThread.main(ActivityThread.java:5041)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   java.lang.reflect.Method.invokeNative(Native Method)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   java.lang.reflect.Method.invoke(Method.java:511)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

     

05-06 01:04:39.736:E / AndroidRuntime(16356):at   dalvik.system.NativeStart.main(原生方法)

1 个答案:

答案 0 :(得分:1)

注意,当getCount()上的值返回大于适配器上的项目数(通常只填充单元格)时,您需要检查当前边界是否从未被绕过,如果是,则返回空视图(或者有一些背景的人。)

您的代码:

public int getCount() {
        //return Images.imageThumbUrls.length + mNumColumns;
        return DataStore.photosInfoOfTag.total + mNumColumns;
}

只应返回DataStore.photosInfoOfTag.total

另外,不要使用这样的静态内容,你应该使用List作为适配器的数据。