custom checkbox difficulty in android

旧时模样 提交于 2019-11-28 10:37:40
Joey
class imageCheckBoxAdapter extends ArrayAdapter<String> implements View.onClickListener
{
    private final Context context;
    private final ArrayList<String> values;
    private final Map< String, SmbFile> obj;
    private ArrayList<Boolean> checks=new ArrayList<Boolean>();

    public imageCheckBoxAdapter(Context context,ArrayList<String> values,Map< String, SmbFile>obj) 
    {
        super(context, R.layout.row_checkbox, values);
        this.context = context;
        this.values = values;
        this.obj=obj;

        for (int i = 0; i < values.size(); i++) {
            checks.add(i, false);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row_checkbox, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.text1_check);
        CheckBox chk = (CheckBox) rowView.findViewById(R.id.checkBox1);
        textView.setText(values.get(position));
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_image_check);
        try
        {
            if((obj.get(values.get(position)).isFile()))
            {
                imageView.setImageResource(R.drawable.view_file_icon);
            }
            else
            {
                imageView.setImageResource(R.drawable.view_folder_icon);
            }
        }
        catch (SmbException e) 
        {
            Toast.makeText(context,"Network error",Toast.LENGTH_SHORT).show();
            Log.d("id1", "error1");
            e.printStackTrace();
        }

        chk.setTag(Integer.valueOf(position));

        // Set a listener for the checkbox
        chk.setOnClickListener(this);

        //Sets the state of CB, since we have the list of checked CB
        chk.setChecked(checks.get(position));

        return rowView;
    }

    @Override
    public void onClick(View view) {
        Integer index = (Integer)view.getTag();
        boolean state = checks.get(index.intValue());

        checks.set(index.intValue(), !state);
    }
}

I think your need to store value in an array. For each checkBox, you set a changeListener to store the checkBox associated.

Dina Kaiser

I recently read, not sure where already, a similar case someone asked about.

If I understand correctly, this may be because the CheckBox control is not databound in the DataRepeaterItem, and when scrolling the checkboxes lose their state.

You can try using Viewstate to maintain checked value. I'll try finding that post, it seemed informative.

Joey

I think you can save the position of the checkbox in the list by using the View.setTag method make a reference of your checkbox in the getView like this:

CheckBox chk = (CheckBox) rowView.findViewById(R.id.checkBox1);

Since CheckBoxes are indirect subclasses of View, it also has an attribute View.setTag like this:

chk.setTag(Integer.valueOf(position));

Then set an OnCheckedChangeListener to your chk like:

chk.setOnCheckedChangeListener(new OnCheckedChangeListener {
    public void onCheckedChanged(CompoundButton buttonView, boolean state) {
           Integer int = (Integer)buttonView.getTag();

           checks.set(int.intValue(), !boolean)
    }
});

Since the boolean arraylist size is same to the values size, the specified position corresponds the exact location of the checkbox

Then add this snippet to your getView method:

if (checks.get(position)) {
  chk.setChecked(checks.get(position));
  // do some stuff if wanted..
} else {
  chk.setChecked(checks.get(position));
  // do some stuff if wanted..
}

We did the snippet because when listview is scrolled upward or downward getView is called for the unseen row item. And to update the row item if already checked or not.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!