Advertisement
Guest User

tasker

a guest
Mar 27th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. package com.example.app0811.menu;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.os.Bundle;
  10. import android.text.InputType;
  11. import android.view.Menu;
  12. import android.view.MenuInflater;
  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. Context context;
  20. EditText userName;
  21. EditText userPass;
  22. SharedPreferences myPref ;
  23. SharedPreferences.Editor editor;
  24.  
  25.  
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. userName=(EditText)findViewById(R.id.username);
  30. userPass=(EditText)findViewById(R.id.password);
  31. context=this;
  32.  
  33. myPref = getSharedPreferences("userPref", MODE_PRIVATE);
  34. editor = myPref.edit();
  35.  
  36.  
  37. final String getUserName = checkIfLogged();
  38. if (!getUserName.equals("na"))
  39. {
  40. startTaskActivity(getUserName);
  41. }
  42. }
  43.  
  44. public void btnLogin(View v)
  45. {
  46. if (checkUser())
  47. {
  48. updateLogged();
  49. userName.requestFocus();
  50. startTaskActivity(userName.getText().toString());
  51. userName.setText("");
  52. userPass.setText("");
  53. userName.requestFocus();
  54.  
  55. }
  56. else
  57. {
  58. Toast.makeText(this, "Error in user name or password", Toast.LENGTH_SHORT).show();
  59. }
  60. }
  61.  
  62. public void btnRegister(View v)
  63. {
  64. Intent registerIntent = new Intent(this,Register.class);
  65. startActivity(registerIntent);
  66. }
  67.  
  68.  
  69. private String checkIfLogged()
  70. {
  71.  
  72. return myPref.getString("logged","na");
  73. }
  74.  
  75. private void updateLogged()
  76. {
  77. //move user to loged mode
  78. editor.putString("logged", userName.getText().toString());
  79. editor.commit();
  80. }
  81.  
  82. private void startTaskActivity(String loggedUserName)
  83. {
  84. //move to task activity
  85. Intent taskIntent = new Intent(this,TaskList.class);
  86. taskIntent.putExtra("logged",loggedUserName);
  87. startActivity(taskIntent);
  88.  
  89. }
  90.  
  91. private boolean checkUser()
  92. {
  93. if (userName.getText().toString().isEmpty() || userPass.getText().toString().isEmpty()) return false;
  94. return myPref.getString(userName.getText().toString(),"na").equals(userPass.getText().toString());
  95. }
  96. }
  97. =============================================================================================================================
  98. register:
  99.  
  100. package com.example.app0811.menu;
  101.  
  102. import android.app.Activity;
  103. import android.content.SharedPreferences;
  104. import android.os.Bundle;
  105. import android.view.Menu;
  106. import android.view.MenuItem;
  107. import android.view.View;
  108. import android.widget.EditText;
  109. import android.widget.Toast;
  110.  
  111. public class Register extends Activity {
  112. EditText userName;
  113. EditText userPass;
  114. EditText userPassCheck;
  115. @Override
  116. protected void onCreate(Bundle savedInstanceState) {
  117. super.onCreate(savedInstanceState);
  118. setContentView(R.layout.activity_register);
  119. userName=(EditText)findViewById(R.id.name);
  120. userPass=(EditText)findViewById(R.id.pass);
  121. userPassCheck=(EditText)findViewById(R.id.pass1);
  122. }
  123.  
  124. public void btnCancel(View v)
  125. {
  126. finish();
  127. }
  128.  
  129. public void btnRegister(View v)
  130. {
  131. if (checkUser())
  132. {
  133. finish();
  134. }
  135. }
  136.  
  137. private boolean checkUser()
  138. {
  139. if (userName.getText().toString().isEmpty() || userPass.getText().toString().isEmpty())
  140. {
  141. Toast.makeText(Register.this, "Fill all fields", Toast.LENGTH_SHORT).show();
  142. return false;
  143. }
  144.  
  145. if (!userPass.getText().toString().equals(userPassCheck.getText().toString()))
  146. {
  147. Toast.makeText(Register.this, "Password not match", Toast.LENGTH_SHORT).show();
  148. return false;
  149. }
  150.  
  151. SharedPreferences myPref = getSharedPreferences("userPref",MODE_PRIVATE);
  152. if (myPref.contains(userName.getText().toString()))
  153. {
  154. Toast.makeText(Register.this, "User already exists", Toast.LENGTH_SHORT).show();
  155. return false;
  156. }
  157.  
  158. SharedPreferences.Editor editor = myPref.edit();
  159. editor.putString(userName.getText().toString(),userPass.getText().toString());
  160. editor.commit();
  161. return true;
  162. }
  163.  
  164. }
  165. ==============================================================================================================================
  166. TaskAdapter:
  167.  
  168. package com.example.app0811.menu;
  169.  
  170. import android.content.Context;
  171. import android.view.View;
  172. import android.view.ViewGroup;
  173. import android.widget.BaseAdapter;
  174.  
  175. import java.util.List;
  176.  
  177. /**
  178. * Created by App0811 on 3/27/2016.
  179. */
  180. public class TaskAdapter extends BaseAdapter {
  181. Context context;
  182. String userName;
  183. List<utlShared> taskList;
  184.  
  185. public TaskAdapter(Context context,List<utlShared> myTaskList)
  186. {
  187. this.context=context;
  188. taskList= myTaskList;
  189. }
  190.  
  191. public int getCount() {
  192. return 0;
  193. }
  194.  
  195. @Override
  196. public Object getItem(int position) {
  197. return null;
  198. }
  199.  
  200. @Override
  201. public long getItemId(int position) {
  202. return 0;
  203. }
  204.  
  205. @Override
  206. public View getView(int position, View convertView, ViewGroup parent) {
  207. return null;
  208. }
  209. }
  210. ====================================================================================================================================
  211. utlShared:
  212.  
  213. package com.example.app0811.menu;
  214.  
  215. import android.content.Context;
  216. import android.content.SharedPreferences;
  217.  
  218. import java.util.ArrayList;
  219. import java.util.Map;
  220.  
  221. /**
  222. * Created by App0811 on 3/27/2016.
  223. */
  224. public class utlShared {
  225. private Context context;
  226. private SharedPreferences userPref;
  227. private SharedPreferences.Editor editor;
  228. public String taskName;
  229. public Boolean taskDone;
  230. private String userName;
  231. private ArrayList<utlShared> myTasks;
  232.  
  233. public utlShared(String taskName, Boolean taskDone)
  234. {
  235. this.taskName=taskName;
  236. this.taskDone=taskDone;
  237. }
  238.  
  239. public utlShared(Context context, String userName)
  240. {
  241. //get context
  242. this.context = context;
  243. //get username for shared preferances file
  244. this.userName=userName;
  245. userPref=context.getSharedPreferences(userName, Context.MODE_PRIVATE);
  246. editor = userPref.edit();
  247. //create new list
  248. myTasks = new ArrayList<utlShared>();
  249. //get all tasks to list
  250. getTasks();
  251. }
  252.  
  253. public void newTask(String taskName)
  254. {
  255. //PUT NEW TASK INTO SHARED PREFERANCES
  256. editor.putString(taskName,"0");
  257. editor.commit();
  258. //PUT TASK INTO LIST
  259. myTasks.add(new utlShared(taskName, false));
  260. }
  261.  
  262. public void removeTask(String taskName)
  263. {
  264. //REMOVE TASK FROM SHARED PREFERANCES
  265. editor.remove(taskName);
  266. editor.commit();
  267. //REMOVE TASK FROM LIST
  268. myTasks.remove(taskName);
  269. }
  270. public void doneTask(String taskName, boolean taskDone)
  271. {
  272.  
  273. //CHANGE TASK IN SHARED PREFERANCES
  274. editor.putBoolean(taskName, taskDone);
  275. //REMOVE TASK FROM LIST
  276. myTasks.remove(taskName);
  277. //ADD TASK WITH NEW TASK DONE INTO LIST
  278. myTasks.add(new utlShared(taskName, taskDone));
  279. }
  280. public void getTasks()
  281. {
  282. //GET ALL TASK FROM SHARED PREFERANCES
  283. Map<String,?> userTasks= userPref.getAll();
  284. for(Map.Entry<String,?> singleTask:userTasks.entrySet())
  285. {
  286. if (!singleTask.getKey().isEmpty())
  287. //using parse to store boolean
  288. //Boolean.parseBoolean((String) singleTask.getValue())
  289. myTasks.add(new utlShared(singleTask.getKey(),Boolean.parseBoolean((String) singleTask.getValue())));
  290. }
  291. }
  292.  
  293. public ArrayList<utlShared> getAllTasks()
  294. {
  295. return myTasks;
  296. }
  297. }
  298. ===============================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement