package com.spbstu.appmath.Workout_Journal;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;

import java.util.List;

public class MainListAdapter extends ArrayAdapter<Training> {

    final private List<Training> trainings;
    final ListView listView;
    final DBHelper db;

    public MainListAdapter(Context context, int resource, List<Training> trains,
                           final ListView listView, final DBHelper db) {
        super(context, resource, trains);
//        this.trainings = new CopyOnWriteArrayList<>();
//        this.trainings.addAll(trains);
        this.trainings = trains;
        this.listView = listView;
        this.db = db;
    }

    private class ViewHolder {
        TextView name;
        TextView date;
        CheckBox checked;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) super.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.listItem);
            holder.checked = (CheckBox) convertView.findViewById(R.id.checkBox);
            convertView.setTag(holder);

            final View finalConvertView = convertView;
            holder.checked.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    cb.setChecked(cb.isChecked());
                    Training training = (Training) cb.getTag();
                    training.setChecked(cb.isChecked());

                    final ImageButton deleteButton = (ImageButton) finalConvertView.getRootView().findViewById(R.id.button_delete);
                    deleteButton.setVisibility(View.VISIBLE);
                    deleteButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            for (Training t : trainings) {
                                if (t.isChecked()) {
                                    if (db.deletePlannedTraining(t)) {
                                        trainings.remove(t);
                                        notifyDataSetChanged();
                                    }
                                }
                            }
                        }
                    });

                    if (cb.isChecked()) {
                        deleteButton.setVisibility(View.VISIBLE);

                    } else {
                        boolean isAnyChecked = false;
                        for (Training t : trainings) {
                            if (t.isChecked()) {
                                isAnyChecked = true;
                                break;
                            }
                        }
                        if (!isAnyChecked) {
                            deleteButton.setVisibility(View.INVISIBLE);
                        }
                    }
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Training training = trainings.get(position);
        holder.name.setText(training.getName());
        holder.checked.setChecked(training.isChecked());
        holder.checked.setTag(training);

        return convertView;
    }
}

@@ -57,18 +57,23 @@
 oid.
-app.Dialog
+content.Context
 ;%0Aim
@@ -81,39 +81,35 @@
 ort android.
-content.Context
+media.Image
 ;%0Aimport and
@@ -220,36 +220,8 @@
 up;%0A
-import android.view.Window;%0A
 impo
@@ -2728,32 +2728,91 @@
               %7D%0A
+                            checkSelections(deleteButton);%0A
                 
@@ -2813,32 +2813,32 @@
               %7D%0A
-
                 
@@ -2954,17 +2954,16 @@
 SIBLE);%0A
-%0A
         
@@ -3011,432 +3011,38 @@
     
-boolean isAnyChecked = false;%0A                        for (Training t : trainings) %7B%0A                            if (t.isChecked()) %7B%0A                                isAnyChecked = true;%0A                                break;%0A                            %7D%0A                        %7D%0A                        if (!isAnyChecked) %7B%0A                            deleteButton.setVisibility(View.INVISIBLE);%0A                        %7D
+checkSelections(deleteButton);
 %0A   
@@ -3052,32 +3052,34 @@
               %7D%0A
+%0A%0A
                 
@@ -3377,24 +3377,24 @@
 training);%0A%0A
-
         retu
@@ -3414,11 +3414,374 @@
 w;%0A    %7D
+%0A%0A    private void checkSelections(final ImageButton deleteButton) %7B%0A        boolean isAnyChecked = false;%0A        for (Training t : trainings) %7B%0A            if (t.isChecked()) %7B%0A                isAnyChecked = true;%0A                break;%0A            %7D%0A        %7D%0A        if (!isAnyChecked) %7B%0A            deleteButton.setVisibility(View.INVISIBLE);%0A        %7D%0A    %7D
 %0A%7D%0A

Fix debug when delete button does not removed