1

这是我的自定义适配器的代码:

        public static class ImageAdapter extends BaseAdapter {
        private static Context mContext;
        private static LayoutInflater mInflater;
        // Keep all Images in array
        private static Bitmap[] mThumbIds;
        private static int mViewResourceId, pos;
        private static CheckBox cb;
        // Constructor
        public ImageAdapter(Context ctx, int viewResourceId, Bitmap[] pics) {
            mInflater = (LayoutInflater) ctx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mThumbIds = pics;
            mViewResourceId = viewResourceId;
            mContext = ctx;
        }

        @Override
        public int getCount() {
            return mThumbIds.length;
        }

        @Override
        public Object getItem(int position) {
            return mThumbIds[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressWarnings("deprecation")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = mInflater.inflate(mViewResourceId, null);
            // ImageButton imageView = (ImageButton)
            // convertView.findViewById(R.id.icon);
            cb = new CheckBox(mContext);
            Drawable background = new BitmapDrawable(mThumbIds[position]);
            cb.setBackgroundDrawable(background);
            pos = position;
            System.out.println("Setting checkbox set: "+imageIsDup[pos]);
            cb.setChecked(imageIsDup[pos]);
            System.out.println("Has checkbox been set? "+cb.isChecked());
            cb.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (cb.isChecked()) {
                        imageIsDup[pos] = true;
                    } else
                        imageIsDup[pos] = false;
                }
            });
            return cb;
        }

    }
}

我正在这样设置适配器:

GridView list = (GridView) dialog
                            .findViewById(R.id.grid_view);
                    TextView no = (TextView) dialog
                            .findViewById(R.id.noOfDups);
                    no.setText("Found " + noOfImages
                            + " duplicates. Please verify.");
                    //list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                    dialog.setTitle("Images Found");
                    dialog.setCancelable(true);
                    // ImageAndTextAdapter adapter = new ImageAndTextAdapter
                    // (longOperationContext, R.layout.row, imageNames,
                    // imageLocs, thumb);
                    ImageAdapter adapter = new ImageAdapter(
                            longOperationContext, R.layout.row, thumb);
                    System.out.println("No of images:"+thumb.length);
                    list.setAdapter(adapter);

如果我单击并取消单击它们,这些复选框可以正常工作。不起作用的是setChecked()功能。虽然参数是true显示时没有设置复选框。错误是什么?

这段代码以前可以工作,不久前我做了一些编辑,但从那以后就没有工作了。遗憾的是我不记得编辑了。

更新 即使只有 8 张图片,而且println消息应该16只有48. 第一组只是假的,后两组具有正确的imageIsDup值。

4

3 回答 3

0

而不是在 ImageAdapter 类中设置复选框侦听器,您可以使用onitemClickListener,它最终将为您提供被选中的位置。所以你可以从那里更新你的imageIsDup 数组

CheckBox 将根据imageIsDup 的更新值自动保持选中或取消选中状态

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {
                // TODO Auto-generated method stub
                                if(imageIsDup[pos] == true)
                                  imageIsDup[pos] = false;
                                else
                                  imageIsDup[pos] = true;
                              adapter.notifyDataSetChanged();

            }
        });
于 2013-05-01T21:05:02.937 回答
0

我改为getView

public View getView(int position, View convertView, ViewGroup parent) {
            convertView = mInflater.inflate(mViewResourceId, list, false);
            // ImageButton imageView = (ImageButton)
            // convertView.findViewById(R.id.icon);
            cb = (CheckBox) convertView.findViewById(R.id.select);
            //cb = new CheckBox(mContext);
            Drawable background = new BitmapDrawable(mThumbIds[position]);
            cb.setBackgroundDrawable(background);
            pos = position;
            System.out.println("Setting checkbox set: "+imageIsDup[pos]);
            cb.setChecked(imageIsDup[pos]);
            System.out.println("Has checkbox been set? "+cb.isChecked());
            cb.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (cb.isChecked()) {
                        imageIsDup[pos] = true;
                    } else
                        imageIsDup[pos] = false;
                }
            });
            return convertView;
        }
于 2013-05-07T16:02:28.220 回答
0

是否调用了 adapter.notifyDataSetChanged() 方法通知列表刷新视图?

此外,如果您使用的是 eclipse,您可以从您编辑的任何文件中查看本地历史记录(可从右键菜单中获得)——也许您可以使用它来记住您所做的哪些编辑改变了您的功能。

于 2013-05-01T14:03:47.993 回答