Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GradeItem.java
- --------------
- package com.example.mohamadpc.myapplication4;
- /**
- * Created by MOHAMADPC on 03/10/2017.
- */
- 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;
- }
- }
- GradesAdapter.java
- ------------------
- package com.example.mohamadpc.myapplication4;
- 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;
- /**
- * Created by MOHAMADPC on 03/10/2017.
- */
- public class GradesAdapter extends BaseAdapter {
- private Context context;
- private String userName;
- private List<GradeItem> gradesList;
- 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) {
- /*TextView tvSubject = new TextView(context);
- tvSubject.setTextSize(30);
- tvSubject.setText(gradesList.get(i).getSubjectName() + " : " + gradesList.get(i).getGrade());
- return tvSubject; */
- 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() + "");
- rowView.findViewById(R.id.btnItemSave).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- int mynewGrade = Integer.parseInt(etGrade.getText().toString());
- gradesList.get(i).setGrade(mynewGrade);
- SaveStudentGrades(userName, i, context);
- Toast.makeText(context, "Update OK!", Toast.LENGTH_SHORT).show();
- }
- });
- return rowView;
- }
- private List<GradeItem> LoadStudentGrades(String userName, Context context) {
- SharedPreferences gradePrefs = context.getSharedPreferences(userName, Context.MODE_PRIVATE);
- List<GradeItem> res = new ArrayList<>();
- // not soo good
- 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;
- }
- private void SaveStudentGrades(String userName, int i, Context context) {
- SharedPreferences gradePrefs = context.getSharedPreferences(userName, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = gradePrefs.edit();
- editor.putInt(gradesList.get(i).getSubjectName(), gradesList.get(i).getGrade());
- editor.commit();
- }
- }
- MainActivity.java
- -----------------
- package com.example.mohamadpc.myapplication4;
- 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;
- @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)) {
- Intent i = new Intent(this, WelcomeActivity.class);
- i.putExtra("CurrentUser", userName);
- startActivity(i);
- //this.finish();
- } 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 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.java
- --------------------
- package com.example.mohamadpc.myapplication4;
- 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 java.util.ArrayList;
- import java.util.List;
- public class WelcomeActivity extends AppCompatActivity {
- private TextView tvWelcome;
- private String userName;
- private ListView lsvGrades;
- private List<GradeItem> myGrades;
- @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);
- tvWelcome.setText(userName);
- myGrades = new ArrayList<GradeItem>();
- myGrades.add(new GradeItem("Arabic", 90));
- myGrades.add(new GradeItem("Hebrew", 99));
- myGrades.add(new GradeItem("Math", 83));
- myGrades.add(new GradeItem("History", 20));
- myGrades.add(new GradeItem("English", 98));
- myGrades.add(new GradeItem("Fatin", 30));
- myGrades.add(new GradeItem("Ayoub", 80));
- myGrades.add(new GradeItem("Lolo", 100));
- GradesAdapter gAdapter = new GradesAdapter(userName, this);
- lsvGrades.setAdapter(gAdapter);
- }
- }
- activity_layout.xml
- -------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="15dp"
- android:gravity="center"
- android:text="@string/login_title"
- 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="@string/user_name_hint"
- 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">
- <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="1"
- android:text="Cancel"
- android:textSize="18sp" />
- </LinearLayout>
- </LinearLayout>
- activity_welcome.xml
- --------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <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="match_parent"></ListView>
- </LinearLayout>
- grade_item_layout.xml
- ---------------------
- <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" />
- <Button
- android:id="@+id/btnItemSave"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="Save"
- android:textSize="20sp" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement