2

我用自定义适配器制作了一个列表视图。它有一个 Imageview、textview 和一个复选框

---------------------------------------
 <ImageView --- textView --- checkbox >
---------------------------------------

我正在使用名为“mCheckStates”的 SparseBooleanArray 来跟踪复选框的状态。另外我想限制用户一次只检查两个复选框。为此,我使用了一个 int 变量作为计数器。

但是当我滚动 listView onCheckChangedListener 被调用并且计数减少一,有时减少二,允许用户检查 3 或 4 个复选框。

我怎样才能阻止这种情况发生?请帮我找到解决方案。谢谢。

代码

protected class MyCustomAdapter extends ArrayAdapter<String> implements
        CompoundButton.OnCheckedChangeListener {

    ArrayList<String> myList;
    int count = 0;
    ViewHolder holder = null;

    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<String> sList) {
        super(context, textViewResourceId, sList);
        mCheckStates = new SparseBooleanArray(sList.size());
        myList = sList;
    }

    private SparseBooleanArray mCheckStates;

    private class ViewHolder {
        TextView text;
        CheckBox chkbox;
        ImageView imageview;
    }

    @Override
    public int getCount() {
        return stg1.size();
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.new_search_adptr, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.adapterText1);
            holder.chkbox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.imageView1);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.chkbox.setTag(position);
        holder.chkbox.setChecked(mCheckStates.get(position, false));
        holder.chkbox.setOnCheckedChangeListener(this);
        holder.text.setText(stg1.get(position).toString());
        holder.imageview.setImageBitmap(arr_img.get(position));
        return convertView;
    }

    public boolean isChecked(int position) {
        System.out.println("inside isChecked");
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        System.out.println("inside setChecked");
        mCheckStates.put(position, isChecked);

    }

    public void toggle(int position) {
        System.out.println("inside toggle");
        setChecked(position, !isChecked(position));

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {

        System.out.println("\n inside onCheckedChanged: " + isChecked);

        if (isChecked) {
            System.out.println("inside isChecked true");
            System.out.println("count= " + count);

            if (count >= 2) {

                // count--;

                System.out.println("inside count>2: " + count);
                buttonView.setChecked(false);
                Toast.makeText(getApplicationContext(),
                        "Please select only two cars!", Toast.LENGTH_LONG)
                        .show();

            } else {

                count++;
                System.out.println("inside else: " + count);
                mCheckStates.put((Integer) buttonView.getTag(), isChecked);
            }

        } else {

            System.out.println("inside isChecked false" + count);
            mCheckStates.put((Integer) buttonView.getTag(), isChecked);
            count--;

        }

    }
}
4

2 回答 2

0

我假设您的 CustomAdapter 用于 ListView。所以事情就是这样,通过单击单元格进行检查会更舒适。

所以你可以做的是在你的adater本身上设置一个监听器:

adapter .setOnItemClickListener();

然后在活动/片段中实现 onItemClickListener

它将捕获所有单元格的点击,并为您提供该单元格在列表中的位置。

如果在您的适配器中,您使用一些 Holder 类填充单元格并将其设置为单元格的标签,您可以在 onItemClick 方法中获取持有者:

FiltreCellHolder filtreCellHolder = null;                       

filtreCellHolder = (FiltreCellHolder) view.getTag();

然后根据您的全局检查列表,您可以测试是否检查了此视图,并且由于用户单击它,您检查/取消检查它:

filtreCellHolder.cellCheck.setChecked(!filtreCellHolder.cellCheck.isChecked());
adapter.notifyDataSetChanged();
于 2013-09-23T10:05:40.327 回答
0

我希望这能帮到您。请将此代码更改为您的格式

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void initialize() {


        count = myList.size();
        thumbnailsselection = new boolean[count];
        notifyDataSetChanged();
    }

    public int getCount() {
        return arrPath.length;
    }

    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.galleryitem, 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.setAdjustViewBounds(true);
        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.setImageBitmap(decodeFile(arrPath[position]));
        // holder.imageview.setScaleType(ScaleType.FIT_XY);
        // holder.imageview.setAdjustViewBounds(true);
        holder.checkbox.setChecked(thumbnailsselection[position]);
        holder.id = position;
        return convertView;
    }
}

请在按钮单击中检查此条件

final int len = thumbnailsselection.length;
            int cnt = 0;
            String selectImages = "";
            path = new ArrayList<String>();
            for (int i = 0; i < len; i++) {
                if (thumbnailsselection[i]) {
                    cnt++;
                    selectImages = arrPath[i];
                    path.add(selectImages);
                }
            }
            if (cnt == 0) {
                Toast.makeText(getApplicationContext(),
                        "Please select at least one image",
                        Toast.LENGTH_LONG).show();
            } else if (cnt > 5) {
                Toast.makeText(getApplicationContext(),
                        "Please select only 5 images", Toast.LENGTH_LONG)
                        .show();
            } else {}
于 2013-09-23T10:59:30.900 回答