Q:

How to get checked checkbox count from adapter?

I have a ListView in the middle of my app and i have added a transparent RelativeLayout on the bottom that holds a TextView and a button. In the ListView there is a checkbox for each row. My problem is that i want to populate the TextView with the amount of checked item in the list as an item is clicked. Is there a way to do that?
this is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:text="@string/compare_information"
        android:textColor="#8F8F8F"
        android:textSize="20sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/transparent"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/informationText" />
    <ListView
        android:id="@+id/lvCompare"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/informationText">

    </ListView>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="75dp"
        android:layout_alignParentBottom="true"
        android:background="#90303030" >
        <TextView
            android:id="@+id/checkedItems"
            android:text="0 of 10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#EEEEEE"
            android:textSize="14sp"
            android:gravity="center_vertical|left"
            />
        <Button
            android:id="@+id/compare"
            android:background="@drawable/filter_button_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical|right"
            android:text="@string/compare"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            />
    </RelativeLayout>
</RelativeLayout>

This is my adapter:
    public class CompareListAdapter extends ArrayAdapter<Restaurant>{
        private ArrayList<Restaurant> list = new ArrayList<Restaurant>();
        private Context context;
        private static int checkBoxCounter = 0;
        private static int checkBoxInitialized = 0;
        private int layoutResourceId;
        private TextView cCheckedItems;
        private final String INFORMATION_TEXT = "Select six companies to compare";

        public CompareListAdapter(Context context, int resource,
                List<Restaurant> objects,TextView textViewToEdit ) {
            super(context, resource, objects);
            this.context = context;
            this.layoutResourceId = resource;
            checkBoxCounter = 0;
            cCheckedItems = textViewToEdit;
            // We put the strings from the array in to the hash map for storing
            for (int i = 0; i < objects.size(); ++i) {
                list.add(objects.get(i));
            }
        }

        public View getView( final int position, View convertView, ViewGroup parent) {
            View view = null;
            if(convertView==null){
                LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                view=inflater.inflate(layoutResourceId, parent, false);
                final ViewHolder viewHolder = new ViewHolder();
                viewHolder.cName =(TextView) view.findViewById(R.id.rest_name_textView1);
                viewHolder.addres1 =(TextView) view.findViewById(R.id.address1_textView2);
                viewHolder.addres2 =(TextView) view.findViewById(R.id.address2_textView3);
                viewHolder.openingTime =(TextView) view.findViewById(R.id.opening_hour_textView4);
                viewHolder.distance =(TextView) view.findViewById(R.id.distance_user_textView5);
                viewHolder.openImage =(ImageView)view.findViewById(R.id.clock_imageView2);
                viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
                viewHolder.cbBackground = (RelativeLayout) view.findViewById(R.id.checkbox_layout);
                viewHolder.rating = (RatingBar) view.findViewById(R.id.restaurant_ratingBar1);
                viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked && checkBoxCounter>=2){
                            list.get(position).setChecked(false);
                            viewHolder.checkBox.setChecked(false);
                            viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background);
                        }else {
                            Restaurant element = (Restaurant) viewHolder.checkBox.getTag();
                            element.setChecked(buttonView.isChecked());
                            if (isChecked) {
                                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background_checked);
                                checkBoxCounter++;
                            } else {
                                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background);
                                checkBoxCounter--;
                            }                            
                        }
                    Log.d("MYLOG INSIDE getView", String.valueOf(cCheckedItems));
                    cCheckedItems.setText(checkBoxCounter + " of 6");
                    Toast.makeText(context, "there are " + checkBoxCounter + " items selected", Toast.LENGTH_LONG).show();}});
}});
                view.setTag(viewHolder);
                viewHolder.checkBox.setTag(list.get(position));
            }else{
                view = convertView;
                ((ViewHolder) view.getTag()).checkBox.setTag(list.get(position));
            }
            ViewHolder viewHolder = (ViewHolder) view.getTag();
            Restaurant obj=list.get(position);
            viewHolder.rating.setRating((float) obj.getRating());
            viewHolder.cName.setText(obj.getRestaurantName());
            viewHolder.addres1.setText(obj.getAddress1());
            viewHolder.addres2.setText(obj.getAddress2());
            viewHolder.openingTime.setText(obj.getDayOfWeek()+" "+obj.getOpeningHour());
            viewHolder.distance.setText(obj.getDistance());
            viewHolder.checkBox.setChecked(obj.isChecked());
            if(obj.isChecked()) {
                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background_checked);
            }else{
                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background);
            }
            boolean isOpen=checkForOpen(obj.getDayOfWeek(),obj.getOpeningHour());
            if(isOpen)
                viewHolder.openImage.setImageResource(R.drawable.opened_logo);
            else viewHolder.openImage.setImageResource(R.drawable.closed_logo);
            return view;
        }
        private boolean checkForOpen(String weekDay,String openingTimeRange) {
            Calendar cal = Calendar.getInstance(); 

            int currentMinute=cal.get(Calendar.HOUR_OF_DAY)*60+cal.get(Calendar.MINUTE);;//today.MINUTE;
            int dayNo=cal.get(Calendar.DAY_OF_WEEK);
            String []timeRange=openingTimeRange.split("-");

            int openingMn=Integer.parseInt(timeRange[0].substring(0,2))*60+Integer.parseInt(timeRange[0].substring(3,5));

            int closingMn=Integer.parseInt(timeRange[1].substring(0,2))*60+Integer.parseInt(timeRange[1].substring(3,5));

            if(weekDay.equals(getDayOfWeekFromDayNo(dayNo))&& currentMinute>=openingMn  &&currentMinute<=closingMn)
                return true;

            return false;
        }
        public String getDayOfWeekFromDayNo(int dayNo)
        {
            String dayName="";
            switch (dayNo) {
            case 1:
                dayName="SUNDAY";
                break;
            case 2:
                dayName="MONDAY";
                break;
            case 3:
                dayName="TUESDAY";
                break;
            case 4:
                dayName="WEDNESDAY";
                break;
            case 5:
                dayName="THURSDAY";
                break;
            case 6:
                dayName="FRIDAY";
                break;
            case 7:
                dayName="SATERDAY";
                break;

            default:
                break;
            }
            return dayName;              
        }
        static class ViewHolder{
            RatingBar rating;
            CheckBox checkBox;
            RelativeLayout cbBackground;
            TextView cName, addres1, addres2, openingTime, distance;
            ImageView openImage;

        }
    }

Update : Got it working. Added what i have now working on my IDE. I made a TextView requirement in the constructor and saved that TextView in the adapter then called it when i need it. Log set up for testing if TextView exists. Thanks to @SagarPilkhwal for guiding to the solution.

A:

private TextView cCheckedItems;
....
cCheckedItems =(TextView) view.findViewById(R.id.checkedItems);
....
    viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked && checkBoxCounter>=2){
            list.get(position).setChecked(false);
            viewHolder.checkBox.setChecked(false);
            viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background);
        }else {
            Restaurant element = (Restaurant) viewHolder.checkBox.getTag();
            element.setChecked(buttonView.isChecked());
            if (isChecked) {
                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background_checked);
                checkBoxCounter++;              
            } else {
                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background);
                checkBoxCounter--;
            }
            cCheckedItems.setText(checkBoxCounter); //<-- ADD THIS
            Toast.makeText(context, "there are " + checkBoxCounter + " items selected", Toast.LENGTH_LONG).show();
        }}});

