2011-10-26 70 views
1

我做這樣的事情:如何將數組傳遞給GridView中的getView方法?

grid = new GridView(this); 

imagesPreviewH={1,2,3,4} 

mAdapter = new HorizontalImageAdapter(this, imagesPreviewH); 

grid.setAdapter(mAdapter); 

grid2 = new GridView(this); 

imagesPreviewH={5,6,7,8} 

mAdapter = new HorizontalImageAdapter(this, imagesPreviewH); 

grid2.setAdapter(mAdapter); 

public View getView(final int position, View convertView, 

ViewGroup parent) { 

    ImageView imageView; 

if (convertView == null) { // if it's not recycled, initialize    // 

imageView = new ImageView(mContext); 

imageView.setImageDrawable(imagesPreviewH.get(position)); 

int x = (int) (imagesPreviewH.get(position).getBitmap().getWidth() * 1.6); 

    int y = (int) (imagesPreviewH.get(position).getBitmap().getHeight() * 1.6); 

    imageView.setLayoutParams(new GridView.LayoutParams(x, y)); 

imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 


imageView.setPadding(40, 20, 0, 1); 


    imageView = (ImageView) convertView; 
     } 

     return imageView; 

    } 

但進入GriView只加載{5,6,7,8}。如何使用{1,2,3,4}和{5,6,7,8}數據來處理兩個GridView,但不能使用兩個帶有{5,6,7,8}和{5,6,7, 8}現在的數據?

回答

1

您使用imagesPreviewH兩次。每個gridview都需要一個不同的數組。代之以:

grid = new GridView(this); 

imagesPreviewH1={1,2,3,4} 

mAdapter = new HorizontalImageAdapter(this, imagesPreviewH1); 

grid.setAdapter(mAdapter); 

grid2 = new GridView(this); 

imagesPreviewH2={5,6,7,8} 

mAdapter = new HorizontalImageAdapter(this, imagesPreviewH2); 

grid2.setAdapter(mAdapter); 
相關問題