判断gridview中必须选中才可以点击按钮

起先的想法是遍历gridview然后查看没有选中的,当有没有选中的时候

弹出Toast提示,又或者遍历选中的判断当有选中的时候可以上传,

这两种方法都不能实现全部item的判断,比如说第一个item没有选中的时候

就会弹出Toast提示,又或者,当第一个选中的时候那么直接就会上传,不会再遍历整个gridview

 

询问前辈后找到了一种简单的判断方式

 JSONArray jsonArray1 = new JSONArray();
                for (int i = 0; i < addTagResBeanLCommonAdapter.getCount(); i++) {
                    GetTagResBean.DataBean dataBean = addTagResBeanLCommonAdapter.getList().get(i);
                    if (dataBean.isIscheck()) {
                        try {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("tagName", dataBean.getTagName());
                            jsonObject.put("userId", DBUtils.getUserType());
                            jsonObject.put("id", dataBean.getId());
                            jsonArray1.put(jsonObject);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
                if (jsonArray1.length()<=0){
                    Toast(mContext,"未选择退回/拒绝理由",2);
                    return;
                }

即,先声明数组,再遍历gridview,然后把已经选中的放入Array中

再做判断,当这个数组的长度<=0时,弹出Toast,否则即数组里有数据,即有选中的item的时候

上传Array的数据

posted @ 2020-11-16 16:53  null_lii  阅读(85)  评论(0)    收藏  举报