Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity
- ----------------
- package com.example.asusproi5win10.myapplication;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- EditText etUserName, etPassword;
- String c;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- connectToLayout();
- }
- private void connectToLayout() {
- etUserName = (EditText) findViewById(R.id.etUserName);
- etPassword = (EditText) findViewById(R.id.etPassword);
- findViewById(R.id.btnCancel).setOnClickListener(this);
- findViewById(R.id.btnLogin).setOnClickListener(this);
- findViewById(R.id.btnRegister).setOnClickListener(this);
- }
- @Override
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.btnLogin:
- String userName = etUserName.getText().toString();
- String password = etPassword.getText().toString();
- if (userName.length() == 0 || password.length() == 0) {
- Toast.makeText(this, "Please fill user name and password!", Toast.LENGTH_LONG).show();
- return;
- }
- if (isValid(userName, password)) {
- if(isTeasher(password)) {
- Intent i = new Intent(this, Teasher.class);
- i.putExtra("CurrentUser", userName);
- startActivity(i);
- }
- else{
- Intent i = new Intent(this, WelcomeActivity.class);
- i.putExtra("CurrentUser", userName);
- startActivity(i);
- }
- } else {
- Toast.makeText(this, "Invalid username or password!", Toast.LENGTH_LONG).show();
- }
- break;
- case R.id.btnCancel:
- this.finish();
- break;
- case R.id.btnRegister:
- userName = etUserName.getText().toString();
- password = etPassword.getText().toString();
- if (!isUserExist(userName)) {
- if (addNewUser(userName, password)) {
- Toast.makeText(this, "User was added!", Toast.LENGTH_LONG).show();
- etUserName.setText("");
- etPassword.setText("");
- } else {
- Toast.makeText(this, "Error adding a user " + userName, Toast.LENGTH_LONG).show();
- }
- }
- break;
- default:
- Toast.makeText(this, "Error on click", Toast.LENGTH_LONG).show();
- break;
- }
- }
- private boolean isTeasher(String password) {
- String s = password.substring(0,4);
- return (s.equals("0000"));
- }
- private boolean addNewUser(String userName, String password) {
- SharedPreferences prefs = this.getSharedPreferences("myUsers", Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = prefs.edit();
- editor.putString(userName, password);
- return editor.commit();
- }
- private boolean isUserExist(String userName) {
- SharedPreferences prefs = this.getSharedPreferences("myUsers", Context.MODE_PRIVATE);
- String savedPassword = prefs.getString(userName, "");
- return (!savedPassword.equals(""));
- }
- private boolean isValid(String userName, String password) {
- SharedPreferences prefs = this.getSharedPreferences("myUsers", Context.MODE_PRIVATE);
- String savedPassword = prefs.getString(userName, "");
- return (savedPassword.equals(password));
- }
- }
- WelcomeActivity
- ----------------
- package com.example.asusproi5win10.myapplication;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.database.DataSetObserver;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- public class WelcomeActivity extends AppCompatActivity implements View.OnClickListener{
- private TextView tvWelcome;
- private String userName;
- private ListView lsvGrades;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_welcome);
- userName = getIntent().getStringExtra("CurrentUser");
- tvWelcome = (TextView) findViewById(R.id.tvWellcome);
- lsvGrades = (ListView) findViewById(R.id.lsvGrades);
- findViewById(R.id.btnCancel).setOnClickListener(this);
- tvWelcome.setText(userName);
- //lsvGrades.setAdapter(new GradesAdapter(userName,this));
- }
- @Override
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.btnCancel:
- this.finish();
- break;
- default:
- Toast.makeText(this, "Error on click", Toast.LENGTH_LONG).show();
- break;
- }
- }
- }
- Teasher
- --------
- package com.example.asusproi5win10.myapplication;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- import android.os.Bundle;
- import android.app.AlertDialog;
- import android.content.ClipData;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Set;
- import static android.os.Build.VERSION_CODES.M;
- import static com.example.asusproi5win10.myapplication.R.id.btnAdd;
- import static com.example.asusproi5win10.myapplication.R.id.etGrade;
- import static com.example.asusproi5win10.myapplication.R.id.etStudentName;
- import static com.example.asusproi5win10.myapplication.R.id.tvItemSubject;
- import static com.example.asusproi5win10.myapplication.R.id.tvSubject;
- public class Teasher extends AppCompatActivity implements View.OnClickListener {
- private TextView tvTeasher;
- private static ListView lsvStudent;
- Context context;
- private static String userName;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_teasher);
- context = this;
- userName = getIntent().getStringExtra("CurrentUser");
- lsvStudent = (ListView) findViewById(R.id.lsvStudent);
- tvTeasher = (TextView) findViewById(R.id.tvTeasher);
- findViewById(R.id.btnCancel).setOnClickListener(this);
- findViewById(R.id.btnAdd).setOnClickListener(this);
- tvTeasher.setText(userName);
- lsvStudent.setAdapter(new GradeAdapter(userName,this));
- }
- public static void getGrade(final Context context) {
- AlertDialog.Builder bldr = new AlertDialog.Builder(context);
- final View v = LayoutInflater.from(context).inflate(R.layout.dialog_add_grade, null);
- bldr.setView(v);
- bldr.setTitle("Add New Student");
- bldr.setPositiveButton("Save", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- String studentName = ((EditText) v.findViewById(R.id.etStudentName)).getText().toString();
- int grade = Integer.parseInt(((EditText) v.findViewById(R.id.etGrade)).getText().toString());
- if (!studentName.isEmpty()) {
- saveGrade(context, studentName, grade);
- }
- }
- });
- bldr.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- bldr.show();
- }
- private static void saveGrade(Context context, String studentName, int grade) {
- SharedPreferences sharedPrefs = context.getSharedPreferences(userName, Context.MODE_PRIVATE);
- sharedPrefs.edit().putInt( studentName,grade).commit();
- lsvStudent.setAdapter(new GradeAdapter(userName,context));
- }
- private List<Grade> loadFromFile() {
- SharedPreferences sharedPrefs = context.getSharedPreferences(userName, Context.MODE_PRIVATE);
- Object[] keys = sharedPrefs.getAll().keySet().toArray();
- List<Grade> lst = new ArrayList<>();
- for (int i = 0; i < keys.length; i += 1) {
- String name = (String) keys[i];
- lst.add(new Grade(name, sharedPrefs.getInt(name, 0)));
- }
- return lst;
- }
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.btnAdd:
- getGrade(context);
- break;
- case R.id.btnCancel:
- this.finish();
- break;
- default:
- Toast.makeText(this, "Error on click", Toast.LENGTH_LONG).show();
- break;
- }
- }
- }
- GradesAdapter
- -------------
- package com.example.asusproi5win10.myapplication;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
- public class GradesAdapter extends BaseAdapter {
- private Context context;
- private String userName;
- private ListView lsvGrades;
- private List<GradeItem> gradesList;
- private static String studentName;
- public GradesAdapter(String userName, Context context) {
- this.userName = userName;
- this.context = context;
- this.gradesList = LoadStudentGrades( userName, context);
- }
- @Override
- public int getCount() {
- return gradesList.size();
- }
- @Override
- public Object getItem(int i) {
- return gradesList
- .get(i);
- }
- @Override
- public long getItemId(int i) {
- return 0;
- }
- @Override
- public View getView(final int i, View view, ViewGroup viewGroup) {
- View rowView = LayoutInflater.from(context).inflate(R.layout.grade_item_layout, null);
- TextView tvSubjectName = (TextView) rowView.findViewById(R.id.tvItemSubject);
- final EditText etGrade = (EditText) rowView.findViewById(R.id.etItemGrade);
- tvSubjectName.setText(gradesList.get(i).getSubjectName());
- etGrade.setText(gradesList.get(i).getGrade() + "");
- LoadStudentGrades( userName, context);
- return rowView;
- }
- private List<GradeItem> LoadStudentGrades (String userName, Context context){
- SharedPreferences gradePrefs = context.getSharedPreferences(userName, Context.MODE_PRIVATE);
- List<GradeItem> res = new ArrayList<>();
- Object[] subjects = gradePrefs.getAll().keySet().toArray();
- for (int i = 0; i < subjects.length; i += 1) {
- String subjectName = (String) subjects[i];
- int grade = gradePrefs.getInt(subjectName, 0);
- res.add(new GradeItem(subjectName, grade));
- }
- return res;
- }
- }
- GradeAdapter
- ------------
- package com.example.asusproi5win10.myapplication;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- public class GradeAdapter extends BaseAdapter {
- private Context context;
- private String studentName;
- private String userName;
- private List<Grade> lst;
- public GradeAdapter(String userName, Context context) {
- this.userName = userName;
- this.context = context;
- this.lst = LoadStudentGrades(userName, context);
- }
- @Override
- public int getCount() {
- return lst.size();
- }
- @Override
- public Object getItem(int i) {
- return lst.get(i);
- }
- @Override
- public long getItemId(int i) {
- return 0;
- }
- @Override
- public View getView(final int i, View view, ViewGroup viewGroup) {
- View rowView = LayoutInflater.from(context).inflate(R.layout.activity_grade_adapter, null);
- TextView etStudentName = (TextView) rowView.findViewById(R.id.tvItemStudentName);
- final EditText etGrade = (EditText) rowView.findViewById(R.id.etItemGrade);
- etStudentName.setText(lst.get(i).getStudentName());
- etGrade.setText( lst.get(i).getGrade() + "" );
- LoadStudentGrades( userName, context);
- return rowView;
- }
- public static List<Grade> LoadStudentGrades(String userName, Context context) {
- SharedPreferences sharedPrefs = context.getSharedPreferences(userName, Context.MODE_PRIVATE);
- List<Grade> res = new ArrayList<>();
- Object[] studentsNames = sharedPrefs.getAll().keySet().toArray();
- for (int i = 0; i < studentsNames.length; i += 1) {
- String SName = (String) studentsNames[i];
- int grade = sharedPrefs.getInt(SName, 0);
- res.add(new Grade(SName, grade));
- }
- return res;
- }
- }
- Grade
- -------
- package com.example.asusproi5win10.myapplication;
- public class Grade {
- private String StudentName;
- private int grade;
- public Grade(String StudentName, int grade) {
- this.StudentName = StudentName;
- this.grade = grade;
- }
- public String getStudentName() {
- return StudentName;
- }
- public void setStudentName(String StudentName) {
- this.StudentName = StudentName;
- }
- public int getGrade() {
- return grade;
- }
- public void setGrade(int grade) {
- this.grade = grade;
- }
- }
- GradeItem
- -----------
- package com.example.asusproi5win10.myapplication;
- public class GradeItem {
- private String subjectName;
- private int grade;
- public GradeItem(String subjectName, int grade) {
- this.subjectName = subjectName;
- this.grade = grade;
- }
- public String getSubjectName() {
- return subjectName;
- }
- public void setSubjectName(String subjectName) {
- this.subjectName = subjectName;
- }
- public int getGrade() {
- return grade;
- }
- public void setGrade(int grade) {
- this.grade = grade;
- }
- }
- activity_teasher
- -----------------
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_teasher"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.example.asusproi5win10.myapplication.Teasher">
- <TextView
- android:id="@+id/tvTeasher"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentEnd="true"
- android:layout_alignParentStart="true"
- android:layout_alignParentTop="true"
- android:layout_marginTop="12dp" />
- <Button
- android:id="@+id/btnCancel"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.5"
- android:text="Cancel"
- android:textSize="20sp"
- android:layout_alignParentBottom="true"
- android:layout_alignParentStart="true" />
- <Button
- android:id="@+id/btnAdd"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.5"
- android:text="Add"
- android:textSize="20sp"
- android:layout_below="@+id/tvTeasher"
- android:layout_alignParentStart="true"
- android:layout_marginTop="19dp" />
- <ListView
- android:id="@+id/lsvStudent"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/btnAdd"
- android:layout_alignParentEnd="true">
- </ListView>
- </RelativeLayout>
- activity_main
- ---------------
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:weightSum="1">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="15dp"
- android:gravity="center"
- android:text="@string/app_name"
- android:textColor="#000ff0"
- android:textSize="32sp" />
- <EditText
- android:id="@+id/etUserName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="15dp"
- android:hint="UserName"
- android:textSize="24sp" />
- <EditText
- android:id="@+id/etPassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Password"
- android:inputType="textPassword"
- android:textSize="24sp" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:layout_weight="0.14">
- <Button
- android:id="@+id/btnLogin"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="10dp"
- android:layout_weight="1"
- android:text="Login"
- android:textSize="18sp" />
- <Button
- android:id="@+id/btnRegister"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="10dp"
- android:layout_weight="1"
- android:text="Register"
- android:textSize="18sp" />
- <Button
- android:id="@+id/btnCancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="10dp"
- android:layout_weight="2.02"
- android:text="Cancel"
- android:textSize="18sp" />
- </LinearLayout>
- </LinearLayout>
- activity_welcome
- -----------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:weightSum="1">
- <TextView
- android:id="@+id/tvWellcome"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="20sp" />
- <ListView
- android:id="@+id/lsvGrades"
- android:layout_width="match_parent"
- android:layout_height="284dp"
- android:layout_weight="0.97">
- </ListView>
- <Button
- android:id="@+id/btnCancel"
- android:layout_width="match_parent"
- android:layout_height="63dp"
- android:layout_margin="10dp"
- android:text="Cancel"
- android:textSize="18sp" />
- </LinearLayout>
- grade_item_layout
- -------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="#00fff0"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/tvItemSubject"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="Subject Name"
- android:textSize="20sp" />
- <EditText
- android:id="@+id/etItemGrade"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:inputType="number"
- android:maxLength="3"
- android:text="Grade"
- android:textSize="20sp" />
- </LinearLayout>
- dialog_add_grade
- ------------------
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/etStudentName"
- android:hint="Enter Student..." />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/etGrade"
- android:hint="Enter Grade..." />
- </LinearLayout>
- activity_grade_adapter
- -----------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="#00fff0"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/tvItemStudentName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="Student Name"
- android:textSize="20sp" />
- <EditText
- android:id="@+id/etItemGrade"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:inputType="number"
- android:maxLength="3"
- android:text="Grade"
- android:textSize="20sp" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement