android : checkboxes in a ListView ( strange behaviour of selected element)

前端 未结 2 1202
暗喜
暗喜 2021-01-06 16:57

I found here and here similar issues, but the problem is slightly different.

In a ListView, I try to put an adapter (extends from base Adapter) which contents checkb

2条回答
  •  长情又很酷
    2021-01-06 17:31

    I too had the same problem. I solved it using the following method.
    I created a bean class to set the checked property of checkbox and used ArrayAdapter. The ArrayAdpater extends the bean class.

    Bean Class

    public class MCSSCheckState
    {
        private boolean isChecked;
        public boolean getIsChecked()
        {
            return isChecked;
        }
        public void setIsChecked(boolean isChecked)
        {
            this.isChecked = isChecked;
        }
    }
    

    Java Class

    ArrayList mTitleList = new ArrayList();
    MCSSCheckState check_state=new MCSSCheckState();
    check_state.setIsChecked(false);
    mTitleList.add(i, check_state);
    ListAdapters adapter = new ListAdapters(this,R.id.camTitleTextView, mTitleList);
    ListView mListView = (ListView) findViewById(R.id.cameraListView);
    mListView.setAdapter(adapter);
    

    Adapter Class

    private class ListAdapters extends ArrayAdapter
    {
        private ArrayList items;
        private int position;       
        public ListAdapters(Context context, int textViewResourceId,
                            ArrayList mTitleList) 
                            {
                                super(context, textViewResourceId, mTitleList);
                                this.items = mTitleList;
                            }
                            @Override
                            public View getView(int position, View convertView, ViewGroup parent)
                            {
                                View v = convertView;
                                this.position = position;
                                if (v == null)
                                {
                                    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                    v = inflater.inflate(R.layout.camerslistinflater, null);
                                }
                                MCSSCheckState o = (MCSSCheckState) items.get(position);
                                if (o != null)
                                {
                                    checkBox = (CheckBox) v.findViewById(R.id.checkBox1);
                                    checkBox.setTag(position);
                                    checkBox.setChecked(o.getIsChecked());
                                    checkBox.setOnClickListener(new OnClickListener()
                                    {
                                         @Override
                                         public void onClick(View v)
                                         {
                                             MCSSCheckState obj = (MCSSCheckState) items.get(Integer.parseInt(v.getTag().toString()));
                                             obj.setIsChecked(((CheckBox)v).isChecked());
                                             items.set(Integer.parseInt(v.getTag().toString()), obj);                       
                                         }
                                     });
                                 }
                                 return v;
                             });
    }
    

    Hope this helps you.

提交回复
热议问题