Question

I want to unchecked my all check boxes inside listview on button click. This my adapter class. It unchecked all check boxes but for that I have to scroll through listview. When I scroll through it, it unchecked according to the rows that are visible.

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;

    }

}
Was it helpful?

Solution

As Listview recycles view its not possible to do this with only BroadCastReciver. It was really a bad idea. I done this using Database and 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);
                    }
                }
            });

what i done is i set the checked or unchecked state of checkbox according to id in database. Then by fetching state of that id i done calculation!.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top