Advertisement
Guest User

MySqliteChoreList

a guest
Apr 8th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 31.66 KB | None | 0 0
  1. ----------------------------------------------------------XML FILES--------------------------------------------------------------------
  2.  
  3. ----------------------------------------------------------activity_man-----------------------------------------------------------------
  4. <!-- Login screen-->
  5. <LinearLayout
  6.     android:orientation="vertical"
  7.     android:layout_width="match_parent"
  8.     android:layout_height="match_parent"
  9.     android:background="@drawable/screen_background"
  10.     xmlns:android="http://schemas.android.com/apk/res/android">
  11.     <LinearLayout
  12.         android:layout_width="match_parent"
  13.         android:layout_height="match_parent"
  14.         android:layout_weight="5.8">
  15.         <TextView
  16.             android:layout_width="match_parent"
  17.             android:layout_height="wrap_content"
  18.             android:text="Welcome to MyChores"
  19.             android:textSize="25sp"
  20.             android:textColor="#c0c0c0"
  21.             android:gravity="center"
  22.             android:layout_marginTop="10sp"
  23.             />
  24.     </LinearLayout>
  25.  
  26.     <LinearLayout
  27.         android:layout_width="match_parent"
  28.         android:layout_height="match_parent"
  29.         android:layout_weight="4.3"
  30.         android:orientation="vertical">
  31.     <EditText
  32.         android:id="@+id/logUserId"
  33.         android:layout_width="match_parent"
  34.         android:layout_height="wrap_content"
  35.         android:textSize="35sp"
  36.         android:textColor="#878787"
  37.         android:padding="5sp"
  38.         android:gravity="center"
  39.         android:hint="Username"
  40.         android:layout_marginTop="20sp"
  41.         android:background="@drawable/input_text_shape"/>
  42.  
  43.     <EditText
  44.         android:id="@+id/logPassId"
  45.         android:layout_width="match_parent"
  46.         android:layout_height="wrap_content"
  47.         android:textSize="35sp"
  48.         android:textColor="#878787"
  49.         android:padding="5sp"
  50.         android:gravity="center"
  51.         android:inputType="textPassword"
  52.         android:hint="Password"
  53.         android:layout_marginTop="30sp"
  54.         android:background="@drawable/input_text_shape"/>
  55.     </LinearLayout>
  56.     <LinearLayout
  57.         android:layout_width="match_parent"
  58.         android:layout_height="match_parent"
  59.         android:orientation="vertical"
  60.         android:layout_weight="4">
  61.         <Button
  62.             android:id="@+id/loginBtnId"
  63.             android:layout_width="match_parent"
  64.             android:layout_height="wrap_content"
  65.             android:layout_gravity="center"
  66.             android:text="Log-In"
  67.             android:textSize="20sp"
  68.             android:textColor="#878787"
  69.             android:background="@drawable/btn_shape_round"/>
  70.         <Button
  71.             android:id="@+id/logRegBtnId"
  72.             android:layout_width="wrap_content"
  73.             android:layout_height="wrap_content"
  74.             android:layout_gravity="center"
  75.             android:layout_marginTop="25sp"
  76.             android:padding="10sp"
  77.             android:text="Register"
  78.             android:textSize="20sp"
  79.             android:textColor="#878787"
  80.             android:background="@drawable/btn_smpl_shape"/>
  81.  
  82.     </LinearLayout>
  83. </LinearLayout>
  84.  
  85. ---------------------------------------------------------------------------------------------------------------------------------------
  86.  
  87. ----------------------------------------------------------activity_registration--------------------------------------------------------
  88. <!-- registration -->
  89. <LinearLayout
  90.     android:layout_width="match_parent"
  91.     android:layout_height="match_parent"
  92.     android:orientation="vertical"
  93.     android:background="@drawable/screen_background"
  94.     xmlns:android="http://schemas.android.com/apk/res/android">
  95.  
  96.     <LinearLayout
  97.         android:layout_width="match_parent"
  98.         android:layout_height="match_parent"
  99.         android:orientation="vertical"
  100.         android:layout_weight="7.5">
  101.         <TextView
  102.             android:layout_width="match_parent"
  103.             android:layout_height="wrap_content"
  104.             android:text="Registration"
  105.             android:textSize="28sp"
  106.             android:textColor="#c0c0c0"
  107.             android:gravity="center"
  108.             android:layout_marginTop="20sp" />
  109.     </LinearLayout>
  110.  
  111.     <LinearLayout
  112.         android:layout_width="match_parent"
  113.         android:layout_height="match_parent"
  114.         android:orientation="vertical"
  115.         android:layout_weight="5.1">
  116.         <EditText
  117.             android:id="@+id/regUserId"
  118.             android:layout_width="match_parent"
  119.             android:layout_height="wrap_content"
  120.             android:textSize="35sp"
  121.             android:textColor="#878787"
  122.             android:padding="5sp"
  123.             android:hint="Username"
  124.             android:gravity="center"
  125.             android:layout_marginTop="15sp"
  126.             android:background="@drawable/input_text_shape"/>
  127.  
  128.         <EditText
  129.             android:id="@+id/regPassId"
  130.             android:layout_width="match_parent"
  131.             android:layout_height="wrap_content"
  132.             android:textSize="35sp"
  133.             android:textColor="#878787"
  134.             android:padding="5sp"
  135.             android:hint="Password"
  136.             android:inputType="textPassword"
  137.             android:gravity="center"
  138.             android:layout_marginTop="20sp"
  139.             android:background="@drawable/input_text_shape"/>
  140.  
  141.         <EditText
  142.             android:id="@+id/regRePasId"
  143.             android:layout_width="match_parent"
  144.             android:layout_height="wrap_content"
  145.             android:textSize="35sp"
  146.             android:textColor="#878787"
  147.             android:padding="5sp"
  148.             android:hint="Re-password"
  149.             android:inputType="textPassword"
  150.             android:gravity="center"
  151.             android:layout_marginTop="20sp"
  152.             android:background="@drawable/input_text_shape"/>
  153.  
  154.     </LinearLayout>
  155.  
  156.     <LinearLayout
  157.         android:layout_width="match_parent"
  158.         android:layout_height="match_parent"
  159.         android:orientation="vertical"
  160.         android:layout_weight="6">
  161.         <Button
  162.             android:id="@+id/regBtnId"
  163.             android:layout_width="match_parent"
  164.             android:layout_height="wrap_content"
  165.             android:layout_gravity="center"
  166.             android:layout_marginTop="10sp"
  167.             android:text="Register"
  168.             android:textSize="26sp"
  169.             android:textColor="#404040"
  170.             android:background="@drawable/btn_shape_round"/>
  171.     </LinearLayout>
  172.  
  173. </LinearLayout>
  174. ---------------------------------------------------------------------------------------------------------------------------------------
  175.  
  176. -------------------------------------------------------------activity_usertask---------------------------------------------------------
  177. <LinearLayout
  178.     android:layout_width="match_parent"
  179.     android:layout_height="match_parent"
  180.     android:orientation="vertical"
  181.     android:background="@drawable/screen_background"
  182.     xmlns:android="http://schemas.android.com/apk/res/android">
  183.     <LinearLayout
  184.         android:layout_width="match_parent"
  185.         android:layout_height="match_parent"
  186.         android:orientation="vertical"
  187.         android:layout_weight="8">
  188.         <TextView
  189.             android:id="@+id/sayHelloId"
  190.             android:layout_width="match_parent"
  191.             android:layout_height="wrap_content"
  192.             android:textSize="35sp"
  193.             android:textColor="#c0c0c0"
  194.             android:layout_marginTop="10sp"/>
  195.     </LinearLayout>
  196.  
  197.     <LinearLayout
  198.         android:layout_width="match_parent"
  199.         android:layout_height="match_parent"
  200.         android:orientation="vertical"
  201.         android:layout_weight="2">
  202.         <ListView
  203.             android:id="@+id/ChoreListId"
  204.             android:layout_width="match_parent"
  205.             android:layout_height="match_parent">
  206.         </ListView>
  207.     </LinearLayout>
  208. </LinearLayout>
  209. ---------------------------------------------------------------------------------------------------------------------------------------
  210.  
  211. -------------------------------------------------------------END XML-------------------------------------------------------------------
  212.  
  213. ==========================================================JAVA CODE====================================================================
  214.  
  215. ============================================================MainActivity===============================================================
  216. package com.example.igor.mychores20;
  217.  
  218. import android.app.Activity;
  219. import android.content.Intent;
  220. import android.content.SharedPreferences;
  221. import android.os.Bundle;
  222. import android.view.View;
  223. import android.widget.Button;
  224. import android.widget.CheckBox;
  225. import android.widget.EditText;
  226. import android.widget.Toast;
  227.  
  228. public class MainActivity extends Activity
  229. {
  230.     //strign variable that will contain the name of the user that recently logged in
  231.     private String isOnline;//<--contains the value of the logged user
  232.     //        username   password
  233.     EditText usrNamLog, passWoLog; //<-- will contain users input data
  234.     // LogIn button Register Button
  235.     Button btnLogIn, BtnRegUsr; //<-- will be used to connect variables with btn ids
  236.     //set instance of SharedPrefrence
  237.     SharedPreferences myPref;//<--will use the xml file that contains all the accounts
  238.     SharedPreferences.Editor editor;//<--will be used to edit our SharedPrefrences XML file
  239.  
  240.     @Override
  241.     protected void onCreate(Bundle savedInstanceState)
  242.     {
  243.         super.onCreate(savedInstanceState);
  244.         setContentView(R.layout.activity_main);
  245.         setPoints();//<-- connect all variables and SP
  246.         LogIn();//<--LogIn Button
  247.         regBtn();//<--Registration Button
  248.         //will move the user into logged in page if logged in the past
  249.         if(chkOnline())
  250.         {
  251.             Intent logIn = new Intent(MainActivity.this, UserTasks.class);
  252.            // logIn.putExtra("isOnline",isOnline);//<--put extra value on this intent
  253.             this.startActivity(logIn);//<--go to the UserTasks page
  254.         }
  255.     }
  256.  
  257.  
  258.  
  259.     //connects local variables with xml id's and sets up the sharedprefrences
  260.     private void setPoints()
  261.     {
  262.         //connect local variables with with XML id's
  263.         //EditTexts
  264.         usrNamLog = (EditText)findViewById(R.id.logUserId);//<--username
  265.         passWoLog = (EditText)findViewById(R.id.logPassId);//<--password
  266.         //Buttons
  267.         btnLogIn = (Button)findViewById(R.id.loginBtnId);//<--LogIn Button
  268.         BtnRegUsr = (Button)findViewById(R.id.logRegBtnId);//<--Register Button
  269.         //SharedPrefrences                                   sets up XML file that contains Users Data
  270.         myPref = getApplicationContext().getSharedPreferences(Utility.MY_ACC,MODE_PRIVATE);
  271.         //set editor up
  272.         editor = myPref.edit();
  273.     }
  274.  
  275.     //checks if user logged in the past or not
  276.     private boolean chkOnline()
  277.     {
  278.         //insert into variable the the Strign value of isOnline key
  279.         isOnline = myPref.getString(Utility.isUserON,"offline");//<--defualt value of offline to be inserted if key is empty
  280.         return !isOnline.equals("offline");//<--return true if isOnline variable doesnt equal "offline"
  281.     }
  282.  
  283.     //checks if users input of username & password are valid
  284.     private boolean usrLog()
  285.     {
  286.         String userName = usrNamLog.getText().toString();//<-- contains input of username field
  287.         String passWord = passWoLog.getText().toString();//<-- contains input of password field
  288.  
  289.         //check if fields are not empty and pop toast of error msg
  290.         if(userName.trim().isEmpty() || passWord.trim().isEmpty())
  291.         {
  292.             Toast.makeText(this,"Error, please make sure all fields are full",Toast.LENGTH_SHORT).show();
  293.             return false;//<-- return false and exit the method
  294.         }
  295.         //check if input data is invalid
  296.         if(!myPref.contains(userName) || !passWord.equals(myPref.getString(userName,passWord)))
  297.         {
  298.             Toast.makeText(this,"Error, wrong username or password",Toast.LENGTH_SHORT).show();
  299.             return false;//<-- return false and exit the method
  300.         }
  301.  
  302.         //if everything is correct insert key of username into isOnline keys value
  303.         editor.putString(Utility.isUserON,userName).commit();//<--insert usernames key as value into isOnline
  304.         return true;//<-- return true and exit the method
  305.     }
  306.     //LogIn button method checks if usrLog is true or false
  307.     public void LogIn()
  308.     {
  309.         btnLogIn.setOnClickListener(new View.OnClickListener() {
  310.             @Override
  311.             public void onClick(View v) {
  312.                 if (usrLog())//<-- if method returns true move to login page
  313.                 {
  314.                     Intent logIn = new Intent(MainActivity.this, UserTasks.class);
  315.                     Toast.makeText(MainActivity.this, "Loggin in", Toast.LENGTH_SHORT).show();
  316.                     MainActivity.this.startActivity(logIn);
  317.                 }
  318.             }
  319.         });
  320.     }
  321.     //Registration btn that takes the user to Registration page
  322.     public void regBtn()
  323.     {
  324.         BtnRegUsr.setOnClickListener(new View.OnClickListener()
  325.         {
  326.             @Override
  327.             public void onClick(View v)
  328.             {
  329.                 Intent register = new Intent(MainActivity.this,Registration.class);
  330.                 MainActivity.this.startActivity(register);//<--activate intent and move to registration page
  331.             }
  332.         });
  333.     }
  334. }
  335. =======================================================================================================================================
  336.  
  337. ============================================================Registration===============================================================
  338. package com.example.igor.mychores20;
  339.  
  340. import android.app.Activity;
  341. import android.content.SharedPreferences;
  342. import android.os.Bundle;
  343. import android.view.View;
  344. import android.widget.Button;
  345. import android.widget.EditText;
  346. import android.widget.Toast;
  347.  
  348. /**
  349.  * Created by Igor on 06-Apr-16.
  350.  */
  351. public class Registration extends Activity
  352. {
  353.     //      username   password   confirmation
  354.     private EditText userRegIn, passRegIn,passConfIn;
  355.     private  Button btnReg;//button instance
  356.  
  357.     private SharedPreferences myPrefs;//<-- sharedprefrences instance
  358.     private SharedPreferences.Editor editor; //<-- set up sharedprefrences editor instance
  359.  
  360.     @Override
  361.     protected void onCreate(Bundle savedInstanceState)
  362.     {
  363.         super.onCreate(savedInstanceState);
  364.         setContentView(R.layout.activity_registration);
  365.         setPoints();//<--set valiable points on in the oncreate method
  366.         regMe();//<--btn register method
  367.     }
  368.  
  369.     //method that sets all casting points and sharedprefrences here and being called by onCreate
  370.     private void setPoints()
  371.     {
  372.         //EditText casting local variables with xml id's
  373.         userRegIn = (EditText)findViewById(R.id.regUserId); //<-- username
  374.         passRegIn = (EditText)findViewById(R.id.regPassId); //<-- paassword
  375.         passConfIn = (EditText)findViewById(R.id.regRePasId); //<-- password confirmation
  376.  
  377.         //Button
  378.         btnReg = (Button)findViewById(R.id.regBtnId);//<-- button registration
  379.         //SharedPrefrences and its editors setup
  380.         //set the xml file of sharedprefrences to the final static String MY_ACC with mode_private on
  381.         myPrefs = getApplicationContext().getSharedPreferences(Utility.MY_ACC,MODE_PRIVATE);
  382.         //set the editor for myPrefs allowing to edit from the sharedPref xml files
  383.         editor = myPrefs.edit();
  384.     }
  385.     //boolean method that handels user input and checks if all data inputed is valid and returns true or false
  386.     private boolean signMeUp()
  387.     {
  388.         String userName = userRegIn.getText().toString();//<-- put all user input of userRegId into this var
  389.         String passWord = passRegIn.getText().toString();//<--put all pass input of passRegIn into this var
  390.         String confPass = passConfIn.getText().toString();//<--put all passConf input of passConfIn into this var
  391.         //if data is missing in one of the inputs throw toast message and return false
  392.         if(userName.trim().isEmpty() || passWord.trim().isEmpty() || confPass.trim().isEmpty())
  393.         {
  394.             Toast.makeText(this,"Please make sure you filled all the data",Toast.LENGTH_SHORT).show();
  395.             return false;
  396.         }
  397.         //if password and confirm password do not match throw toast and return false
  398.         if(!confPass.equals(passWord))
  399.         {
  400.             Toast.makeText(this,"Error, please make sure passwords match",Toast.LENGTH_SHORT).show();
  401.             return false;
  402.         }
  403.         //if username allready exists as a myPrefs key throw toast msg and return false
  404.         if(myPrefs.contains(userName))
  405.         {
  406.             Toast.makeText(this,"Error, this username allready exists",Toast.LENGTH_SHORT).show();
  407.             return false;
  408.         }
  409.         //if none of the above happened
  410.         editor.putString(userName,passWord).commit(); //<--put data in the xml file of myPref to be used as username and password
  411.         return true;//<-- return true
  412.  
  413.     }
  414.  
  415.     //set up button method that will activate registration algorithem method
  416.     public void regMe()
  417.     {
  418.         btnReg.setOnClickListener(new View.OnClickListener()
  419.         {
  420.             @Override
  421.             public void onClick(View v)
  422.             {
  423.                 if(signMeUp())//<-- if method returns true pop toast and exit this page
  424.                 {
  425.                     Toast.makeText(Registration.this,"Registered succesfully",Toast.LENGTH_SHORT).show();
  426.                     finish();//<-- finish this activity and return to mainpage
  427.                 }
  428.             }
  429.         });
  430.     }
  431. }
  432.  
  433. =======================================================================================================================================
  434.  
  435. ============================================================UserTasks==================================================================
  436. package com.example.igor.mychores20;
  437.  
  438.  
  439. import android.content.SharedPreferences;
  440. import android.os.Bundle;
  441. import android.support.v7.app.AppCompatActivity;
  442. import android.view.Menu;
  443. import android.view.MenuInflater;
  444. import android.view.MenuItem;
  445. import android.widget.ListView;
  446. import android.widget.TextView;
  447. import android.widget.Toast;
  448.  
  449. /**
  450.  * Created by Igor on 06-Apr-16.
  451.  */
  452. //TODO Create a chores app that will consist of 3 screens
  453. //TODO (1 Login Screen, (2 Registration Screen, (3 Online Screen containing the listView of chores
  454. public class UserTasks extends AppCompatActivity
  455. {
  456.     //TODO --CREATE page with TextView 'that says hi' to the user and ListView containing users chores
  457.     private TextView sayHiTxt;//<--say hi variable
  458.     protected static ListView choreList;//<--users chores listView
  459.     //SharedPrefrences
  460.     private SharedPreferences myPref;
  461.     private SharedPreferences.Editor editor;
  462.     private Utility myUtility;
  463.     private MyDbManager db;
  464.     @Override
  465.     protected void onCreate(Bundle savedInstanceState)
  466.     {
  467.         super.onCreate(savedInstanceState);
  468.         setContentView(R.layout.activity_usertasks);
  469.         setPoints();//<--set valuable points
  470.         sayHiTxt.setText("Hello " + getUserName());//<--says hello to the user
  471.         db = new MyDbManager(this,getUserName());//<--set db with username table
  472.  
  473.         if(choreList != null)//<--if chore list is not empty update ListView
  474.         {
  475.             MyAdapter adp = new MyAdapter(this,getUserName());
  476.             choreList.setAdapter(adp);
  477.         }
  478.     }
  479.  
  480.     //get menut oncreate options
  481.     @Override
  482.     public boolean onCreateOptionsMenu(Menu menu)
  483.     {
  484.         MenuInflater ifnlater = getMenuInflater();//<-- call menu inflater class and equal it to getMenuInflater method
  485.         ifnlater.inflate(R.menu.my_menu, menu);//<-- inflate the wished menu (my_menu)
  486.         return super.onCreateOptionsMenu(menu);
  487.     }
  488.  
  489.     @Override
  490.     public boolean onOptionsItemSelected(MenuItem item)
  491.     {
  492.         switch (item.getItemId())
  493.         {
  494.             case R.id.addTaskId:
  495.                 myUtility.alrtDialog("add new task", "add task", new Utility.AdapterCallback()
  496.                 {
  497.                     @Override
  498.                     public void onCallback(String result)
  499.                     {
  500.                      if(db.newChore(result))
  501.                      {
  502.                          Toast.makeText(UserTasks.this, "Chore added", Toast.LENGTH_SHORT).show();
  503.                          myUtility.setAdapter();
  504.                      }
  505.                      else
  506.                          Toast.makeText(UserTasks.this,"Error, couldnt add chore",Toast.LENGTH_SHORT).show();
  507.                     }
  508.                 });
  509.                 break;
  510.             case R.id.rmvDoneId:
  511.                 db.removDone();
  512.                 myUtility.setAdapter();
  513.                 break;
  514.             case R.id.logOutId:
  515.                 logOut();
  516.                 break;
  517.         }
  518.         return super.onOptionsItemSelected(item);
  519.     }
  520.  
  521.     //set points of local variables and sharedprefrences
  522.     private void setPoints()
  523.     {
  524.         //connect local variables with XML id's
  525.         sayHiTxt = (TextView)findViewById(R.id.sayHelloId);//<--textview
  526.         choreList = (ListView)findViewById(R.id.ChoreListId);//<--listview
  527.         //set sharePrefrences
  528.         myPref = getApplicationContext().getSharedPreferences(Utility.MY_ACC, MODE_PRIVATE);
  529.         editor = myPref.edit();
  530.         myUtility = new Utility(this,getUserName());//<--utility constractor initiation
  531.     }
  532.  
  533.     private String getUserName()
  534.     {
  535.         String username = myPref.getString("isOnline","offline");
  536.         return username;//<-- return usernames string value
  537.     }
  538.     //logout method used in the menu select button
  539.     private void logOut()
  540.     {
  541.         editor.remove(Utility.isUserON).commit();
  542.         finish();
  543.     }
  544. }
  545.  
  546. =======================================================================================================================================
  547.  
  548. =============================================================MyDbManager===============================================================
  549. package com.example.igor.mychores20;
  550.  
  551. import android.content.ContentValues;
  552. import android.content.Context;
  553. import android.database.Cursor;
  554. import android.database.sqlite.SQLiteDatabase;
  555. import android.database.sqlite.SQLiteOpenHelper;
  556. import android.database.sqlite.SQLiteStatement;
  557.  
  558. import java.util.ArrayList;
  559. import java.util.List;
  560.  
  561. /**
  562.  * Created by Igor on 07-Apr-16.
  563.  */
  564. //TODO CREATE database recieving data from user input with a table for each user
  565. //TODO (1 DataBase file. (2 Agile table. (3 allow user write, read and delete from his private table in DB
  566. public class MyDbManager extends SQLiteOpenHelper
  567. {
  568.     private static final String DB_NAME = "chores.db";//<--DataBase file name in a final static variable
  569.     private static String table_name;//<-- table name is agile for each user to have its own table
  570.     private SQLiteDatabase db; //<-- create instance of SQLiteDataBase to set Clearance of usage of the db and its editing
  571.     private List<Task> list;
  572.     private Context context;
  573.  
  574.     public MyDbManager(Context context,String table_name)
  575.     {
  576.         super(context, DB_NAME, null, 1);//<-- recieve context from called class, db file name factory of null and verion 1
  577.         this.context = context;//<--context
  578.         this.table_name = table_name;//<--set the table_name as usernames name
  579.         db = this.getWritableDatabase();//<-- set clearance that allows to write and read from database
  580.         db.execSQL("CREATE TABLE IF NOT EXISTS " + table_name +" (id INTEGER PRIMARY KEY AUTOINCREMENT,task TEXT,status INTEGER)");
  581.         list = new ArrayList<Task>();//<--intiate the array each time this constractor is called
  582.     }
  583.  
  584.     @Override
  585.     public void onCreate(SQLiteDatabase db)
  586.     {
  587.         //create table with users name and insert in it 3 rows
  588.         //(1 id automaticly inserted by the system (2 task users chore (3 status integer of 0 undone 1 done
  589.         String query = "CREATE TABLE IF NOT EXISTS " + table_name +" (id INTEGER PRIMARY KEY AUTOINCREMENT,task TEXT,status INTEGER)";
  590.         db.execSQL(query);
  591.     }
  592.  
  593.     //insert chore method
  594.     public boolean newChore(String choreName)
  595.     {
  596.         //insert db insert data command into 'inserChore' String variable
  597.         //insert users chore with defualt status of 0 (undone)
  598.         String insertChore = "INSERT INTO "+table_name+" (task,status) VALUES('"+choreName+"',0)";
  599.         //create SQLite statemnt and compile it with insertChore variable
  600.         SQLiteStatement statement = db.compileStatement(insertChore);
  601.         long res = statement.executeInsert();//<--insert statement execcute in a long variable
  602.         //to check if data got inserted into DB
  603.         return (res!=-1);//<--if res doesnt equal to -1 return true (data was inserted succesfully
  604.     }
  605.  
  606.  
  607.     //display in a list of a custom class Task which holds 3 variables for each column
  608.     public List<Task> getChoreList()//<--full display method
  609.     {
  610.         //create Cursor class and intitate it to hold all the data from table name
  611.         Cursor res =  db.rawQuery("SELECT * FROM " + table_name, null);
  612.  
  613.         while(res.moveToNext())//<--as long there is a next row continue runing
  614.         {
  615.             //insert all data from the Database (id, task, status)
  616.             list.add(new Task(res.getInt(0),res.getString(1),res.getInt(2)));//<--add it into list
  617.         }
  618.         return list;//<-- return all the values from the list
  619.     }
  620.  
  621.     //method that allows to share task with a friend
  622.     //table gets Sring from userInput of the AlertDialog
  623.     //the id, chore and status get their inout from adapter
  624.     public void shareFriend(String table_name,String choreName,int status)
  625.     {
  626.         //set username to recieve choesen taskname and its status
  627.         String shareChore = "INSERT INTO "+table_name+" (task,status) VALUES('"+choreName+"',"+status+")";
  628.         SQLiteStatement statement = db.compileStatement(shareChore);//<--insert taskname into statement and compile
  629.         statement.executeInsert();//<--execute statement and insert it into long variable
  630.     }
  631.  
  632.     //change chores name using the input variables in the method
  633.     public void choreUpdate(int id, String taskName, int taskStatus)
  634.     {
  635.         ContentValues myContent = new ContentValues();
  636.         myContent.put("id",id);
  637.         myContent.put("task",taskName);
  638.         myContent.put("status",taskStatus);
  639.         db.update(table_name, myContent, "id = ?", new String[]{id + ""});
  640.     }
  641.  
  642.     //remove chore method
  643.     public void delChore(int id)//recieve position
  644.     {
  645.         //remove the whole row using the id number of the row
  646.         db.execSQL("DELETE FROM " + table_name + " WHERE id ='"+id+"'");//delete task with chosen id
  647.     }
  648.  
  649.     //remove all done methods
  650.     public void removDone()
  651.     {
  652.         db.execSQL("DELETE FROM " + table_name + " WHERE status = 1");//delete all finished tasks
  653.     }
  654.  
  655.     //DO NOT TOUCH BEN IEM TIPAGACH!!
  656.     @Override
  657.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  658.     {
  659.  
  660.     }
  661. }
  662. =======================================================================================================================================
  663.  
  664. ==============================================================Utility==================================================================
  665. package com.example.igor.mychores20;
  666.  
  667. import android.app.AlertDialog;
  668. import android.content.Context;
  669. import android.content.DialogInterface;
  670. import android.text.InputType;
  671. import android.widget.EditText;
  672.  
  673. /**
  674.  * Created by Igor on 07-Apr-16.
  675.  */
  676. public class Utility
  677. {
  678.     //setting up sharedprefrences xml file name that will store all usernames and passwords
  679.     public static final String MY_ACC = "accounts";
  680.     public static final String isUserON = "isOnline";
  681.  
  682.     private Context context;
  683.     private String userName;
  684.  
  685.  
  686.     public Utility(Context context,String userName)
  687.     {
  688.         this.context = context;
  689.         this.userName = userName;
  690.     }
  691.     //set adapter method refreshes the adapter
  692.     protected void setAdapter()
  693.     {
  694.         MyAdapter adp = new MyAdapter(context,userName);
  695.         UserTasks.choreList.setAdapter(adp);
  696.     }
  697.  
  698.     //create interface with void method that requires String
  699.     public interface AdapterCallback //<--set interface to be abel to call method to implant
  700.     {
  701.         void onCallback(String usrInput);//<--returns what user has typed
  702.         //this abstract method will allow us to recieve alrtDialog() String input
  703.         //if the place we wish to implant alrtDialog() method!
  704.     }
  705.     //requires name of the title, name of the positive btn and implantation of the interface class /*AdapterCallback*/
  706.     public void alrtDialog(String setTitle, String setPositiveBtn, final AdapterCallback callback)
  707.     {
  708.         AlertDialog.Builder myDialog = new AlertDialog.Builder(context);//<--create alert dialog builder
  709.         myDialog.setTitle(setTitle);//<--inserts the string for the title
  710.         //create edittext for the user to imput its method
  711.         final EditText myText = new EditText(context);//<--intiate EditText
  712.         myText.setInputType(InputType.TYPE_CLASS_TEXT);//<--declare imput type
  713.         //posetive button
  714.         myDialog.setPositiveButton(setPositiveBtn, new DialogInterface.OnClickListener() {
  715.  
  716.             @Override
  717.             public void onClick(DialogInterface dialog, int which) {
  718.                 ;
  719.                 callback.onCallback(myText.getText().toString());
  720.             }
  721.         });
  722.         //negetive button
  723.         myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  724.             @Override
  725.             public void onClick(DialogInterface dialog, int which) {
  726.                 dialog.cancel();
  727.             }
  728.         });
  729.         myDialog.setView(myText);//<-set the view of the alrt dialog
  730.         myDialog.show();
  731.     }
  732.  
  733.     //TODO Delete method that deltes tasks by their id numbers
  734.     //TODO create graphics and desgin the app
  735.     //TODO profit
  736.  
  737. }
  738. =======================================================================================================================================
  739.  
  740. ===============================================================Tasks===================================================================
  741. package com.example.igor.mychores20;
  742.  
  743. /**
  744.  * Created by Igor on 08-Apr-16.
  745.  */
  746. public class Task
  747. {
  748.     //create class to store all the data from database in this class
  749.  
  750.     private int id;
  751.     private String taskName;
  752.     private int status;
  753.  
  754.     //constractor that recieves input of taskName and status
  755.     public Task(int id,String taskName, int status)
  756.     {
  757.         this.id = id;
  758.         this.taskName = taskName;
  759.         this.status = status;
  760.     }
  761.  
  762.     //name getter and setter
  763.     public String getTaskName()//<--getter
  764.     {
  765.         return taskName;
  766.     }
  767.     //name getter and setter
  768.     public void setTaskName(String taskName)//<--setter
  769.     {
  770.         this.taskName = taskName;
  771.     }
  772.  
  773.     public int getStatus()//<--getter
  774.     {
  775.         return status;
  776.     }
  777.  
  778.     public void setStatus(int status)//<--setter
  779.     {
  780.         this.status = status;
  781.     }
  782.  
  783.     public int getId() //<--getter
  784.     {
  785.         return id;
  786.     }
  787.  
  788.     public void setId(int id) //<--setter
  789.     {
  790.         this.id = id;
  791.     }
  792. }
  793. =======================================================================================================================================
  794.  
  795. ============================================================END========================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement