【问题标题】:How to check all checkbox in gridview using another checkbox or a button如何使用另一个复选框或按钮检查gridview中的所有复选框
【发布时间】:2013-06-27 14:09:49
【问题描述】:

我知道这个问题很熟悉,但我需要帮助。我不知道为什么我的代码返回一个空指针。到这里为止:

    final GridView imagegrid = (GridView) findViewById(R.id.grid_GalleryImage);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);

    // Check All

    Button btnCheckAll = (Button) findViewById(R.id.btn_SelectAll);
    btnCheckAll.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int count = imagegrid.getAdapter().getCount();
                for (int i = 0; i < count; i++) {
                    LinearLayout itemLayout = (LinearLayout)imagegrid.getChildAt(i); // Find by under LinearLayout
                    CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.cbo_CheckImage);
                    checkbox.setChecked(true);
                } 
            }
    });

【问题讨论】:

    标签: android button gridview checkbox


    【解决方案1】:

    在Adapter类里面把getView改成:

    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater in = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView =  in.inflate(R.layout.inflator, null);
    CheckBox c = (CheckBox) convertView.findViewById(R.id.checkBox);
    c.setId(position);
    
    return convertView;
    

    }

    在 onClickListener 内部:

    btnCheckAll.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
    
                int count = imagegrid.getAdapter().getCount();
    
                for (int i = 0; i < count; i++) {
                  CheckBox c = (CheckBox) gridView.findViewById(i);
                  c.setChecked(true); 
                }
    });
    

    【讨论】:

      猜你喜欢
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 2017-01-24
      • 1970-01-01
      相关资源
      最近更新 更多