From 2cb60b06acd27455f6bfbca48d7557d5ec1afa2d Mon Sep 17 00:00:00 2001 From: IrinaVasekha Date: Thu, 28 Feb 2019 20:50:47 +0300 Subject: [PATCH 1/3] HW2 --- .idea/gradle.xml | 9 +- .idea/misc.xml | 7 +- .idea/vcs.xml | 2 +- app/build.gradle | 6 + .../ok/technopolis/students/MainActivity.java | 156 ++++++++++++++++++ .../technopolis/students/StudentsAdapter.java | 68 ++++++++ app/src/main/res/layout/activity_main.xml | 95 +++++++++++ app/src/main/res/layout/student_item.xml | 32 ++++ app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 6 + 10 files changed, 369 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java create mode 100644 app/src/main/res/layout/student_item.xml diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 7ac24c7..2996d53 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -3,14 +3,11 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index af0bbdd..7bfef59 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,11 +1,6 @@ - - - - - - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 3710062..30e7247 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,6 +16,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + compileOptions { + sourceCompatibility = '1.8' + targetCompatibility = '1.8' + } } dependencies { @@ -25,4 +29,6 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation 'com.android.support:recyclerview-v7:28.0.0' + implementation 'de.hdodenhof:circleimageview:3.0.0' } diff --git a/app/src/main/java/ru/ok/technopolis/students/MainActivity.java b/app/src/main/java/ru/ok/technopolis/students/MainActivity.java index adde500..021e4e4 100644 --- a/app/src/main/java/ru/ok/technopolis/students/MainActivity.java +++ b/app/src/main/java/ru/ok/technopolis/students/MainActivity.java @@ -2,13 +2,169 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.Toast; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import de.hdodenhof.circleimageview.CircleImageView; public class MainActivity extends AppCompatActivity { + private List students; + private StudentsAdapter studentsAdapter; + private Student currentStudent; + private EditText firstNameView; + private EditText lastNameView; + private CheckBox genderView; + private CircleImageView photoView; + private Button buttonAdd; + private Button buttonSave; + private Button buttonDelete; + final private Random random = new Random(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + RecyclerView recyclerView = findViewById(R.id.students_list); + students = generateStudentList(); + studentsAdapter = new StudentsAdapter(students, this::onStudentClick); + recyclerView.setAdapter(studentsAdapter); + LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); + recyclerView.setLayoutManager(mLayoutManager); + firstNameView = findViewById(R.id.student_first_name); + lastNameView = findViewById(R.id.student_last_name); + genderView = findViewById(R.id.student_gender); + photoView = findViewById(R.id.student_photo); + buttonAdd = findViewById(R.id.button_add); + buttonSave = findViewById(R.id.button_save); + buttonDelete = findViewById(R.id.button_delete); + setupAddButton(); + setupDeleteButton(); + setupSaveButton(); + } + + private void setupAddButton() { + buttonAdd.setOnClickListener(v -> { + currentStudent = generateNewStudent(); + if (currentStudent != null) { + students.add(currentStudent); + studentsAdapter.notifyDataSetChanged(); + cleanCard(); + } + }); + } + + private void setupSaveButton() { + buttonSave.setOnClickListener(v -> { + if (currentStudent != null) { + if(editStudent()) { + studentsAdapter.notifyDataSetChanged(); + cleanCard(); + } + return; + } + Toast toast = Toast.makeText(getApplicationContext(), + "Choose a student", Toast.LENGTH_SHORT); + toast.show(); + }); + } + + private void setupDeleteButton() { + buttonDelete.setOnClickListener(v -> { + int index = students.indexOf(currentStudent); + if (index != -1) { + students.remove(currentStudent); + studentsAdapter.notifyDataSetChanged(); + cleanCard(); + return; + } + Toast toast = Toast.makeText(getApplicationContext(), + "Choose a student", Toast.LENGTH_SHORT); + toast.show(); + }); } + private void cleanCard() { + firstNameView.setText(""); + lastNameView.setText(""); + genderView.setChecked(false); + photoView.setImageResource(R.color.colorGray); + currentStudent = null; + } + + public void onStudentClick(Student student) { + currentStudent = student; + setStudentCard(); + } + + private void setStudentCard() { + firstNameView.setText(currentStudent.getFirstName()); + lastNameView.setText(currentStudent.getSecondName()); + genderView.setChecked(currentStudent.isMaleGender()); + photoView.setImageResource(currentStudent.getPhoto()); + } + + private int choosePhoto(boolean isMale){ + String photoName; + if (isMale) { + photoName = "male_"; + } else { + photoName = "female_"; + } + return getResources().getIdentifier(photoName + (random.nextInt(3)+1), "drawable", getPackageName()); + + } + + private Student generateNewStudent() { + String firstName = firstNameView.getText().toString(); + String lastName = lastNameView.getText().toString(); + if (firstName.length() != 0 && lastName.length() != 0) { + boolean isMale = genderView.isChecked(); + return new Student(firstName, lastName, isMale, choosePhoto(isMale)); + } + Toast toast = Toast.makeText(getApplicationContext(), + "Fill first name and last name", Toast.LENGTH_SHORT); + toast.show(); + return null; + } + + private boolean editStudent() { + String firstName = firstNameView.getText().toString(); + String lastName = lastNameView.getText().toString(); + if (firstName.length() != 0 && lastName.length() != 0) { + currentStudent.setFirstName(firstName); + currentStudent.setSecondName(lastName); + boolean oldIsMale = currentStudent.isMaleGender(); + currentStudent.setMaleGender(genderView.isChecked()); + if (oldIsMale != currentStudent.isMaleGender()) { + currentStudent.setPhoto(choosePhoto(currentStudent.isMaleGender())); + cleanCard(); + } + return true; + } + Toast toast = Toast.makeText(getApplicationContext(), + "Fill first name and last name", Toast.LENGTH_SHORT); + toast.show(); + return false; + } + + private List generateStudentList() { + List students = new ArrayList<>(); + students.add(new Student("Ivan", "Ivanov", true, R.drawable.male_1)); + students.add(new Student("Rita", "Ritina", false, R.drawable.female_1)); + students.add(new Student("Petr", "Petrov", true, R.drawable.male_2)); + students.add(new Student("Masha", "Mashina", false, R.drawable.female_2)); + students.add(new Student("Kyzya", "Kyzin", true, R.drawable.male_3)); + students.add(new Student("Tanya", "Tanina", false, R.drawable.female_3)); + return students; + } } diff --git a/app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java b/app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java new file mode 100644 index 0000000..0435ee4 --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/students/StudentsAdapter.java @@ -0,0 +1,68 @@ +package ru.ok.technopolis.students; + +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import java.util.List; + +public class StudentsAdapter extends RecyclerView.Adapter { + + private final List students; + private final Listener onStudentClickListener; + + public StudentsAdapter(List students, Listener onStudentClickListener) { + this.students = students; + this.onStudentClickListener = onStudentClickListener; + } + + @NonNull + @Override + public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { + View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.student_item, viewGroup, false); + view.setOnClickListener(v -> onStudentClickListener.onStudentClick((Student) v.getTag())); + return new StudentViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull StudentViewHolder viewHolder, final int i) { + Student student = students.get(i); + viewHolder.bind(student); + viewHolder.itemView.setTag(student); + } + + @Override + public int getItemCount() { + return students.size(); + } + + static final class StudentViewHolder extends RecyclerView.ViewHolder { + + private final TextView nameEditView; + private final ImageView photoImageView; + + private StudentViewHolder(@NonNull View itemView) { + super(itemView); + nameEditView = itemView.findViewById(R.id.student_item_name); + photoImageView = itemView.findViewById(R.id.student_item_photo); + } + + private void bind(@NonNull Student student) { + nameEditView.setText(student.getFirstName() + " " + student.getSecondName()); + photoImageView.setImageResource(student.getPhoto()); + } + + } + + interface Listener { + + void onStudentClick(Student student); + + } + + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 304e72d..d3ffd77 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,104 @@ + + + + + + + + +