Advertisement
Guest User

zeev mindali - sql HW

a guest
Jul 7th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.21 KB | None | 0 0
  1. MainActivity.java
  2. ==================
  3. package com.example.zeev.sqlcrud;
  4.  
  5.  
  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.database.Cursor;
  9. import android.database.SQLException;
  10. import android.database.sqlite.SQLiteDatabase;
  11. import android.graphics.Color;
  12. import android.os.Bundle;
  13. import android.view.View;
  14. import android.widget.EditText;
  15. import android.widget.LinearLayout;
  16. import android.widget.ListView;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20.  
  21. public class MainActivity extends Activity
  22. {
  23.     //place for pointers
  24.     EditText input;
  25.     LinearLayout taskCont;
  26.     SQLiteDatabase db;
  27.     Context context;
  28.  
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState)
  31.     {
  32.         super.onCreate(savedInstanceState);
  33.         this.setContentView(R.layout.activity_main);
  34.  
  35.         //pointers for screen elements
  36.         input=(EditText)findViewById(R.id.input);
  37.         taskCont=(LinearLayout)findViewById(R.id.taskCont);
  38.         context=this;
  39.  
  40.         //using DBmanage to make a conncter to data base
  41.         db=(new DBmanage(this)).getWritableDatabase();
  42.         db.execSQL("CREATE TABLE IF NOT EXISTS myTaskList(id INTEGER PRIMARY KEY , taskName VARCHAR, done BOOLEAN DEFAULT 0)");
  43.  
  44.         //get data that already stored in table
  45.         retriveData();
  46.     }
  47.  
  48.     public void clearTaskList(View v)
  49.     {
  50.         //erase all task that is done
  51.         db.execSQL("DELETE FROM myTaskList where done=1");
  52.         //get data in table
  53.         retriveData();
  54.     }
  55.  
  56.     private void retriveData()
  57.     {
  58.         try
  59.         {
  60.             //clear view
  61.             taskCont.removeAllViews();
  62.             //set Cursor to get data
  63.             Cursor c = db.rawQuery("SELECT * FROM myTaskList", null);
  64.             //move cursor to start, stop condition when cursor after last, for each item move next
  65.             for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
  66.             {
  67.                 //create new TextView
  68.                 TextView task = new TextView(this);
  69.                 //set text into TextView , column(1) is taskName
  70.                 task.setText(c.getString(1));
  71.                 //check if task is done, when value of column(2) is 1 set background as green
  72.                 if (c.getInt(2)==1) task.setBackgroundColor(Color.GREEN);
  73.                 //create taskName for later user
  74.                 final String taskName=c.getString(1);
  75.                 //create listener when long click will fire event that will set task is done
  76.                 task.setOnLongClickListener(new View.OnLongClickListener()
  77.                 {
  78.                     @Override
  79.                     public boolean onLongClick(View view)
  80.                     {
  81.                         //set task is done by task name
  82.                         db.execSQL("UPDATE myTaskList SET done=1 WHERE taskName='"+taskName+"'");
  83.                         //make toast to message user that task is done
  84.                         Toast.makeText(context,"TASK "+taskName+" IS FINISHED",Toast.LENGTH_LONG).show();
  85.                         return true;
  86.                     }
  87.                 });
  88.                 //add text view to contianer
  89.                 taskCont.addView(task);
  90.             }
  91.         }
  92.         catch (SQLException e)
  93.         {
  94.             //if there is not task, inform user
  95.             Toast.makeText(this,"NO TASK YET",Toast.LENGTH_LONG).show();
  96.         }
  97.     }
  98.     public void runStmt(View v)
  99.     {
  100.         String newTask=input.getText().toString();
  101.  
  102.         try
  103.         {
  104.             //check if task is not empty
  105.             if (newTask.isEmpty())
  106.             {
  107.                 //alert user that task is empty
  108.                 Toast.makeText(this,"TASK CAN NOT BE EMPTY",Toast.LENGTH_LONG).show();
  109.                 //return from method, do not preform any command
  110.                 return;
  111.             }
  112.             //insert data into table, we don't need to set done since it's defualt value is 0 (false)
  113.             db.execSQL("INSERT INTO myTaskList (taskName) VALUES('"+newTask+"')");
  114.             //clear input text box so we will not create duplicate values
  115.             input.setText("");
  116.             //show all data in table
  117.             retriveData();
  118.         }
  119.         catch (SQLException e)
  120.         {
  121.             //alert user that we have error in sql statment
  122.             Toast.makeText(this,"ERROR IN SQL!!",Toast.LENGTH_LONG).show();
  123.  
  124.         }
  125.     }
  126. }
  127.  
  128.  
  129.  
  130. DBmanage.java
  131. =============
  132. package com.example.zeev.sqlcrud;
  133.  
  134. import android.content.Context;
  135. import android.database.DatabaseErrorHandler;
  136. import android.database.sqlite.SQLiteDatabase;
  137. import android.database.sqlite.SQLiteOpenHelper;
  138.  
  139. /**
  140.  * Created by zeev on 7/6/15.
  141.  */
  142. public class DBmanage extends SQLiteOpenHelper
  143. {
  144.  
  145.     public DBmanage(Context context)
  146.     {
  147.         super(context, "myDB", null, 1);
  148.     }
  149.  
  150.     @Override
  151.     public void onCreate(SQLiteDatabase sqLiteDatabase)
  152.     {
  153.         /* */
  154.  
  155.     }
  156.  
  157.     @Override
  158.     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
  159.     {
  160.  
  161.     }
  162.  
  163. }
  164.  
  165.  
  166. activity_main.xml
  167. ==================
  168. <RelativeLayout
  169.     android:layout_width="match_parent"
  170.     android:layout_height="match_parent"
  171.     xmlns:android="http://schemas.android.com/apk/res/android">
  172.     <EditText
  173.         android:layout_width="200sp"
  174.         android:layout_height="wrap_content"
  175.         android:id="@+id/input"
  176.         android:hint="enter task"
  177.         />
  178.     <Button
  179.         android:layout_width="100sp"
  180.         android:layout_height="wrap_content"
  181.         android:text="SUBMIT"
  182.         android:id="@+id/runBtn"
  183.         android:onClick="runStmt"
  184.         android:layout_toRightOf="@+id/input"/>
  185.     <LinearLayout
  186.         android:layout_width="match_parent"
  187.         android:layout_height="wrap_content"
  188.         android:orientation="vertical"
  189.         android:id="@+id/taskCont"
  190.         android:layout_below="@id/input">
  191.     </LinearLayout>
  192.     <Button
  193.         android:layout_width="match_parent"
  194.         android:layout_height="wrap_content"
  195.         android:id="@+id/btnClear"
  196.         android:text="CLEAR ALL TASKS"
  197.         android:layout_below="@+id/taskCont"
  198.         android:onClick="clearTaskList"/>
  199.     </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement