Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity
- package com.example.teacher.mytasks;
- import android.content.Context;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- EditText txtUser, txtPass;
- Button btnLogin, btnRegister;
- Context context;
- UtlUserSP userSP = new UtlUserSP();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- this.context = this;
- userSP.setContext(this);
- txtUser = findViewById(R.id.txtUser);
- txtPass = findViewById(R.id.txtPass);
- btnLogin = findViewById(R.id.btnLogin);
- btnRegister = findViewById(R.id.btnRegister);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- startActivity(new Intent(context, RegisterActivity.class));
- }
- });
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {/*
- String userName = txtUser.getText().toString();
- String userPass = txtPass.getText().toString();
- if (userSP.checkUser(userName, userPass)) {
- //Toast.makeText(context, "User o.k.", Toast.LENGTH_SHORT).show();
- Intent myIntent = new Intent(context,TaskActivity.class);
- //insert data for the intent that we goint to start
- myIntent.putExtra("userName",userName);
- //start intent
- startActivity(myIntent);
- }
- else
- {
- Toast.makeText(context, "Error in user name or password", Toast.LENGTH_SHORT).show();
- txtUser.setText("");
- txtPass.setText("");
- }
- */
- //for byPass the login
- Intent myIntent = new Intent(context, TaskActivity.class);
- myIntent.putExtra("userName","Zeev");
- startActivity(myIntent);
- }
- });
- }
- }
- -------------------------------------------
- RegisterActivity
- package com.example.teacher.mytasks;
- import android.content.Context;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- /**
- * Created by teacher on 12/21/2017.
- */
- public class RegisterActivity extends AppCompatActivity {
- Context context;
- EditText regUser,regPass1,regPass2;
- Button btnRegister,btnCancel;
- UtlUserSP userUtil=new UtlUserSP();
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- setPointer();
- }
- private void setPointer() {
- this.context=this;
- userUtil.setContext(context);
- regUser=findViewById(R.id.regUserName);
- regPass1=findViewById(R.id.regPass1);
- regPass2=findViewById(R.id.regPass2);
- btnCancel=findViewById(R.id.btnRegCancel);
- btnCancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- finish();
- }
- });
- btnRegister =findViewById(R.id.btnRegRegister);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //check if password o.k.
- if (!regPass1.getText().toString().equals(regPass2.getText().toString()))
- {
- Toast.makeText (context, "Password not match", Toast.LENGTH_SHORT).show();
- return;
- }
- if (regPass1.getText().toString().length()<3)
- {
- Toast.makeText(context, "Password must be 3 letter minimum", Toast.LENGTH_SHORT).show();
- return;
- }
- if(!userUtil.registerUser(regUser.getText().toString(),regPass1.getText().toString()))
- {
- Toast.makeText(context, "Error in user registration....", Toast.LENGTH_SHORT).show();
- return;
- }
- Toast.makeText(context, "new user is add...", Toast.LENGTH_SHORT).show();
- finish();
- }
- });
- }
- }
- -----------------------------------------------
- SQLite
- package com.example.teacher.mytasks;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteException;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteStatement;
- import android.util.Log;
- import java.util.Date;
- /**
- * Created by teacher on 1/4/2018.
- */
- public class SQLite extends SQLiteOpenHelper implements TaskAble {
- //name of the DB file in our phone
- public static final String DB_NAME = "task.db";
- //name of the table
- private String tableName = "myTask";
- //as instance of the SQLiteDataBase
- SQLiteDatabase db;
- public SQLite(Context context) {
- super(context, DB_NAME, null, 1);
- //open our db for read write
- db = this.getWritableDatabase();
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- String sql = "CREATE TABLE IF NOT EXISTS " + this.tableName + " ";
- // type - INTEGER , TEXT
- // key (index) - PRIMARY KEY
- // +=1 - AUTOINCREMENT
- sql += "(id INTEGER PRIMARY KEY AUTOINCREMENT, taskName TEXT, isDone INTEGER, date TEXT)";
- db.execSQL(sql);
- }
- @Override
- public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
- //do not touch
- }
- //METHOD I - using content values (preventing SQL injection)
- public boolean newTask(String taskName) {
- //create instance of content values to hold our value
- ContentValues myValues = new ContentValues();
- //insert data by key and value
- myValues.put("taskName", taskName);
- myValues.put("isDone", 0);
- myValues.put("date", new Date().toString());
- //putting our values into table and getting a result which reflact record id(-1 indicates an error)
- long res = db.insert(this.tableName, null, myValues);
- return res != (-1);
- }
- //METHOD II - using SQL statment
- public boolean newTaskSQL(String taskName) {
- //create sql String -> INSERT INTO [table name] (taskName,isDone,date) value ('buy milk',0,current date)
- String sql = "INSERT INTO " + this.tableName + " (taskName,isDone,date) VALUES('" + taskName + "' 0,'" + new Date().toString() + "')";
- //we need to compile the SQL string to SQL statment
- SQLiteStatement statement = db.compileStatement(sql);
- //we need the sql command, result of (01) is an error
- Long res = statement.executeInsert();
- //check if we have an error
- return res != (-1);
- }
- //METHOD III - Pure SQL
- public boolean newTaskRawSQL(String taskName) {
- try {
- //create sql String -> INSERT INTO [table name] (taskName,isDone,date) value ('buy milk',0,current date)
- String sql = "INSERT INTO " + this.tableName + " (taskName,isDone,date) VALUES('" + taskName + "' 0,'" + new Date().toString() + "')";
- //execute SQL
- db.execSQL(sql);
- return true;
- } catch (SQLiteException error) {
- Log.e("SQL", "newTaskRawSQL: " + error.getMessage());
- return false;
- }
- }
- //get data from our database by table name
- public Cursor getAllTasks() {
- //get all the tasks into cursor
- Cursor res = db.rawQuery("SELECT * FROM " + this.tableName, null);
- //return the data to the user
- return res;
- }
- //get done/undone tasks
- public Cursor getTaskUnDone(boolean isDone) {
- String sql = "SELECT * FROM " + this.tableName + " WHERE isDone=" + (isDone ? 1 : 0);
- Cursor res = db.rawQuery(sql, null);
- //return the user the information
- return res;
- }
- //get task by name
- //make hani get electrified
- //make%
- //%electrified
- //%get%
- public Cursor getTaskByPartialName(String taskName) {
- String sql = "SELECT * FROM " + this.tableName + " WHERE taskName LIKE %'" + taskName + "'% AND isDone=0";
- Cursor res = db.rawQuery(sql, null);
- return res;
- }
- //delete task by ID
- public void deleteTaskByID(int taskID) {
- db.execSQL("DELETE FROM " + this.tableName + " WHERE id=" + taskID);
- }
- //delte task by task name
- public void deleteTaskByName(String taskName) {
- db.execSQL("DELETE FROM " + this.tableName + " WHERE taskName='" + taskName + "'");
- }
- @Override
- public boolean changeTaskLeStatus(int taskId, boolean isDone) {
- try {
- db.execSQL("UPDATE " + this.tableName + " SET isDone=" + (isDone ? 1 : 0) + " WHERE id=" + taskId);
- return true;
- } catch (SQLiteException error) {
- Log.e("SQLite", "changeTaskLeStatus: "+error.getMessage() );
- }
- return false;
- }
- }
- -----------------------------------------------
- TaskAble
- package com.example.teacher.mytasks;
- import android.database.Cursor;
- /**
- * Created by teacher on 1/4/2018.
- */
- public interface TaskAble {
- boolean newTask(String taskName);
- Cursor getAllTasks();
- Cursor getTaskUnDone(boolean isDone);
- Cursor getTaskByPartialName(String taskName);
- void deleteTaskByID(int taskID);
- void deleteTaskByName(String taskName);
- boolean changeTaskLeStatus(int taskId,boolean isDone);
- }
- ---------------------------------------------------
- TaskActivity
- package com.example.teacher.mytasks;
- import android.content.Context;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.List;
- public class TaskActivity extends AppCompatActivity {
- private String currentUser;
- private Context context;
- TextView taskUser;
- EditText newTask;
- Button addTask;
- ListView taskList;
- List<String> myTasks;
- TaskAdapter myAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_task);
- setPointer();
- }
- private void setPointer() {
- this.context=this;
- //we getting the current intent
- Intent intent=getIntent();
- //get the data from the intent
- this.currentUser=intent.getStringExtra("userName");
- taskUser=findViewById(R.id.txtUserName);
- taskUser.setText(currentUser);
- newTask=findViewById(R.id.etMyTask);
- addTask=findViewById(R.id.btnAddTask);
- addTask.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (newTask.getText().toString().isEmpty()){return;}
- myAdapter.addTask(newTask.getText().toString());
- newTask.setText("");
- }
- });
- taskList=findViewById(R.id.lstTask);
- myAdapter=new TaskAdapter(context);
- taskList.setAdapter(myAdapter);
- }
- }
- TaskAdapter
- ------------------------------------------------
- package com.example.teacher.mytasks;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.database.Cursor;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.CompoundButton;
- import android.widget.ImageView;
- import android.widget.Switch;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.zip.Inflater;
- /**
- * Created by teacher on 12/25/2017.
- */
- public class TaskAdapter extends BaseAdapter {
- Context context;
- List<Tasks> myTasks;
- SQLite sql;
- //Backendless sql
- public TaskAdapter(Context context) {
- this.context=context;
- sql = new SQLite(context);
- //sql = new Backendless(context)
- myTasks = new ArrayList<>();
- getData();
- }
- private void getData() {
- notifyDataSetChanged();
- }
- @Override
- public int getCount() {
- //get data from our SQLite
- Cursor cursor = sql.getAllTasks();
- if (cursor.getCount()>0)
- {
- myTasks = new ArrayList<>();
- while (cursor.moveToNext())
- {
- int taskId = cursor.getInt(0); //0-taskId
- String taskName = cursor.getString(1);
- boolean isDone= cursor.getInt(2)==1; //0-false 1-true
- Date taskDate = new Date();
- myTasks.add(new Tasks(taskId,taskName,isDone,taskDate));
- }
- }
- return myTasks.size();
- }
- @Override
- public Object getItem(int i) {return null;}
- @Override
- public long getItemId(int i) {return 0;}
- @Override
- public View getView(final int i, View view, ViewGroup viewGroup) {
- View taskView = View.inflate(context,R.layout.task_item,null);
- Switch taskDone = taskView.findViewById(R.id.taskDone);
- TextView taskName = taskView.findViewById(R.id.taskName);
- ImageView taskDelete = taskView.findViewById(R.id.taskDelete);
- //set task name
- taskName.setText(myTasks.get(i).getTaskName());
- //set taskDone
- taskDone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
- sql.changeTaskLeStatus(myTasks.get(i).getTaskId(),b);
- }
- });
- taskDone.setChecked(myTasks.get(i).isDone());
- Log.e("isDone", "getView: "+ myTasks.get(i).isDone());
- //delete task
- taskDelete.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // Toast.makeText(context, myTasks.get(i), Toast.LENGTH_SHORT).show();
- AlertDialog.Builder confirmDelete = new AlertDialog.Builder(context);
- confirmDelete.setTitle("Delete a task");
- confirmDelete.setMessage("Are you sure to delete \n"+myTasks.get(i).getTaskName());
- confirmDelete.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int idx) {
- sql.deleteTaskByID(myTasks.get(i).getTaskId());
- myTasks.remove(i);
- dialogInterface.dismiss();
- notifyDataSetChanged();
- }
- });
- confirmDelete.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int idx) {
- dialogInterface.dismiss();
- }
- });
- confirmDelete.show();
- }
- });
- return taskView;
- }
- public boolean addTask(String taskName)
- {
- boolean success = sql.newTask(taskName);
- notifyDataSetChanged();
- return success;
- }
- }
- ----------------------------------------------------
- Tasks
- package com.example.teacher.mytasks;
- import java.util.Date;
- /**
- * Created by teacher on 1/4/2018.
- */
- public class Tasks {
- private int taskId;
- private String taskName;
- private boolean isDone;
- private Date taskDate;
- public Tasks(int taskId, String taskName, boolean isDone, Date taskDate) {
- this.taskId = taskId;
- this.taskName = taskName;
- this.isDone = isDone;
- this.taskDate = taskDate;
- }
- public void setDone(boolean done) {
- isDone = done;
- }
- public String getTaskName() {
- return taskName;
- }
- public boolean isDone() {
- return isDone;
- }
- public int getTaskId() {
- return taskId;
- }
- }
- --------------------------------------------------
- UserAble
- package com.example.teacher.mytasks;
- import android.content.Context;
- /**
- * Created by teacher on 12/21/2017.
- */
- public interface UserAble {
- boolean registerUser(String userName, String userPass);
- boolean userExists(String userName);
- boolean checkUser(String userName,String userPass);
- void setContext(Context context);
- }
- -----------------------------------------------------
- UtlUserSP
- package com.example.teacher.mytasks;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.widget.Toast;
- /**
- * Created by teacher on 12/21/2017.
- */
- public class UtlUserSP implements UserAble {
- Context context;
- public UtlUserSP() { }
- @Override
- public void setContext(Context context) {
- this.context=context;
- }
- @Override
- public boolean registerUser(String userName, String userPass) {
- //User Exists
- if (userExists(userName)) {
- Toast.makeText(context, "user already exists...", Toast.LENGTH_SHORT).show();
- return false;
- }
- //save user and password
- //declaration of shared preference
- SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
- //declaration of shared preference editor
- SharedPreferences.Editor editor = prefs.edit();
- //put data inside, we use hash map, so we need KEY,VALUE (k,v)
- editor.putString(userName, userPass);
- //sending data to my shared preferences file.
- editor.commit();
- return true;
- }
- @Override
- public boolean userExists( String userName) {
- //declaration of shared prefernces
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //get user pass
- String spPass=prefs.getString(userName,"na");
- //return if users exists, if not (na) return false
- return !spPass.equals("na");
- }
- @Override
- public boolean checkUser( String userName, String userPass) {
- //declaration of shared prefernces
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //get user password
- String spPass=prefs.getString(userName,"na");
- //return the result if the passwords are match
- return spPass.equals(userPass);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment