我的代码发生了一些奇怪的事情,我不知道为什么会发生。在我的 xml 文件中,我放置了一个复选框和图像视图,但是在我的班级中发生了错误,复选框和图像视图被反转了我投射的图像视图来自复选框的 id,复选框来自图像视图的 id,当我投射我的 xml 中 imageview 的 id 上的 imageview 与它出错的复选框相同。 这是我的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<CheckBox
android:id="@+id/cbo_CheckImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px" />
<ImageView
android:id="@+id/img_thumbImage"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginRight="10px" />
</LinearLayout>
这是我的课
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.gallery, null);
//LOOK AT HERE I DON'T KNOW WHY IT'S HAPPENING BUT IT WORKS. BUT WHEN I SWITCH THE IMAGEVIEW CAST TO THE ID OF MY IMAGEVIEW IN MY XML FILE IT GOT ERROR
holder.imageview = (ImageView) convertView.findViewById(R.id.cbo_CheckImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.img_thumbImage);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]){
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imageview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
startActivity(intent);
}
});
holder.imageview.setImageBitmap(thumbnails[position]);
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder {
CheckBox checkbox;
ImageView imageview;
int id;
}