取消选中自定义列表视图中的复选框

时间:2012-10-18 10:22:15

标签: android android-listview android-checkbox

我想在按钮点击时取消选中listview中的所有复选框。 这是我的适配器类。它取消选中所有复选框,但为此我必须滚动浏览列表视图。 当我滚动浏览它时,它会根据可见的行取消选中。

public class ExpenseCalculatorAdapter extends ArrayAdapter<String>{
    private Activity context;
    private static LayoutInflater inflator = null;
    private  ArrayList<String> data;
    private  ArrayList<String> values;
    DataBaseUtil dbUtils;


    public ExpenseCalculatorAdapter(Activity context, ArrayList<String> data){
        super(context,R.layout.expenserow, data);
        this.context = context;
        this.data = data;
        dbUtils=new DataBaseUtil(context);


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        String row=data.get(position);
        String date;
        String type;
        String cost;
        final String id;
        /*  result.add(c.getString(iRowid)+"&"+c.getString(iDate)+"~"+c.getString(iCost)+"#"+c.getString(iType)+"*");*/
        id=row.substring(0, row.indexOf("&"));
        date=row.substring(row.indexOf("&")+1,row.indexOf("~"));
        cost=row.substring(row.indexOf("~")+1,row.indexOf("#"));
        type=row.substring(row.indexOf("#")+1,row.indexOf("*"));

        if(convertView==null)
        {
            ViewHolder holder = new ViewHolder();
            inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.expenserow, null);

            holder.togCheck=(CheckBox)convertView.findViewById(R.id.check);
            holder.textDate = (TextView) convertView.findViewById(R.id.txtrowDate);
            holder.textType = (TextView) convertView.findViewById(R.id.txtrowType);
            holder.textCost = (TextView) convertView.findViewById(R.id.txtrowCost);

            holder.togCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    Intent intent=new Intent();
                    intent.setAction("clicked");
                    if(buttonView.isChecked())
                    {
                        try
                        {
                            dbUtils.open();
                            dbUtils.setVisibility(id);
                        }catch(SQLException sqx)
                        {
                            sqx.printStackTrace();
                        }
                        catch(Exception ex)
                        {
                            ex.printStackTrace();
                        }
                        finally
                        {
                            dbUtils.close();
                        }
                        intent.putExtra("isClicked","yes");
                        intent.putExtra("ID",""+id);
                        context.sendBroadcast(intent);
                    }
                    else
                    {
                        try
                        {
                            dbUtils.open();
                            dbUtils.setInVisibility(id);
                        }catch(SQLException sqx)
                        {
                            sqx.printStackTrace();
                        }
                        catch(Exception ex)
                        {
                            ex.printStackTrace();
                        }
                        finally
                        {
                            dbUtils.close();
                        }
                        intent.putExtra("isClicked","no");
                        intent.putExtra("ID",""+id);
                        context.sendBroadcast(intent);

                    }
                }
            });




            convertView.setTag(holder);

        }

        ViewHolder hold = (ViewHolder) convertView.getTag();

        //setting Data to List
        hold.textDate.setText(date);
        hold.textType.setText(type);
        hold.textCost.setText(cost);



        if(CheckReceiver.checkAll)
        {
            hold.togCheck.setChecked(true);


        }
        if(!CheckReceiver.checkAll)
        {
            hold.togCheck.setChecked(false);
        }


        return convertView;
    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();



    static class ViewHolder{

        public CheckBox togCheck;
        public TextView textDate;
        public TextView textType;
        public TextView textCost;

    }

}

1 个答案:

答案 0 :(得分:0)

作为Listview回收视图,仅使用BroadCastReciver无法执行此操作。这真是一个坏主意。 我是使用Database和BroadCastReciver完成的。

holder.togCheck.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    /*Intent intent=new Intent();
                    intent.setAction("clicked")*/;
                    if(holder.togCheck.isChecked())
                    {
                        //holder.togCheck.setBackgroundResource(R.drawable.check);
                        try
                        {
                            dbUtils.open();
                            dbUtils.setVisibility(id);
                        //  Toast.makeText(context, "Checked ", Toast.LENGTH_SHORT).show();
                        }catch(SQLException sqx)
                        {
                            sqx.printStackTrace();
                        }
                        catch(Exception ex)
                        {
                            ex.printStackTrace();
                        }
                        finally
                        {
                            dbUtils.close();
                        }
                        intent.putExtra("isClicked","yes");
                        intent.putExtra("ID",""+id);
                        context.sendBroadcast(intent);
                    }
                    else
                    {
                    //  holder.togCheck.setBackgroundResource(R.drawable.uncheck);

                        try
                        {
                            dbUtils.open();
                            dbUtils.setInVisibility(id);
                            Toast.makeText(context, "UnChecked ", Toast.LENGTH_SHORT).show();
                        }catch(SQLException sqx)
                        {
                            sqx.printStackTrace();
                        }
                        catch(Exception ex)
                        {
                            ex.printStackTrace();
                        }
                        finally
                        {
                            dbUtils.close();
                        }
                        intent.putExtra("isClicked","no");
                        intent.putExtra("ID",""+id);
                        context.sendBroadcast(intent);
                    }
                }
            });

我所做的是根据数据库中的id设置复选框的已选中或未选中状态。 然后通过获取该id的状态我完成了计算!。

相关问题