[Android]在网格视图中为图像添加复选框

时间:2011-06-14 06:07:10

标签: android gridview view inflate

网格布局的Xml。          

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myGrid"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:padding="2dip"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="148dp"
    android:stretchMode="spacingWidthUniform"
    android:gravity="center"
    />
</RelativeLayout>

imagenselect.xml for Image and checkbox。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/GridItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="#000080">

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>
 <CheckBox 
     android:id="@+id/check1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" android:text="Android" />
</LinearLayout>

用于在gridview中添加图像并显示的类

private class ImageAdapter extends BaseAdapter {

        private Context context;
        public ImageAdapter(Context localcontext){  
            context = localcontext;
        }
        public int getCount() { 
            return cursor.getCount();
        } 

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub\

    View MyView = convertView;
    ImageView picturesView;
    picturesView = new ImageView(context);

    if (convertView == null) {

     LayoutInflater li = getLayoutInflater();
     MyView =  li.inflate(R.layout.imagenselect, null);

    // Move cursor to current position
    cursor.moveToPosition(position);
    // Get the current value for the requested column
    int imageID = cursor.getInt(columnIndex);
    // Set the content of the image based on the provided URI
        picturesView.setImageURI(Uri.withAppendedPath(
    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));

    picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    picturesView.setPadding(8, 8, 8, 8);
    picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));           
        }
    else {
         picturesView = (ImageView) convertView;
    }
    return picturesView;
      }
    }
}

这里有这个代码,我只得到gridview中的图像。但我想膨胀视图并使用它,这样我就可以添加一个复选框和图像。 (对于每个图像一个复选框)。

由于函数“myView”和“picturesView”中有两个视图。如果我尝试将picturesView强制转换为myView,那么我就会崩溃。提前谢谢!

关于改变,因为你建议我崩溃。

        @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub\

        View myView = convertView;
        if (convertView == null) {
           LayoutInflater li = getLayoutInflater();
           myView =  li.inflate(R.layout.imagenselect, null);
            }
        ImageView picturesView;
        picturesView = new ImageView(context);
               picturesView = (ImageView) myView.findViewById( R.id.grid_item_image);

        // Move cursor to current position
        cursor.moveToPosition(position);
        // Get the current value for the requested column
        int imageID = cursor.getInt(columnIndex);
        // Set the content of the image based on the provided URI
        picturesView.setImageURI(Uri.withAppendedPath(
              MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));

        picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        picturesView.setPadding(8, 8, 8, 8);
        picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));               

        return myView;
    }

1 个答案:

答案 0 :(得分:1)

你的代码看起来真的很困惑我。您的代码始终会返回ImageView个对象,而永远不会显示您从包含复选框的布局中展开的MyView视图。这可以解释为什么您的复选框没有出现。

我认为你需要的是:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View myView = convertView
    if( myView == null )
    {
        LayoutInflater li = getLayoutInflater();
        myView = li.inflate(R.layout.imagenselect, null);
    }
    ImageView pictureView = (ImageView) myView.findViewById( R.id.grid_item_image );
    // initialise pictureView here.
    return myView;
}