adventure_fragments.Running.java Source code

Java tutorial

Introduction

Here is the source code for adventure_fragments.Running.java

Source

package adventure_fragments;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.InputFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import townley.stuart.app.android.trekkinly.MainActivityTrekkly;
import townley.stuart.app.android.trekkinly.R;
import itemclass_all.ItemClass;
import townley.stuart.app.android.trekkinly.DataBaseHelper;

/**
 * Copyright 2015 Stuart Lachlan Townley
    
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
 *Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

public class Running extends Fragment {
    View rootView;
    DataBaseHelper db;
    List<ItemClass> list;
    CustomAdapter4 adapt;
    ListView listItem;
    TextView nothingtext;
    int count;

    public static Running newInstance() {
        Running fragment = new Running();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.running_fragment, container, false);
        this.rootView = rootView;
        db = new DataBaseHelper(getActivity());
        list = db.getAllItemsRunning();

        adapt = new CustomAdapter4(getActivity(), R.layout.item_layout, list);
        listItem = (ListView) rootView.findViewById(R.id.listview_run);
        listItem.setAdapter(adapt);
        nothingtext = (TextView) rootView.findViewById(R.id.nothing_here_running);
        count = listItem.getCount();
        if (count > 0) {
            nothingtext.setVisibility(View.GONE);
        } else if (count == 0) {
            nothingtext.setVisibility(View.VISIBLE);
        }
        final ImageButton imageButton = (ImageButton) rootView.findViewById(R.id.add_item_running);
        listItem.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                switch (scrollState) {
                case 2: // SCROLL_STATE_FLING
                    //hide button here
                    imageButton.setVisibility(View.GONE);
                    break;

                case 1: // SCROLL_STATE_TOUCH_SCROLL
                    //hide button here
                    imageButton.setVisibility(View.GONE);
                    break;

                case 0: // SCROLL_STATE_IDLE
                    //show button here
                    imageButton.setVisibility(View.VISIBLE);
                    break;

                default:
                    //show button here
                    imageButton.setVisibility(View.VISIBLE);
                    break;
                }
            }
        });

        imageButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                CountDownTimer timer = new CountDownTimer(4000, 1000) {

                    @Override
                    public void onTick(long millisUntilFinished) {
                        imageButton.setVisibility(View.GONE);
                    }

                    @Override
                    public void onFinish() {
                        imageButton.setVisibility(View.VISIBLE);
                    }

                };

                timer.start();

                return true;
            }
        });

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddItemNow();
                AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
                animation1.setDuration(500);
                imageButton.startAnimation(animation1);

            }
        });

        return rootView;

    }

    public void AddItemNow() {
        final EditText input;

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Add item to list");
        input = new EditText(getActivity());
        int maxLength = 20;
        input.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });

        builder.setView(input);
        builder.setPositiveButton("Add item", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String item_name = input.getText().toString();

                if (item_name.equals("")) {
                    Toast.makeText(getActivity(), "Please enter a name for the item", Toast.LENGTH_SHORT).show();
                } else if (item_name.equals(" ")) {
                    Toast.makeText(getActivity(), "Please enter a name for the item", Toast.LENGTH_SHORT).show();
                } else if (item_name.equals("  ")) {
                    Toast.makeText(getActivity(), "Please enter a name for the item", Toast.LENGTH_SHORT).show();
                } else if (item_name.equals("This app sucks")) {
                    Toast.makeText(getActivity(), "No it doesn't!", Toast.LENGTH_SHORT).show();
                } else {

                    ItemClass itemClass = new ItemClass(item_name, 0);
                    db.addItemRunning(itemClass);
                    adapt.add(itemClass);
                    adapt.notifyDataSetChanged();
                    list = db.getAllItemsRunning();
                    adapt = new CustomAdapter4(getActivity(), R.layout.item_layout, list);
                    listItem = (ListView) getActivity().findViewById(R.id.listview_run);
                    listItem.setAdapter(adapt);
                    count = listItem.getCount();
                    if (count > 0) {
                        nothingtext.setVisibility(View.GONE);
                    } else if (count == 0) {
                        nothingtext.setVisibility(View.VISIBLE);
                    }
                }
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.show();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        ((MainActivityTrekkly) activity).onSectionAttached(6);
        MainActivityTrekkly mA = ((MainActivityTrekkly) getActivity());

        mA.restoreActionBar(Color.parseColor("#FF8000"));
    }

    private class CustomAdapter4 extends ArrayAdapter<ItemClass> {
        DataBaseHelper db;
        Context context;
        List<ItemClass> itemList = new ArrayList<ItemClass>();
        int layoutResourceId;

        public CustomAdapter4(Context context, int layoutResourceId, List<ItemClass> objects) {
            super(context, layoutResourceId, objects);
            this.layoutResourceId = layoutResourceId;
            this.itemList = objects;
            this.context = context;
        }

        @Override
        public View getView(int position, View otherView, ViewGroup parent) {

            CheckBox chk = null;
            ImageButton btn = null;
            if (otherView == null) {

                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                otherView = inflater.inflate(R.layout.item_layout, parent, false);
                listItem = (ListView) otherView.findViewById(R.id.listview_run);
                chk = (CheckBox) otherView.findViewById(R.id.checkbox);
                otherView.setTag(chk);
                final Running running = new Running();
                btn = (ImageButton) otherView.findViewById(R.id.delete);

                chk.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        CheckBox cbox = (CheckBox) v;
                        ItemClass otherTask = (ItemClass) cbox.getTag();
                        otherTask.setStatus(cbox.isChecked() == true ? 1 : 0);
                        if (cbox.isChecked()) {
                            cbox.setPaintFlags(cbox.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                        } else {
                            cbox.setPaintFlags(cbox.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                        }
                        //Allows you to have a line through the checkbox
                        DataBaseHelper.getInstance(context).upDateDataBaseR(otherTask);
                    }

                });

                btn.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        View view = (View) v.getParent();
                        CheckBox box = (CheckBox) view.findViewById(R.id.checkbox);
                        String DeleteObject = box.getText().toString();

                        //You have to obtain the string
                        String DeleteItem = String.format("DELETE FROM %s WHERE %s = '%s'",

                                DataBaseHelper.TABLE_NAME_RUNNING, DataBaseHelper.ITEM_NAME, DeleteObject);

                        db = new DataBaseHelper(getContext());

                        SQLiteDatabase sqLiteDataBase = db.getWritableDatabase();
                        sqLiteDataBase.execSQL(DeleteItem);
                        DataBaseHelper.getInstance(context).upDateDataBaseR(new ItemClass());

                        running.list = db.getAllItemsRunning();
                        running.adapt = new CustomAdapter4(getActivity(), R.layout.item_layout, running.list);
                        running.listItem = (ListView) rootView.findViewById(R.id.listview_run);
                        running.listItem.setAdapter(running.adapt);
                        running.nothingtext = (TextView) rootView.findViewById(R.id.nothing_here_running);
                        running.count = running.listItem.getCount();
                        if (running.count > 0) {
                            running.nothingtext.setVisibility(View.GONE);
                        } else if (running.count == 0) {
                            running.nothingtext.setVisibility(View.VISIBLE);
                        }
                        Log.i("UpDateDataBase()", "Was Called");
                    }
                });

            } else {

                chk = (CheckBox) otherView.getTag();
                Log.i("Else statement", "" + otherView.getTag());
            }

            //Loads the data
            ItemClass current = itemList.get(position);
            chk.setText(current.getName());
            chk.setChecked(current.getStatus() == 1 ? true : false);
            if (chk.isChecked()) {
                chk.setPaintFlags(chk.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            } else {
                chk.setPaintFlags(chk.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
            }
            chk.setTag(current);
            DataBaseHelper.getInstance(context).upDateDataBaseR(current);
            return otherView;
        }
    }
}