上按一下按钮在GridView中提取复选框的状态在GridView项的所有复选框 [英] fetching checkbox state in a gridview item for all checkbox in gridview on button click

查看:114
本文介绍了上按一下按钮在GridView中提取复选框的状态在GridView项的所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,现在我需要保持在每个GridView的项目跟踪复选框的状态,并获取上一个按钮,信息点击更新的信息。我的按钮事件imageadapter的一个GridView调用活动。

I have the code below, now I need to keep track of the checkbox state in each gridview item, and fetch that info on a button click to update the information. My button event in the calling activity of imageadapter for a gridview.

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        ImageView imgView = null;

            if (convertView == null) {

                holder = new ViewHolder(); 
                LayoutInflater ltInflate = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); 
                convertView = ltInflate.inflate(R.layout.griditem, null);

                holder.textview1 = (TextView) convertView.findViewById(R.id.grid_item_alert_date);
                holder.textview2 = (TextView) convertView.findViewById(R.id.grid_item_alert_time);
                holder.textview3 = (TextView) convertView.findViewById(R.id.grid_item_alert_type);

                holder.imageview    = (ImageView) convertView.findViewById(R.id.grid_item_image);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox_ack);
                convertView.setTag(holder);

            }
            else
            {   
                holder = (ViewHolder) convertView.getTag();
            }

            holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                  @Override
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
                {

                    //Accessing or saving position to a List doesn't work here

                    //How to add ? info to a list here...

                }

                  } });

            holder.textview1.setText("Text 1 ");
            holder.textview2.setText("Text 2 ");
            holder.textview3.setText("Text 3 ");
            holder.checkbox.setChecked(false);
            holder.imageview.setImageBitmap(bitmap);
            holder.id = position;

            return convertView;
    }

在活动:

private OnClickListener UpdateButtonListener =
            new OnClickListener(){
            public void onClick(View v)
            {
            //CheckBox ckbocx = (CheckBox) findViewById(R.id.checkbox_ck);
                        //Need info on all the checkboxes for each gridview item

    };

任何线索,提示是无任欢迎。

Any clues, hints are more than welcome.

推荐答案

应对整数数组来存储复选框的状态时,它选中/取消, 最初,填补了数组这表明未选中的复选框,这样的0值。

Deal with integer array to store the state of your checkboxes when it checked/unchecked, Initially fill the array with 0 values which indicate unchecked of your checkboxes like this.

int[] checkStates;
 checkStates = new int[datalist.length()];
   for (int i = 0; i < datalist.length(); i++) {
        checkStates[i] = 0;
   }

现在处理复选框,​​单击事件获得完美的境地。使用<一个href="http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view">settag &放大器; GET gettag 并点击里面的事件时中得到0选择改变特定位置的值设置为1。

Now handle checkboxes click event to get perfect position. use settag & get gettag and inside click event when box get selected change the value of particular position to 1 from 0.

像这样#

  checkbox.setTag(position);



    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            pos = (Integer) buttonView.getTag();
            // checkStates[pos] = 1;
            // pos = (Integer) v.getTag();

            if (!buttonView.isChecked()) {
                boxState[pos] = 0;
            } else {
                boxState[pos] = 1;
            }
            notifyDataSetChanged();
        }
    });

和内部getview方法处理你的支票/取消选中状态,这样一来..

and inside getview method handle your check/uncheck state this way..

if (checkStates[position] == 0) {
            checkbox.setChecked(false); // set unchecked"
        } else {
            checkbox.setChecked(true); // set checked"
        }

此方式,您将得到哪些选择复选框的信息,进一步处理您的按钮单击事件,并得到int数组填补,同时检查和取消。

This way you will get the info of checkboxes which are selected, further handle your button click event, and get the int array which filled while check and uncheck.

这篇关于上按一下按钮在GridView中提取复选框的状态在GridView项的所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆