我使用以下代码使用ImageAdapter填充gridview控件。
我希望得到一个gridview项,但int i =((ViewHolder)imagegrid.getItemAtPosition(0))。imageview.getId()使应用程序崩溃,为什么?
protected void onCreate(Bundle savedInstanceState) {
onCreate(savedInstanceState);
setContentView(R.layout.photo_main);
ImageAdapter imageAdapter = new ImageAdapter();
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(imageAdapter);
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.photogalleryitem, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.imageview.setImageBitmap(thumbnails[position]);
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}
<info.dodata.managephoto.PhotoRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/thumbImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<CheckBox
android:id="@+id/itemCheckBox"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="-2dp"
android:background="#70f2eada"
android:text="" />
</info.dodata.managephoto.PhotoRelativeLayout>
package info.dodata.managephoto;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class PhotoRelativeLayout extends RelativeLayout {
public PhotoRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public PhotoRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PhotoRelativeLayout(Context context) {
super(context);
}
@SuppressWarnings("unused")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
int childWidthSize = getMeasuredWidth();
int childHeightSize = getMeasuredHeight();
heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
答案 0 :(得分:0)
getItemAtPosition返回一个视图,而不是ViewHolder。你应该有castException,不是吗?
答案 1 :(得分:0)
实施项目点击监听器
imagegrid .setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
}});
基本上,诀窍是通过setTag
和getTag
方法设置和检索位置索引。 items变量引用包含我的自定义(适配器)对象的数据。