SHARE
TWEET

Main+HomePage+TaskAdapter+utlShared

a guest Mar 20th, 2016 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Main
  2.  
  3. public class MainActivity extends Activity {
  4.  
  5.     private EditText name;
  6.     private EditText password;
  7.     SharedPreferences prefs;
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_main);
  12.         /////////////////////////////////////////////////////////////////////////
  13.         prefs=getApplicationContext().getSharedPreferences("userPrefs",MODE_PRIVATE);
  14.         name =(EditText)findViewById(R.id.userName);
  15.         password =(EditText)findViewById(R.id.pass);
  16.         if(prefs.getString("logd","logd").equals("logd")) //If the user is already logdin
  17.         {
  18.                 Intent HomePage = new Intent(this, HomePage.class);  //Pass diractly to the main menu
  19.                 HomePage.putExtra("Name", prefs.getString("userName", "exist").toString()); //Show "Hello "+ name of the user in the main menu
  20.         }
  21.     }
  22.     ////////////////////////////////////////////////////////////////////////////////////////////////
  23.     public void signUp(View v)
  24.     {
  25.         Intent signup = new Intent(this,Register.class);
  26.         this.startActivity(signup);
  27.     }
  28.     ////////////////////////////////////////////////////////////////////////////////////////////////
  29.     public void logIn(View v)
  30.     {
  31.         if (prefs.contains(name.getText().toString())|| prefs.contains(password.getText().toString()))
  32.         {
  33.                 Intent IhomPage = new Intent(this, HomePage.class);
  34.                 IhomPage.putExtra("Name",name.getText().toString());
  35.                 SharedPreferences.Editor editor=prefs.edit();
  36.                 editor.putString("logd", "logd");
  37.                 startActivity(IhomPage);
  38.         }
  39.         else
  40.         {
  41.                 Toast.makeText(this, "email or password are incorrect", Toast.LENGTH_LONG).show();
  42.         }
  43.     }
  44. }
  45. ////////////////////////////////////////////////////////////////////////////////////////////////////
  46.  
  47. //home page
  48.  
  49. public class HomePage extends AppCompatActivity {
  50.  
  51.     TextView hom ;
  52.  
  53.     LinearLayout myMain;
  54.     LinearLayout top;
  55.     LinearLayout listLayout;
  56.     Context context;
  57.     ListView lv1;
  58.     String userName;
  59.     @Override
  60.     protected void onCreate(Bundle savedInstanceState) {
  61.         super.onCreate(savedInstanceState);
  62.         setContentView(R.layout.activity_home_page);
  63.         hom = (TextView)findViewById(R.id.helow);
  64.         hom.setText("Hello " + getIntent().getStringExtra("Name"));
  65.         userName= getIntent().getStringExtra("Name");
  66.  
  67. //--------------------------------------------------------------------------------------------------
  68.         myMain = (LinearLayout) findViewById(R.id.hom);
  69.         listLayout = (LinearLayout) findViewById(R.id.listLayout);
  70.         top = (LinearLayout) findViewById(R.id.top);
  71.         lv1=(ListView)findViewById(R.id.lv1);
  72.         context = this;
  73.  
  74.         //created new objects
  75.         Button myButton = new Button(this);
  76.         TaskAdapter adapter = new TaskAdapter(this, userName); //******
  77.         //link adapter to ListView
  78.         lv1.setAdapter(adapter);
  79.         //set properties for new Button
  80.         myButton.setText("+ Add new task");
  81.         myButton.setWidth(listLayout.getWidth());
  82.         myButton.setTextSize(30);
  83.         myButton.setBackgroundColor(Color.GRAY);
  84.         myButton.setGravity(Gravity.CENTER_HORIZONTAL);
  85.         myButton.setOnClickListener(new View.OnClickListener() {
  86.             @Override
  87.             public void onClick(View v)
  88.             {
  89.                 Intent IaddTask = new Intent(context,AddTaskActivity.class);
  90.                 IaddTask.putExtra("userName",getIntent().getStringExtra("Name"));
  91.                 startActivity(IaddTask);
  92.             }
  93.         });
  94.  
  95.         //add new elements to our LinearLayout
  96.         myMain.addView(myButton);
  97.     }
  98. //__________________________________________________________________________________________________
  99.     public void logUot(View v)
  100.     {
  101.         SharedPreferences prefs=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  102.         SharedPreferences.Editor editor=prefs.edit();
  103.         editor.remove("logd");
  104.         editor.commit();
  105.         finish();
  106.  
  107.     }
  108. }
  109.  
  110.  
  111.  
  112. //TaskAdapter
  113.  
  114.  
  115. public class TaskAdapter extends BaseAdapter
  116. {
  117.     //SET CONTEXT
  118.     Context context;
  119.     String userName;
  120.     List<utlShared> userTasks;
  121.  
  122.     public TaskAdapter(Context context,String userName)
  123.     {
  124.         this.context=context;
  125.         this.userName=userName;
  126.         //CREATE utlShared instance
  127.         utlShared spTasks = new utlShared(context,userName);
  128.         //get all task into instance
  129.         userTasks = spTasks.getAllTasks();
  130.     }
  131.  
  132.     @Override
  133.     public int getCount() {
  134.         //RETURN SIZE OF ALL TASKS
  135.         return userTasks.size();
  136.     }
  137.  
  138.     @Override
  139.     public Object getItem(int position) {
  140.         return null;
  141.     }
  142.  
  143.     @Override
  144.     public long getItemId(int position) {
  145.         return 0;
  146.     }
  147.  
  148.     @Override
  149.     public View getView(int position, View convertView, ViewGroup parent) {
  150.         //CREATE NEW TEXT VIEW
  151.         TextView taskName = new TextView(context);
  152.         //PUT TASK NAME INTO TEXT VIEW
  153.         taskName.setText(userTasks.get(position).taskName);
  154.         //SET COLOR OF TASK
  155.         taskName.setTextColor(userTasks.get(position).taskDone.equals("1")?Color.GREEN: Color.RED);
  156.         //RETURN TEXTVIEW
  157.         return taskName;
  158.  
  159.     }
  160. }
  161.  
  162. //utlShared
  163.  
  164.   public class utlShared
  165.     {
  166.         private Context context;
  167.         private SharedPreferences userPref;
  168.         private SharedPreferences.Editor editor;
  169.         public String taskName;
  170.         public Boolean taskDone;
  171.         private String userName;
  172.         private List<utlShared> myTasks;
  173.  
  174.         //contructor for adding name to list
  175.         public utlShared(String taskName, Boolean taskDone)
  176.         {
  177.             this.taskName=taskName;
  178.             this.taskDone=taskDone;
  179.         }
  180.  
  181.         //contructor for new utlShared instance
  182.         public utlShared(Context context, String userName)
  183.         {
  184.             //get context
  185.             this.context = context;
  186.             //get username for shared preferances file
  187.             this.userName=userName;
  188.             userPref=context.getSharedPreferences(userName,Context.MODE_PRIVATE);
  189.             editor = userPref.edit();
  190.             //create new list
  191.             List<utlShared> myTasks = new ArrayList<>();
  192.             //get all tasks to list
  193.             getTasks();
  194.         }
  195.  
  196.         public void newTask(String taskName)
  197.         {
  198.             //PUT NEW TASK INTO SHARED PREFERANCES
  199.             editor.putString(taskName,"0");
  200.             editor.commit();
  201.             //PUT TASK INTO LIST
  202.             myTasks.add(new utlShared(taskName, false));
  203.         }
  204.  
  205.         public void removeTask(String taskName)
  206.         {
  207.             //REMOVE TASK FROM SHARED PREFERANCES
  208.             editor.remove(taskName);
  209.             editor.commit();
  210.             //REMOVE TASK FROM LIST
  211.             myTasks.remove(taskName);
  212.         }
  213.  
  214.         public void doneTask(String taskName, boolean taskDone)
  215.         {
  216.             //CHANGE TASK IN SHARED PREFERANCES
  217.             editor.putString(taskName, taskDone ? "1" : "0");
  218.             editor.commit();
  219.             //REMOVE TASK FROM LIST
  220.             myTasks.remove(taskName);
  221.             //ADD TASK WITH NEW TASK DONE INTO LIST
  222.             myTasks.add(new utlShared(taskName, taskDone));
  223.         }
  224.  
  225.         public void getTasks()
  226.         {
  227.             //GET ALL TASK FROM SHARED PREFERANCES
  228.             Map<String,?> tasks= userPref.getAll();
  229.             for(Map.Entry<String,?> singleTask:tasks.entrySet())
  230.             {
  231.                 //INSERT TASK INTO LIST
  232.                 myTasks.add(new utlShared(singleTask.getKey(),singleTask.getValue().equals("1")));
  233.             }
  234.         }
  235.  
  236.         public List<utlShared> getAllTasks()
  237.         {
  238.             //GETTER RETURN TASK LIST
  239.             return myTasks;
  240.         }
  241. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top