[Android的]在网格视图中添加复选框,图像 [英] [Android]adding checkbox to images in grid view
问题描述
XML的网格布局。
< GridView控件的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / myGrid
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:填充=2DIP
机器人:verticalSpacing =10dp
机器人:horizontalSpacing =10dp
机器人:为numColumns =auto_fit
机器人:columnWidth时=148dp
机器人:stretchMode =spacingWidthUniform
机器人:比重=中心
/>
< / RelativeLayout的>
imagenselect.xml的图像和复选框。
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /的GridItem
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:重力=CENTER_HORIZONTAL
机器人:背景=#000080> < ImageView的
机器人:ID =@ + ID / grid_item_image
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
< / ImageView的>
<复选框
机器人:ID =@ + ID / CHECK1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT机器人:文本=机器人/>
< / LinearLayout中>
类为GridView控件添加图像和显示,
私有类ImageAdapter延伸BaseAdapter { 私人上下文的背景下;
公共ImageAdapter(上下文localcontext){
上下文= localcontext;
}
公众诠释的getCount(){
返回cursor.getCount();
} 公共对象的getItem(INT位置){
返回的位置;
} 众长getItemId(INT位置){
返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
// TODO自动生成方法存根\\ 查看MyView的= convertView;
ImageView的picturesView;
picturesView =新ImageView的(上下文); 如果(convertView == NULL){ LayoutInflater李= getLayoutInflater();
MyView的= li.inflate(R.layout.imagenselect,NULL); //将光标移动到当前位置
cursor.moveToPosition(位置);
//获取被请求的列的当前值
INT imageID = cursor.getInt(参数:columnIndex);
//设置基于所提供的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(新GridView.LayoutParams(100,100));
}
其他{
picturesView =(ImageView的)convertView;
}
返回picturesView;
}
}
}
下面这个code,我只得到在GridView中的图像。但我想膨胀的观点并使用它,这样我能够与图像一起添加一个复选框。 (每个图像的复选框)。
由于在功能MyView的两种看法和picturesView。如果我尝试将强制转换为picturesView然后MyView的我得到一个崩溃。在此先感谢!
在不断变化的,如u建议即时得到崩溃。
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
// TODO自动生成方法存根\\ 查看MyView的= convertView;
如果(convertView == NULL){
LayoutInflater李= getLayoutInflater();
MyView的= li.inflate(R.layout.imagenselect,NULL);
}
ImageView的picturesView;
picturesView =新ImageView的(上下文);
picturesView =(ImageView的)myView.findViewById(R.id.grid_item_image); //将光标移动到当前位置
cursor.moveToPosition(位置);
//获取被请求的列的当前值
INT imageID = cursor.getInt(参数:columnIndex);
//设置基于所提供的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(新GridView.LayoutParams(100,100)); 返回MyView的;
}
您code实际上看起来真的很困惑我。您code 总是返回的ImageView
对象,从不 MyView的
观点从您的布局,其中包含您的复选框膨胀。这可以解释为什么你的复选框没有出现。
我认为你需要的线沿线的东西:
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看MyView的= convertView
如果(MyView的== NULL)
{
LayoutInflater李= getLayoutInflater();
MyView的= li.inflate(R.layout.imagenselect,NULL);
}
ImageView的pictureView =(ImageView的)myView.findViewById(R.id.grid_item_image);
//初始化pictureView在这里。
返回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;
}
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;
}
这篇关于[Android的]在网格视图中添加复选框,图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!