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

发布于 2024-11-15 07:34:43 字数 4113 浏览 3 评论 0原文

网格布局的 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 用于图像和复选框。

<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;
    }

Xml for the Grid layout.

<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>

Class for adding the images in gridview and displaying,

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;
      }
    }
}

Here with this code , I get only the images in gridview. But i want to inflate the view and use it such that i'm able to add a checkbox along with the image. (for each image a checkbox).

Since there are two views in the function "myView" and "picturesView". If i try to typecast the picturesView to myView then i'm getting a crash. Thanks in advance !

on changing as u suggested im getting a crash.

        @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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2024-11-22 07:34:43

你的代码实际上对我来说看起来很困惑。您的代码始终返回一个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;
}

Your code actually looks really confused to me. Your code always returns an ImageView object, and never the MyView view that you inflate from your layout which contains your check box. That would explain why your check box is not appearing.

I think that you need something along the lines of:

@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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文