创建自定义网格视图有点奇怪

时间:2013-06-27 00:36:30

标签: android checkbox imageview baseadapter

我的代码发生了一些奇怪的事情我不知道为什么会发生这种情况。在我的xml文件中,我放了一个复选框和图像视图,但在我的班级中发生错误的复选框和图像视图反转我转换的imageview来自复选框的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;
}

1 个答案:

答案 0 :(得分:0)

在你的代码中             holder.imageview =(ImageView)convertView.findViewById(R.id.cbo_CheckImage);             holder.checkbox =(CheckBox)convertView.findViewById(R.id.img_thumbImage);

所以,请改变

holder.imageview =(ImageView)convertView.findViewById(R.id.img_thumbImage);             holder.checkbox =(CheckBox)convertView.findViewById(R.id.cbo_CheckImage);

它的工作

你有用的是给我1 +