Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

Students list #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions .idea/codeStyles/Project.xml

This file was deleted.

18 changes: 0 additions & 18 deletions .idea/gradle.xml

This file was deleted.

14 changes: 0 additions & 14 deletions .idea/misc.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
api 'com.android.support.constraint:constraint-layout:1.1.3'
api 'com.android.support:recyclerview-v7:28.0.0'
api 'com.android.support:design:28.0.0'
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'
Expand Down
194 changes: 192 additions & 2 deletions app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,204 @@
package ru.ok.technopolis.students;

import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends Activity {

private List<Student> students;
private StudentAdapter studentAdapter;
private Student currentStudent;
private Student prevStudent;
private final Student DEFAULTPREV = new Student(" ", " ", false, android.R.color.transparent);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше DEFAULT_PREV

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему вместо id фото стоит цвет?


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.list);
students = new ArrayList<>();
prevStudent = DEFAULTPREV;
studentAdapter = new StudentAdapter(students, new StudentAdapter.Listener() {
@Override
public void onStudentClick(Student student) {
EditText firstName = findViewById(R.id.first_name_info);
firstName.setText(student.getFirstName());
if (student.getFirstName().equals("")) {
firstName.setHint(R.string.default_firstname);
}
EditText secondName = findViewById(R.id.second_name_info);
secondName.setText(student.getSecondName());
if (student.getSecondName().equals("")) {
secondName.setHint(R.string.default_secondname);
}
ImageView photo = findViewById(R.id.big_photo);
photo.setImageResource(student.getPhoto());
CheckBox checkBox = findViewById(R.id.checkbox);
checkBox.setChecked(student.isMaleGender());
student.setInFocus(true);
if (currentStudent != null) {
prevStudent = currentStudent;
}
currentStudent = student;
if (!prevStudent.equals(currentStudent)) {
prevStudent.setInFocus(false);
}
studentAdapter.notifyDataSetChanged();
}
});
recyclerView.setAdapter(studentAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
setupAddButton();
setupDeleteButton();
setupSaveButton();
}

private void setupAddButton() {
Button addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random random = new Random();
boolean maleGender = random.nextBoolean();
int startPhoto = 0;
if (maleGender) {
switch (random.nextInt(3)) {
case 0: startPhoto = R.drawable.male_1;
break;
case 1: startPhoto = R.drawable.male_2;
break;
case 2: startPhoto = R.drawable.male_3;
break;
}
} else {
switch (random.nextInt(3)) {
case 0: startPhoto = R.drawable.female_1;
break;
case 1: startPhoto = R.drawable.female_2;
break;
case 2: startPhoto = R.drawable.female_3;
break;
}
}
students.add(new Student("", "", maleGender, startPhoto));
studentAdapter.notifyDataSetChanged();
}
});
}

private void setupDeleteButton() {
Button deleteButton = findViewById(R.id.delete_button);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int size = students.size();
if (size != 0) {
if (size > 1) {
int index = students.indexOf(currentStudent);
if (index == 0) {
++index;
} else {
if (index == size - 1) {
--index;
} else {
++index;
}
}
Student tempStd = currentStudent;
currentStudent = students.get(index);
currentStudent.setInFocus(true);
students.remove(tempStd);
EditText firstName = findViewById(R.id.first_name_info);
firstName.setText(currentStudent.getFirstName());
EditText secondName = findViewById(R.id.second_name_info);
secondName.setText(currentStudent.getSecondName());
ImageView photo = findViewById(R.id.big_photo);
photo.setImageResource(currentStudent.getPhoto());
CheckBox checkBox = findViewById(R.id.checkbox);
checkBox.setChecked(currentStudent.isMaleGender());
} else {
students.remove(currentStudent);
currentStudent = null;
prevStudent = DEFAULTPREV;
EditText firstName = findViewById(R.id.first_name_info);
firstName.setText("");
firstName.setHint(R.string.default_firstname);
EditText secondName = findViewById(R.id.second_name_info);
secondName.setText("");
secondName.setHint(R.string.default_secondname);
ImageView photo = findViewById(R.id.big_photo);
photo.setImageResource(android.R.color.transparent);
CheckBox checkBox = findViewById(R.id.checkbox);
checkBox.setChecked(false);
}
}
studentAdapter.notifyDataSetChanged();
}
});
}

private void setupSaveButton() {
Button saveButton = findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentStudent != null) {
EditText firstNameInfo = findViewById(R.id.first_name_info);
EditText secondNameInfo = findViewById(R.id.second_name_info);
firstNameInfo.setText(firstNameInfo.getText().toString().trim().replace("\n", "").replace(" ", ""));
secondNameInfo.setText(secondNameInfo.getText().toString().trim().replace("\n", "").replace(" ", ""));
if (!firstNameInfo.getText().toString().equals("") &&
!secondNameInfo.getText().toString().equals("")) {
int index = students.indexOf(currentStudent);
currentStudent.setFirstName(firstNameInfo.getText().toString());
currentStudent.setSecondName(secondNameInfo.getText().toString());
ImageView image = findViewById(R.id.big_photo);
CheckBox checkBox = findViewById(R.id.checkbox);
if (checkBox.isChecked() != currentStudent.isMaleGender()) {
Random random = new Random();
if (checkBox.isChecked()) {
switch (random.nextInt(3)) {
case 0: currentStudent.setPhoto(R.drawable.male_1);
break;
case 1: currentStudent.setPhoto(R.drawable.male_2);
break;
case 2: currentStudent.setPhoto(R.drawable.male_3);
break;
}
} else {
switch (random.nextInt(3)) {
case 0: currentStudent.setPhoto(R.drawable.female_1);
break;
case 1: currentStudent.setPhoto(R.drawable.female_2);
break;
case 2: currentStudent.setPhoto(R.drawable.female_3);
break;
}
}
}
image.setImageResource(currentStudent.getPhoto());
currentStudent.setMaleGender(checkBox.isChecked());
students.set(index, currentStudent);
studentAdapter.notifyDataSetChanged();
}
else {
Toast.makeText(MainActivity.this, R.string.toast_invalid, Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ public class Student {
private String secondName;
private boolean maleGender;
private int photo;
private boolean inFocus;

public Student(String firstName, String secondName, boolean maleGender, int photo) {
this.firstName = firstName;
this.secondName = secondName;
this.maleGender = maleGender;
this.photo = photo;
this.inFocus = false;
}

public String getFirstName() {
Expand Down Expand Up @@ -45,4 +47,12 @@ public int getPhoto() {
public void setPhoto(int photo) {
this.photo = photo;
}

public void setInFocus(boolean focus) {
this.inFocus = focus;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут наверное стоит сделать название поля класса тоже focus

}

public boolean getInFocus() {
return this.inFocus;
}
}
Loading