Advertisement
Guest User

myTasks

a guest
Mar 29th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.82 KB | None | 0 0
  1. ///////////////////////////////
  2.         MainActivity class
  3. //////////////////////////////
  4.  
  5. package com.example.raz.mytasks;
  6.  
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17.  
  18. public class MainActivity extends Activity {
  19.  
  20.     EditText txtUser;
  21.     EditText txtPass;
  22.     String userNameLogged;
  23.  
  24.     @Override
  25.     protected void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.activity_main);
  28.         txtUser = (EditText)findViewById(R.id.username);
  29.         txtPass = (EditText)findViewById(R.id.pass);
  30.     }
  31.  
  32.  
  33.     public void onClick (View v)
  34.     {
  35.         if (chkUser())
  36.         {
  37.             Intent helloIntent = new Intent(this , Hello.class);
  38.             helloIntent.putExtra("extraUserName",txtUser.getText().toString());
  39.             this.startActivity(helloIntent);
  40.         }
  41.     }
  42.  
  43.     private boolean chkUser()
  44.     {
  45.         SharedPreferences myPref = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  46.         String userPass = myPref.getString(txtUser.getText().toString(), "error");
  47.         if (userPass.equals("error") || !userPass.equals(txtPass.getText().toString()))
  48.         {
  49.             Toast.makeText(this, "User or password error", Toast.LENGTH_LONG).show();
  50.             return false;
  51.         }
  52.         return true;
  53.     }
  54.  
  55.     public void btnReg (View v)
  56.     {
  57.         Intent intentReg = new Intent(this, Register.class);
  58.         this.startActivity(intentReg);
  59.     }
  60.  
  61. }
  62.  
  63.  
  64.  
  65. /////////////////////////////
  66.             Hello class
  67. ////////////////////////////
  68.  
  69. package com.example.raz.mytasks;
  70.  
  71. import android.app.Activity;
  72. import android.app.AlertDialog;
  73. import android.content.Context;
  74. import android.content.DialogInterface;
  75. import android.content.SharedPreferences;
  76. import android.os.Bundle;
  77. import android.text.InputType;
  78. import android.view.View;
  79. import android.widget.EditText;
  80. import android.widget.ListView;
  81. import android.widget.TextView;
  82. import android.widget.Toast;
  83.  
  84. import java.util.ArrayList;
  85. import java.util.List;
  86.  
  87. /**
  88.  * Created by raz on 27/03/2016.
  89.  */
  90. public class Hello extends Activity {
  91.     SharedPreferences myPrefs; //calling SharedPrefrences Class
  92.     SharedPreferences.Editor editor;//calling the editor for SharedPrefrences
  93.     private String myText = "";
  94.     Context context;
  95.     TextView myTxt;
  96.     List<utlShared> myList;
  97.     utlShared myUtl;
  98.     ListView myListView;
  99.  
  100.     protected void onCreate(Bundle savedInstanceState)
  101.     {
  102.         super.onCreate(savedInstanceState);
  103.         setContentView(R.layout.hello);
  104.         // setPoints();
  105.         myTxt = (TextView) findViewById(R.id.hellotxt);
  106.         myTxt.setText("Hello " + getIntent().getStringExtra("extraUserName"));
  107.         myList = new ArrayList<>();
  108.         myUtl = new utlShared(this,getIntent().getStringExtra("extraUserName"));
  109.         myList = myUtl.getAllTasks();
  110.         myListView=(ListView)findViewById(R.id.listView);
  111.         if (myList!=null)
  112.         {
  113.             taskAdapter adapter = new taskAdapter(this, (ArrayList<utlShared>) myList);
  114.             myListView.setAdapter(adapter);
  115.         }
  116.     }
  117.  
  118.  
  119.  
  120.     /*private void setPoints()
  121.     {
  122.         myTxt = (TextView)findViewById(R.id.hellotxt);
  123.         myPrefs = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  124.         editor = myPrefs.edit();
  125.     }
  126.  
  127.     /*private String getUser()
  128.     {
  129.         String user = myPrefs.getString("userName","error");
  130.         return user;
  131.     }
  132. */
  133.  
  134.     public void btnAddTask(View v) {
  135.        /* Intent AddTask = new Intent(this, AddTasks.class);
  136.         AddTask.putExtra("userPutTask" , getIntent().getStringExtra("userName"));*/
  137.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  138.         builder.setTitle("Input New Task");
  139.  
  140.         final EditText input = new EditText(this);
  141.         input.setInputType(InputType.TYPE_CLASS_TEXT);
  142.         builder.setView(input);
  143.  
  144.         builder.setPositiveButton("Add Task", new DialogInterface.OnClickListener() {
  145.             @Override
  146.             public void onClick(DialogInterface dialog, int which) {
  147.                 myText = input.getText().toString();
  148.                 myUtl.newTask(myText);
  149.                 myList.add(new utlShared(myText, false));
  150.                 taskAdapter adapter = new taskAdapter(Hello.this, (ArrayList<utlShared>) myList);
  151.                 myListView.setAdapter(adapter);
  152.  
  153.             }
  154.         });
  155.         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  156.             @Override
  157.             public void onClick(DialogInterface dialog, int which) {
  158.                 dialog.cancel();
  159.             }
  160.         });
  161.         builder.show();
  162.     }
  163.  
  164.     public void btnExit(View v) {
  165.         SharedPreferences myPref = getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
  166.         SharedPreferences.Editor editor = myPref.edit();
  167.         editor.remove("extraUserName");
  168.         editor.commit();
  169.         myTxt.setText("");
  170.        /* Intent exit = new Intent(this,MainActivity.class);
  171.         this.startActivity(exit);*/
  172.         finish();
  173.     }
  174. }
  175.  
  176.  
  177. //////////////////////////////////////
  178.         utlShared class
  179. //////////////////////////////////////
  180.  
  181. package com.example.raz.mytasks;
  182.  
  183. import android.content.Context;
  184. import android.content.SharedPreferences;
  185.  
  186. import java.util.ArrayList;
  187. import java.util.List;
  188. import java.util.Map;
  189.  
  190. /**
  191.  * Created by raz on 27/03/2016.
  192.  */
  193. public class utlShared
  194. {
  195.     private Context context;
  196.     private SharedPreferences userPref;
  197.     private SharedPreferences.Editor editor;
  198.     public String taskName;
  199.     public Boolean taskDone;
  200.     public String userName;
  201.     private ArrayList<utlShared> myTasks;
  202.  
  203.     public utlShared (String taskName , Boolean taskDone)
  204.     {
  205.         this.taskName=taskName;
  206.         this.taskDone=taskDone;
  207.     }
  208.  
  209.     public utlShared (Context context, String userName)
  210.     {
  211.         this.context=context;
  212.         this.userName=userName;
  213.         userPref=context.getSharedPreferences(userName, Context.MODE_PRIVATE);
  214.         editor = userPref.edit();
  215.  
  216.         ArrayList<utlShared> myTasks = new ArrayList<utlShared>();
  217.         getTasks();
  218.     }
  219.  
  220.     public void newTask (String taskName)
  221.     {
  222.         editor.putString(taskName , "0");
  223.         editor.commit();
  224.         myTasks.add(new utlShared(taskName, false));
  225.     }
  226.  
  227.     public void removeTask (String taskName)
  228.     {
  229.         editor.remove(taskName);
  230.         editor.commit();
  231.         myTasks.remove(taskName);
  232.     }
  233.  
  234.     public void doneTask (String taskName , boolean taskDone)
  235.     {
  236.         editor.putString(taskName , taskDone ? "1" : "0");
  237.         editor.commit();
  238.         myTasks.remove(taskName);
  239.         myTasks.add(new utlShared(taskName, taskDone));
  240.     }
  241.  
  242.     public void getTasks ()
  243.     {
  244.         Map<String,?> tasks = userPref.getAll();
  245.         for (Map.Entry<String,?> singleTask:tasks.entrySet())
  246.         {
  247.             myTasks.add(new utlShared(singleTask.getKey() , true));
  248.         }
  249.     }
  250.  
  251.     public ArrayList<utlShared> getAllTasks()
  252.     {
  253.         return myTasks;
  254.     }
  255. }
  256.  
  257.  
  258. ////////////////////////////////////////////////
  259.             taskAdapter class
  260. ////////////////////////////////////////////////
  261.  
  262. package com.example.raz.mytasks;
  263.  
  264. import android.content.Context;
  265. import android.graphics.Color;
  266. import android.view.View;
  267. import android.view.ViewGroup;
  268. import android.widget.BaseAdapter;
  269. import android.widget.EditText;
  270.  
  271. import java.util.ArrayList;
  272. import java.util.List;
  273.  
  274. /**
  275.  * Created by raz on 27/03/2016.
  276.  */
  277. public class taskAdapter extends BaseAdapter
  278. {
  279.     Context context;
  280.     List<utlShared> myList;
  281.  
  282.     public taskAdapter(Context context, ArrayList<utlShared> myList)
  283.     {
  284.         this.context=context;
  285.         this.myList=myList;
  286.     }
  287.     @Override
  288.     public int getCount() {
  289.         return myList.size();
  290.     }
  291.  
  292.     @Override
  293.     public Object getItem(int position) {
  294.         return null;
  295.     }
  296.  
  297.     @Override
  298.     public long getItemId(int position) {
  299.         return 0;
  300.     }
  301.  
  302.     @Override
  303.     public View getView(int position, View convertView, ViewGroup parent) {
  304.         final EditText txtTask = new EditText(context);
  305.         txtTask.setText(myList.get(position).taskName);
  306.         txtTask.setTextColor(myList.get(position).taskDone ? Color.GREEN : Color.RED);
  307.         txtTask.setOnClickListener(new View.OnClickListener() {
  308.             @Override
  309.             public void onClick(View v) {
  310.                 txtTask.setTextColor(Color.GREEN);
  311.             }
  312.         });
  313.         return txtTask;
  314.     }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement