Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- ==================
- package com.example.zeev.sqlcrud;
- import android.app.Activity;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity
- {
- //place for pointers
- EditText input;
- LinearLayout taskCont;
- SQLiteDatabase db;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.activity_main);
- //pointers for screen elements
- input=(EditText)findViewById(R.id.input);
- taskCont=(LinearLayout)findViewById(R.id.taskCont);
- context=this;
- //using DBmanage to make a conncter to data base
- db=(new DBmanage(this)).getWritableDatabase();
- db.execSQL("CREATE TABLE IF NOT EXISTS myTaskList(id INTEGER PRIMARY KEY , taskName VARCHAR, done BOOLEAN DEFAULT 0)");
- //get data that already stored in table
- retriveData();
- }
- public void clearTaskList(View v)
- {
- //erase all task that is done
- db.execSQL("DELETE FROM myTaskList where done=1");
- //get data in table
- retriveData();
- }
- private void retriveData()
- {
- try
- {
- //clear view
- taskCont.removeAllViews();
- //set Cursor to get data
- Cursor c = db.rawQuery("SELECT * FROM myTaskList", null);
- //move cursor to start, stop condition when cursor after last, for each item move next
- for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
- {
- //create new TextView
- TextView task = new TextView(this);
- //set text into TextView , column(1) is taskName
- task.setText(c.getString(1));
- //check if task is done, when value of column(2) is 1 set background as green
- if (c.getInt(2)==1) task.setBackgroundColor(Color.GREEN);
- //create taskName for later user
- final String taskName=c.getString(1);
- //create listener when long click will fire event that will set task is done
- task.setOnLongClickListener(new View.OnLongClickListener()
- {
- @Override
- public boolean onLongClick(View view)
- {
- //set task is done by task name
- db.execSQL("UPDATE myTaskList SET done=1 WHERE taskName='"+taskName+"'");
- //make toast to message user that task is done
- Toast.makeText(context,"TASK "+taskName+" IS FINISHED",Toast.LENGTH_LONG).show();
- return true;
- }
- });
- //add text view to contianer
- taskCont.addView(task);
- }
- }
- catch (SQLException e)
- {
- //if there is not task, inform user
- Toast.makeText(this,"NO TASK YET",Toast.LENGTH_LONG).show();
- }
- }
- public void runStmt(View v)
- {
- String newTask=input.getText().toString();
- try
- {
- //check if task is not empty
- if (newTask.isEmpty())
- {
- //alert user that task is empty
- Toast.makeText(this,"TASK CAN NOT BE EMPTY",Toast.LENGTH_LONG).show();
- //return from method, do not preform any command
- return;
- }
- //insert data into table, we don't need to set done since it's defualt value is 0 (false)
- db.execSQL("INSERT INTO myTaskList (taskName) VALUES('"+newTask+"')");
- //clear input text box so we will not create duplicate values
- input.setText("");
- //show all data in table
- retriveData();
- }
- catch (SQLException e)
- {
- //alert user that we have error in sql statment
- Toast.makeText(this,"ERROR IN SQL!!",Toast.LENGTH_LONG).show();
- }
- }
- }
- DBmanage.java
- =============
- package com.example.zeev.sqlcrud;
- import android.content.Context;
- import android.database.DatabaseErrorHandler;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- /**
- * Created by zeev on 7/6/15.
- */
- public class DBmanage extends SQLiteOpenHelper
- {
- public DBmanage(Context context)
- {
- super(context, "myDB", null, 1);
- }
- @Override
- public void onCreate(SQLiteDatabase sqLiteDatabase)
- {
- /* */
- }
- @Override
- public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
- {
- }
- }
- activity_main.xml
- ==================
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <EditText
- android:layout_width="200sp"
- android:layout_height="wrap_content"
- android:id="@+id/input"
- android:hint="enter task"
- />
- <Button
- android:layout_width="100sp"
- android:layout_height="wrap_content"
- android:text="SUBMIT"
- android:id="@+id/runBtn"
- android:onClick="runStmt"
- android:layout_toRightOf="@+id/input"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:id="@+id/taskCont"
- android:layout_below="@id/input">
- </LinearLayout>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnClear"
- android:text="CLEAR ALL TASKS"
- android:layout_below="@+id/taskCont"
- android:onClick="clearTaskList"/>
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement