Advertisement
Guest User

H/W Task App

a guest
Mar 26th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.53 KB | None | 0 0
  1. ///////////////////////////Main Activity Java/////////////////////////
  2.  
  3. package com.mordekicloud.yuli.myapp1980;
  4.  
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13. /**
  14.  * Created by yuli.mordek on 3/26/2016.
  15.  */
  16. public class MainActivity extends Activity
  17. {
  18.  
  19.     EditText txtUserName;
  20.     EditText txtPassword;
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.  
  27.         txtUserName = (EditText) findViewById(R.id.txtUser1);
  28.         txtPassword = (EditText) findViewById(R.id.txtPass1);
  29.  
  30.         if (CheckUser()) {
  31.             SharedPreferences
  32.                     pref = getApplicationContext().getSharedPreferences("Pref", MODE_PRIVATE);
  33.             Intent loggedIntent = new Intent(this, Daf3.class);
  34.             loggedIntent.putExtra("userPut", pref.getString("Logged In", "Error"));
  35.  
  36.  
  37.             this.startActivity(loggedIntent);
  38.         }
  39.     }
  40.  
  41.  
  42.     private boolean CheckUser ()
  43.     {
  44.         SharedPreferences
  45.                 pref = getApplicationContext().getSharedPreferences("user", MODE_PRIVATE);
  46.         String userName = pref.getString("Logged In", "Error");
  47.         return (!userName.equals("Error"));
  48.     }
  49.  
  50.     public void  btnLogIn(View v) {
  51.         SharedPreferences pref =
  52.                 getApplicationContext().getSharedPreferences("user", MODE_PRIVATE);
  53.         String logUserName = txtUserName.getText().toString().toLowerCase();
  54.         String logPassword = txtPassword.getText().toString();
  55.         String logPasswordConf = pref.getString(logUserName, "Error");
  56.  
  57.         if (logUserName.isEmpty() || logPassword.isEmpty()) {
  58.             Toast.makeText(this, "Ther is one or more fields empty", Toast.LENGTH_LONG).show();
  59.             return;
  60.         }
  61.         else if (!pref.contains(logUserName) || !logPassword.equals(logPasswordConf)) {
  62.             Toast.makeText(this, "Invald user name or password", Toast.LENGTH_LONG).show();
  63.             return;
  64.         }
  65.         else {
  66.             SharedPreferences.Editor editor = pref.edit();
  67.             editor.putString("Logged", logUserName).commit();
  68.  
  69.             Toast.makeText(this, "You logged successfuly", Toast.LENGTH_SHORT).show();
  70.             Intent loggedIntent = new Intent(this, Daf3.class);
  71.             loggedIntent.putExtra("userPut", pref.getString("Logged", "Error"));
  72.             this.startActivity(loggedIntent);
  73.         }
  74.     }
  75.  
  76.  
  77.  
  78.     public void btnSingUp (View v)
  79.     {
  80.         Intent singIntent = new Intent(this, Daf2.class);
  81.         this.startActivity(singIntent);
  82.     }
  83.  
  84.  
  85. }
  86. //////////////////////////////////////////Java Daf2//////////////////////////////
  87. package com.mordekicloud.yuli.myapp1980;
  88.  
  89. import android.app.Activity;
  90. import android.content.Intent;
  91. import android.content.SharedPreferences;
  92. import android.os.Bundle;
  93. import android.view.View;
  94. import android.widget.EditText;
  95. import android.widget.Toast;
  96.  
  97. /**
  98.  * Created by yuli.mordek on 3/26/2016.
  99.  */
  100. public class Daf2 extends Activity
  101. {
  102.  
  103.     EditText txtUserName;
  104.     EditText txtPassword;
  105.     EditText txtComfPass;
  106.  
  107.     @Override
  108.     protected void onCreate(Bundle savedInstanceState) {
  109.         super.onCreate(savedInstanceState);
  110.         setContentView(R.layout.daf2);
  111.         txtUserName=(EditText)findViewById(R.id.UserName);
  112.         txtPassword=(EditText)findViewById(R.id.Password);
  113.         txtComfPass=(EditText)findViewById(R.id.ConfPass);
  114.  
  115.     }
  116.  
  117.     public void btnCancel (View v)
  118.     {
  119.         Intent cancelIntent=new Intent(this, MainActivity.class);
  120.         this.startActivity(cancelIntent);
  121.     }
  122.  
  123.     public void btnEnter(View v)
  124.     {
  125.         SharedPreferences
  126.                 pref=getApplicationContext().getSharedPreferences("user",MODE_PRIVATE);
  127.         SharedPreferences.Editor editor=pref.edit();
  128.  
  129.         String  UserName=txtUserName.getText().toString().toLowerCase();
  130.         String  Password=txtPassword.getText().toString().toLowerCase();
  131.         String  ConfPass=txtComfPass.getText().toString().toLowerCase();
  132.  
  133.         if (UserName.isEmpty() || Password.isEmpty() || ConfPass.isEmpty())
  134.         {
  135.             Toast.makeText(this, "You must fill all fields", Toast.LENGTH_LONG).show();
  136.             return;
  137.         }
  138.         else if(pref.contains(UserName))
  139.         {
  140.             Toast.makeText(this,"User you exists",Toast.LENGTH_SHORT).show();
  141.             return;
  142.         }
  143.  
  144.         else if (!Password.equals(ConfPass))
  145.         {
  146.             Toast.makeText(this, "Password is not correct", Toast.LENGTH_LONG).show();
  147.             return;
  148.         }
  149.         Toast.makeText(this, "You registreted successful", Toast.LENGTH_SHORT).show();
  150.         editor.putString(UserName, Password).commit();
  151.         Intent singIntent = new Intent(this,MainActivity.class);
  152.         this.startActivity(singIntent);
  153.  
  154.     }
  155. }
  156. ///////////////////////////////////////Java Daf3////////////////////////////////////
  157. package com.mordekicloud.yuli.myapp1980;
  158.  
  159. import android.app.Activity;
  160. import android.content.Context;
  161. import android.content.Intent;
  162. import android.content.SharedPreferences;
  163. import android.os.Bundle;
  164. import android.view.View;
  165. import android.widget.ListView;
  166. import android.widget.TextView;
  167.  
  168. import java.util.ArrayList;
  169. import java.util.Map;
  170.  
  171. /**
  172.  * Created by yuli.mordek on 3/26/2016.
  173.  */
  174. public class Daf3 extends Activity
  175. {
  176.  
  177.     private TextView txtUser1;
  178.     private ListView myTaskList;
  179.     private Context context=this;
  180.     private ArrayList<String> myTaskArrayList=new ArrayList<String>();
  181.     private NewAdapter adapter;
  182.  
  183.  
  184.     @Override
  185.     protected void onCreate(Bundle savedInstanceState) {
  186.         super.onCreate(savedInstanceState);
  187.         setContentView(R.layout.daf3);
  188.         txtUser1=(TextView)findViewById(R.id.Msg);
  189.         txtUser1.setText("Helllooo... !!  "+getIntent().getStringExtra("userPut"));
  190.         myTaskList=(ListView)findViewById(R.id.myListTv);
  191.         String loggedUser=getIntent().getStringExtra("userPut");
  192.  
  193.     }
  194.  
  195.     public void  onResume()
  196.     {
  197.         super.onResume();
  198.         String loggedUser=getIntent().getStringExtra("userPut");
  199.         SharedPreferences
  200.                 pref=getApplicationContext().getSharedPreferences(loggedUser, MODE_PRIVATE);
  201.         Map<String,?> tasks=pref.getAll();
  202.         for(Map.Entry<String,?> singleTask:tasks.entrySet())
  203.         {
  204.             myTaskArrayList.add(singleTask.getKey());
  205.         }
  206.         NewAdapter adapter =new NewAdapter(context, loggedUser);
  207.  
  208.         myTaskList.setAdapter(adapter);
  209.     }
  210.     public void btnOut(View v)
  211.     {
  212.         SharedPreferences
  213.                 pref=getApplicationContext().getSharedPreferences("user",MODE_PRIVATE);
  214.         SharedPreferences.Editor editor=pref.edit();
  215.         editor.remove("Logged").commit();
  216.         Intent outIntent=new Intent(this,MainActivity.class);
  217.         this.startActivity(outIntent);
  218.         finish();
  219.  
  220.     }
  221.  
  222.     public void btnTask(View v)
  223.     {
  224.         Intent i=new Intent(this,Daf4.class);
  225.         //addTaskIntent.putExtra("putUserTask",getIntent().getStringExtra("userPut"));
  226.         startActivity(i);
  227.         finish();
  228.     }
  229.  
  230. }
  231. ////////////////////////////////Daf4 Java/////////////////////////////////////
  232. package com.mordekicloud.yuli.myapp1980;
  233.  
  234. import android.app.AlertDialog;
  235. import android.content.Context;
  236. import android.content.DialogInterface;
  237. import android.content.Intent;
  238. import android.content.SharedPreferences;
  239. import android.support.v7.app.AppCompatActivity;
  240. import android.os.Bundle;
  241. import android.text.InputType;
  242. import android.view.Menu;
  243. import android.view.MenuInflater;
  244. import android.view.MenuItem;
  245. import android.widget.EditText;
  246. import android.widget.ListView;
  247. import android.widget.Switch;
  248. import android.widget.TextView;
  249. import android.widget.Toast;
  250.  
  251. import java.util.zip.Inflater;
  252.  
  253. public class Daf4 extends AppCompatActivity {
  254.  
  255.     private String myText="";
  256.     private TextView txt;
  257.     protected static ListView myList;
  258.     private Context context=this;
  259.     private String myUser;
  260.     private NewAdapter myTask;
  261.  
  262.  
  263.     @Override
  264.     protected void onCreate(Bundle savedInstanceState) {
  265.         super.onCreate(savedInstanceState);
  266.         setContentView(R.layout.daf4);
  267.         txt=(TextView)findViewById(R.id.text);
  268.         myList=(ListView)findViewById(R.id.myListTv);
  269.         myUser=getIntent().getStringExtra("putUser");
  270.         txt.setText("Hello "+myUser);
  271.  
  272.         refresh();
  273.  
  274.     }
  275.  
  276.  
  277.  
  278.     @Override
  279.     public boolean onCreateOptionsMenu(Menu menu)
  280.     {
  281.         MenuInflater inflater=getMenuInflater();
  282.         inflater.inflate(R.menu.menu, menu);
  283.         return super.onCreateOptionsMenu(menu);
  284.  
  285.     }
  286.  
  287.     @Override
  288.     public boolean onOptionsItemSelected(MenuItem item) {
  289.         switch (item.getItemId()) {
  290.             case R.id.AddTask:
  291.                 btnAddtask();
  292.                 dlgAddTask();
  293.                 Toast.makeText(this, myText, Toast.LENGTH_SHORT).show();
  294.  
  295.                 break;
  296.             case R.id.Logout:
  297.                 btnLogout();
  298.                 Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
  299.                 break;
  300.         }
  301.         return super.onOptionsItemSelected(item);
  302.     }
  303.  
  304.  
  305.  
  306.     public void refresh()
  307.     {
  308.         myTask=new NewAdapter(context,myUser);
  309.         myList.setAdapter(myTask);
  310.     }
  311.  
  312.     public void btnAddtask()
  313.     {
  314.         AlertDialog.Builder builder=new  AlertDialog.Builder(this);
  315.         builder.setTitle("Enter new task");
  316.         final EditText input=new EditText(this);
  317.         input.setInputType(InputType.TYPE_CLASS_TEXT);
  318.         builder.setView(input);
  319.         builder.setPositiveButton("Add Task", new DialogInterface.OnClickListener() {
  320.             public void onClick(DialogInterface dialog, int which) {
  321.                 myText = input.getText().toString();
  322.  
  323.                 UtlShared myPref = new UtlShared(context, myUser);
  324.                 myPref.newTask(myText);
  325.                 refresh();
  326.             }
  327.         });
  328.         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  329.             @Override
  330.             public void onClick(DialogInterface dialog, int which) {
  331.                 dialog.cancel();
  332.             }
  333.         });
  334.  
  335.       builder.show();
  336.     }
  337.  
  338.     public void btnLogout()
  339.     {
  340.         SharedPreferences
  341.                 pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  342.         SharedPreferences.Editor editor=pref.edit();
  343.     editor.remove("userLogged").commit();
  344.         Intent iMain=new Intent(this,MainActivity.class);
  345.         this.startActivity(iMain);
  346.         finish();
  347.     }
  348.  
  349.  
  350.  
  351.     private void dlgAddTask()
  352.     {
  353.         AlertDialog.Builder builder=new AlertDialog.Builder(this);
  354.         builder.setTitle("Input new task");
  355.  
  356.         final EditText input = new EditText(this);
  357.         input.setInputType(InputType.TYPE_CLASS_TEXT);
  358.  
  359.         builder.setPositiveButton("Add task", new DialogInterface.OnClickListener() {
  360.             public void onClick(DialogInterface dialog, int which) {
  361.                 myText = input.getText().toString();
  362.                 Toast.makeText(Daf4.this, "Hello  " + myText + "The Cizar!! ", Toast.LENGTH_SHORT).show();
  363.             }
  364.         });
  365.  
  366.         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  367.             @Override
  368.             public void onClick(DialogInterface dialog, int which) {
  369.                 dialog.cancel();
  370.             }
  371.         });
  372.         builder.show();
  373.     }
  374.  
  375.  
  376. }
  377. ///////////////////////NewAdapter//////////////////////
  378. package com.mordekicloud.yuli.myapp1980;
  379.  
  380. import android.app.AlertDialog;
  381. import android.content.Context;
  382. import android.content.DialogInterface;
  383. import android.graphics.Color;
  384. import android.media.Image;
  385. import android.view.View;
  386. import android.view.ViewGroup;
  387. import android.widget.BaseAdapter;
  388. import android.widget.ImageButton;
  389. import android.widget.LinearLayout;
  390. import android.widget.TextView;
  391. import android.widget.Toast;
  392.  
  393. import java.util.ArrayList;
  394.  
  395. /**
  396.  * Created by yuli.mordek on 3/26/2016.
  397.  */
  398. public class NewAdapter extends BaseAdapter
  399. {
  400.     Context context;
  401.     String userName;
  402.     ArrayList<UtlShared>userTasks;
  403.  
  404.    public NewAdapter(Context context, String userName)
  405.    {
  406.        this.context=context;
  407.        this.userName=userName;
  408.        UtlShared myPref=new UtlShared(context,userName);
  409.        userTasks=myPref.getAllTasks();
  410.    }
  411.  
  412.     @Override
  413.     public int getCount()
  414.     {
  415.         return userTasks.size();
  416.     }
  417.  
  418.     @Override
  419.     public Object getItem(int position)
  420.     {
  421.         return null;
  422.     }
  423.  
  424.     @Override
  425.     public long getItemId(int position)
  426.     {
  427.         return 0;
  428.     }
  429.  
  430.     @Override
  431.     public View getView(final int position, View convertView, ViewGroup parent)
  432.     {
  433.         LinearLayout myLayout=new LinearLayout(context);
  434.         myLayout.setOrientation(LinearLayout.HORIZONTAL);
  435.  
  436.         ImageButton delete=new ImageButton(context);
  437.       //  delete.setBackgroundResource(R.drawable.trash);
  438.         final TextView taskName = new TextView(context);
  439.         taskName.setText(userTasks.get(position).taskName);
  440.         taskName.setTextSize(30);
  441.         //myLayout.setTextColor(userTask.get(position).taskDone.equals(true)?
  442.              //   Color.GREEN:Color.RED);
  443.  
  444.         myLayout.setOnClickListener(new View.OnClickListener() {
  445.             @Override
  446.             public void onClick(View v) {
  447.                 AlertDialog.Builder builder=new AlertDialog.Builder(context);
  448.                 builder.setTitle(userTasks.get(position).taskDone.equals(true) ?
  449.                         "Are you sure the task isn't completed ?" : "Are you sure the task is completed ?");
  450.  
  451.                 builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  452.                     @Override
  453.                     public void onClick(DialogInterface dialog, int which) {
  454.                         UtlShared myPref = new UtlShared(context, userName);
  455.                         myPref.doneTask(userTasks.get(position).taskName, userTasks.get(position).taskDone.equals(true) ? false : true);
  456.                     }
  457.                 });
  458.                // builder.setIcon(android.R.drawable.ic_menu_help);
  459.                 builder.show();
  460.  
  461.             }
  462.  
  463.  
  464.         });
  465.  
  466.         delete.setOnClickListener(new View.OnClickListener() {
  467.             @Override
  468.             public void onClick(View v) {
  469.                 AlertDialog.Builder builder=new AlertDialog.Builder(context);
  470.                 builder.setTitle("Are you sure to delete task?");
  471.                 builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  472.                     @Override
  473.                     public void onClick(DialogInterface dialog, int which) {
  474.                         UtlShared myPref = new UtlShared(context, userName);
  475.                         myPref.removeTask(userTasks.get(position).taskName);
  476.                     }
  477.                 });
  478.                // builder.setIcon(android.R.drawable.ic_delete);
  479.                 builder.show();
  480.  
  481.             }
  482.         });
  483.  
  484.         myLayout.addView(delete);
  485.         myLayout.addView(taskName);
  486.         return myLayout;
  487.     }
  488. }
  489. /////////////////Utlshared///////////////////////////
  490. package com.mordekicloud.yuli.myapp1980;
  491.  
  492. import android.content.Context;
  493. import android.content.Entity;
  494. import android.content.SharedPreferences;
  495. import android.os.DropBoxManager;
  496.  
  497. import java.security.KeyStore;
  498. import java.util.ArrayList;
  499. import java.util.Map;
  500.  
  501. /**
  502.  * Created by yuli.mordek on 3/26/2016.
  503.  */
  504. public class UtlShared
  505. {
  506.     private Context context;
  507.     private SharedPreferences userPref;
  508.     private SharedPreferences.Editor editor;
  509.     public  String taskName;
  510.     public Boolean taskDone;
  511.     private String userName;
  512.     private NewAdapter myTask;
  513.  
  514.     private ArrayList<UtlShared>taskList;
  515.  
  516.     public UtlShared(String taskName, Boolean taskDone)
  517.     {
  518.         this.taskName=taskName;
  519.         this.taskDone=taskDone;
  520.     }
  521.  
  522.     public UtlShared(Context context, String userName)
  523.     {
  524.         this.context=context;
  525.         this.userName=userName;
  526.         userPref=context.getSharedPreferences(userName, Context.MODE_PRIVATE);
  527.         editor=userPref.edit();
  528.         taskList=new ArrayList<UtlShared>();
  529.         getTasks();
  530.     }
  531.  
  532.  
  533.  
  534.  
  535.     public void newTask(String taskName)
  536.     {
  537.      editor.putString(taskName,"0");
  538.      editor.commit();
  539.      taskList.add(new UtlShared(taskName, false));
  540.     }
  541.  
  542.     public void removeTask(String taskName)
  543.     {
  544.         editor.remove(taskName);
  545.         editor.commit();
  546.         taskList.remove(taskName);
  547.         refresh();
  548.     }
  549.  
  550.     public void doneTask(String taskName, boolean taskDone)
  551.     {
  552.         editor.putString(taskName,taskDone?"1":"0");
  553.         editor.commit();
  554.         taskList.remove(taskName);
  555.         taskList.add(new UtlShared(taskName, taskDone));
  556.         refresh();
  557.  
  558.     }
  559.  
  560.     public void getTasks()
  561.     {
  562.         Map<String,?>tasks=userPref.getAll();
  563.         for (Map.Entry<String,?> singleTask:tasks.entrySet())
  564.         {
  565.  
  566.  
  567.  
  568.             taskList.add(new UtlShared(singleTask.getKey(),singleTask.getValue().equals("1")));
  569.         }
  570.     }
  571.     public ArrayList<UtlShared>getAllTasks()
  572.     {
  573.         return taskList;
  574.     }
  575.  
  576.     public void refresh()
  577.     {
  578.         myTask=new NewAdapter(context,userName);
  579.        //Entry.myList.setAdapter(myTask);
  580.     }
  581.  
  582.  
  583. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement