package com.beter.timehole.fragments;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.beter.timehole.R;

import java.lang.reflect.Array;
import java.util.ArrayList;

/**
 * Created by taha on 18.12.2015.
 */
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
    private ArrayList<com.beter.timehole.core.Activity> list = new ArrayList<com.beter.timehole.core.Activity>();
    private Context context;

    public MyCustomAdapter(ArrayList<com.beter.timehole.core.Activity> list, Context context) {
        this.list = list;
        this.context = context;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int pos) {
        return list.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.custom_listview_element, null);
        }
        TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
        listItemText.setText(list.get(position).toString());
        /**
         * Takes the current item converts to string and puts in the current order in the list.
         */
        final CheckBox checkbox = (CheckBox) view.findViewById(R.id.doneCheckBox);
        final Button button = (Button) view.findViewById(R.id.button);
        final int Position = position;
        if(list.get(position).isDone()) {
            checkbox.setChecked(true);
            checkbox.setText("Done");
        }
        else {
            checkbox.setChecked(false);
            checkbox.setText("Undone");
        }

       button.setOnClickListener(new View.OnClickListener() {
                                     public void onClick(View v) {
                                         list.remove(Position);
                                         notifyDataSetChanged();
                                     }
                                 }
       );

        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                                @Override
                                                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                                    String text = "";
                                                    list.get(Position).setDone(checkbox.isChecked());//Changes the value of the activity object's boolean
                                                    if (checkbox.isChecked())
                                                        checkbox.setText("Done");
                                                    else
                                                        checkbox.setText("Undone");
                                                    // perform logic
                                                }
                                            }

        );





        return view;
    }
}

@@ -423,16 +423,123 @@
 hole.R;%0A
+import com.beter.timehole.core.Activity;%0A%0Aimport java.io.FileInputStream;%0Aimport java.io.ObjectInputStream;
 %0Aimport 
@@ -850,16 +850,74 @@
 context;
+%0A    ArrayList%3CActivity%3E dataFromFile = new ArrayList%3C%3E();
 %0A%0A    pu
@@ -1349,16 +1349,22 @@
 getView(
+final 
 int posi
@@ -2616,24 +2616,178 @@
 tChanged();%0A
+                                         dataFromFile = readActivitiesFromFile();%0A                                         dataFromFile.remove(position);%0A
             
@@ -3835,21 +3835,16 @@
      );%0A
-%0A%0A%0A%0A%0A
         
@@ -3861,11 +3861,666 @@
 w;%0A    %7D
+%0A%0A    private ArrayList%3CActivity%3E readActivitiesFromFile()%7B%0A        ArrayList%3CActivity%3E activitiesFromFile = new ArrayList%3C%3E();%0A        try%7B%0A            FileInputStream activityFileInputStream = context.openFileInput(%22activityobjects.dat%22);%0A            ObjectInputStream activityObjectInputStream = new ObjectInputStream(activityFileInputStream);%0A            activitiesFromFile = (ArrayList%3CActivity%3E)activityObjectInputStream.readObject();%0A            activityObjectInputStream.close();%0A            activityFileInputStream.close();%0A        %7D%0A        catch (Exception e)%7B%0A            e.printStackTrace();%0A        %7D%0A        return activitiesFromFile;%0A    %7D
 %0A%7D%0A

delete activity code fixed