in gridview checkbox is unchecked while scrolling gridview up and down

前端 未结 2 1094
野趣味
野趣味 2020-12-04 00:51

I have a GridView and I have put a checkbox and imageview. The problem is that when I check this checkbox and scroll down, the checBox gets unchecked. Though the uncheck ev

2条回答
  •  臣服心动
    2020-12-04 01:43

    https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

    Drawing from romain guy's solution of listview in the above link.

    Your Custom Adapter must implement CompoundButton.OnCheckedChangeListener and use a SparseBooleanArray

    Then

     holder.chckbx.setOnCheckedChangeListener(this);
    

    Then

         public boolean isChecked(int position) {
            return mCheckStates.get(position, false);
        }
    
        public void setChecked(int position, boolean isChecked) {
            mCheckStates.put(position, isChecked);
    
        }
    
        public void toggle(int position) {
            setChecked(position, !isChecked(position));
        }
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
         mCheckStates.put((Integer) buttonView.getTag(), isChecked);    
    
    }
    

    Example

    public class MainActivity extends Activity implements
    AdapterView.OnItemClickListener {
        int count;
    private CheckBoxAdapter mCheckBoxAdapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final GridView gridView = (GridView) findViewById(R.id.lv);
        gridView.setTextFilterEnabled(true);
        gridView.setOnItemClickListener(this);
        mCheckBoxAdapter = new CheckBoxAdapter(this);
               gridView.setAdapter(mCheckBoxAdapter);
    
       }
    
    public void onItemClick(AdapterView parent, View view, int
    position, long id) {
        mCheckBoxAdapter.toggle(position);
    }
    
    class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener
    {  private SparseBooleanArray mCheckStates;
       LayoutInflater mInflater;
        ImageView imageview1,imageview;
        CheckBox cb;
    
        CheckBoxAdapter(MainActivity context)
        {
            super(context,0);
            mCheckStates = new SparseBooleanArray(10);
            mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 10;
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
    
            return 0;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View vi=convertView;
            if(convertView==null)
             vi = mInflater.inflate(R.layout.checkbox, null); 
             imageview= (ImageView) vi.findViewById(R.id.textView1);
    
             cb = (CheckBox) vi.findViewById(R.id.checkBox1);
    
             cb.setTag(position);
             cb.setChecked(mCheckStates.get(position, false));
            cb.setOnCheckedChangeListener(this);
            return vi;
        }
         public boolean isChecked(int position) {
                return mCheckStates.get(position, false);
            }
    
            public void setChecked(int position, boolean isChecked) {
                mCheckStates.put(position, isChecked);
    
            }
    
            public void toggle(int position) {
                setChecked(position, !isChecked(position));
            }
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
             mCheckStates.put((Integer) buttonView.getTag(), isChecked);    
    
        }
    
    }
    
    }
    

    activity_main.xml

    
         
    
        
    

    checkbox.xml

    
    
    
        
    
        
    
    
    

    Similar to the one answered here. Instead of listview use gridview.

    How to change the text of a CheckBox in listview?

提交回复
热议问题