【问题标题】:setChecked() method not working in getView() of custom adaptersetChecked() 方法在自定义适配器的 getView() 中不起作用
【发布时间】:2013-05-01 13:58:32
【问题描述】:

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

        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 值。

【问题讨论】:

    标签: android android-listview baseadapter


    【解决方案1】:

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

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

    【讨论】:

    • 文件在 3 个多月前被编辑过,很遗憾。我会尝试刷新列表。
    • 不,notifyDataSetChanged() 方法什么也没做。我以前也没用过。
    【解决方案2】:

    除了在 ImageAdapter 类中设置复选框侦听器之外,您还可以使用 onitemClickListener ,它最终会为您提供被选中的 position。所以你可以从那里更新你的 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();
    
                }
            });
    

    【讨论】:

    • 这部分已经可以正常工作了。不起作用的是imageIsDup 的预设值。 setChecked() 函数似乎没有任何效果。
    【解决方案3】:

    我把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;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-26
      相关资源
      最近更新 更多