Java tutorial
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 Skiing extends Fragment { View rootView; DataBaseHelper db; List<ItemClass> list; CustomAdapter5 adapt; ListView listItem; TextView nothingtext; int count; public static Skiing newInstance() { Skiing fragment = new Skiing(); return fragment; } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.skiing_fragment_layout, container, false); this.rootView = rootView; db = new DataBaseHelper(getActivity()); list = db.getAllItemsSkiing(); adapt = new CustomAdapter5(getActivity(), R.layout.item_layout, list); listItem = (ListView) rootView.findViewById(R.id.listview_ski); listItem.setAdapter(adapt); nothingtext = (TextView) rootView.findViewById(R.id.nothing_here_ski); 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_skiing); 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.addItemSkiing(itemClass); adapt.add(itemClass); adapt.notifyDataSetChanged(); list = db.getAllItemsSkiing(); adapt = new CustomAdapter5(getActivity(), R.layout.item_layout, list); listItem = (ListView) getActivity().findViewById(R.id.listview_ski); 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(7); MainActivityTrekkly mA = ((MainActivityTrekkly) getActivity()); mA.restoreActionBar(Color.parseColor("#79CDCD")); } private class CustomAdapter5 extends ArrayAdapter<ItemClass> { DataBaseHelper db; Context context; List<ItemClass> itemList = new ArrayList<ItemClass>(); int layoutResourceId; public CustomAdapter5(Context context, int layoutResourceId, List<ItemClass> objects) { super(context, layoutResourceId, objects); this.layoutResourceId = layoutResourceId; this.itemList = objects; this.context = context; } /** * This method will DEFINE what the view inside the list view will * finally look like. */ @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_ski); chk = (CheckBox) otherView.findViewById(R.id.checkbox); otherView.setTag(chk); final Skiing skiing = new Skiing(); 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)); } DataBaseHelper.getInstance(context).upDateDataBaseSK(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_SKIING, DataBaseHelper.ITEM_NAME, DeleteObject); db = new DataBaseHelper(getContext()); //You need this line SQLiteDatabase sqLiteDataBase = db.getWritableDatabase(); sqLiteDataBase.execSQL(DeleteItem); DataBaseHelper.getInstance(context).upDateDataBaseSK(new ItemClass()); skiing.list = db.getAllItemsSkiing(); skiing.adapt = new CustomAdapter5(getActivity(), R.layout.item_layout, skiing.list); skiing.listItem = (ListView) rootView.findViewById(R.id.listview_ski); skiing.listItem.setAdapter(skiing.adapt); skiing.nothingtext = (TextView) rootView.findViewById(R.id.nothing_here_ski); skiing.count = skiing.listItem.getCount(); if (skiing.count > 0) { skiing.nothingtext.setVisibility(View.GONE); } else if (skiing.count == 0) { skiing.nothingtext.setVisibility(View.VISIBLE); } Log.i("UpDateDataBase()", "Was Called"); } }); } else { chk = (CheckBox) otherView.getTag(); Log.i("Else statement", "" + otherView.getTag()); //The error is around here, it has to do with getting the TAG //Make unique itemclass for each table } 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).upDateDataBaseSK(current); return otherView; } } }