This repository has been archived by the owner on Mar 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Students homework android #9
Open
BrainLUX
wants to merge
6
commits into
polis-vk:master
Choose a base branch
from
BrainLUX:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f0bf643
Students homework android
BrainLUX db8759c
Students homework android
BrainLUX 65715de
Students homework android (fixed code style errors)
BrainLUX 1c6fdc9
Students homework android (fixed)
BrainLUX 8cf1ab0
Students homework android final
BrainLUX c966c51
Students homework android final
BrainLUX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,26 +12,29 @@ | |
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Random; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
private List<Student> students; | ||
private EditText surName, name; | ||
private EditText studentSurName; | ||
private EditText studentName; | ||
private CheckBox gender; | ||
private ImageView avatar; | ||
private int index = -1, imageResource; | ||
private StudentAdapter studentAdapter; | ||
private Random random = new Random(); | ||
private int[] men, women; | ||
private View currentView; | ||
private View studentLeftBorder; | ||
private View studentRightBorder; | ||
private View studentTopBorder; | ||
private View studentBottomBorder; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
Objects.requireNonNull(getSupportActionBar()).hide(); | ||
setupFields(); | ||
setupRecyclerView(); | ||
setupButtons(); | ||
|
@@ -53,8 +56,8 @@ private void setupRecyclerView() { | |
} | ||
|
||
private void setupFields() { | ||
surName = findViewById(R.id.activity_main__studentSurName); | ||
name = findViewById(R.id.activity_main__studentName); | ||
studentSurName = findViewById(R.id.activity_main__studentSurName); | ||
studentName = findViewById(R.id.activity_main__studentName); | ||
gender = findViewById(R.id.activity_main__checkGender); | ||
avatar = findViewById(R.id.activity_main__studentPicture); | ||
men = new int[]{R.drawable.male_1, R.drawable.male_2, R.drawable.male_3}; | ||
|
@@ -64,7 +67,7 @@ private void setupFields() { | |
|
||
private void onAddClick() { | ||
if (index != -1 && isDataSaved()) | ||
Toast.makeText(MainActivity.this, "Для начала сохраните уже созданного студента", Toast.LENGTH_SHORT).show(); | ||
Toast.makeText(MainActivity.this, getResources().getString(R.string.userShouldSaveCreated_warning), Toast.LENGTH_SHORT).show(); | ||
else { | ||
int randomGender = random.nextInt(2); | ||
Student student = new Student("", "", randomGender == 1, 0); | ||
|
@@ -77,8 +80,9 @@ private void onAddClick() { | |
} | ||
|
||
private int getRandomPhoto(boolean maleGender) { | ||
if (maleGender) | ||
if (maleGender) { | ||
return men[random.nextInt(men.length)]; | ||
} | ||
return women[random.nextInt(women.length)]; | ||
} | ||
|
||
|
@@ -89,67 +93,79 @@ private void onChecked() { | |
|
||
private void clearData() { | ||
index = -1; | ||
name.setText(""); | ||
surName.setText(""); | ||
studentName.setText(""); | ||
studentSurName.setText(""); | ||
gender.setChecked(false); | ||
avatar.setImageResource(0); | ||
name.setError(null); | ||
surName.setError(null); | ||
studentName.setError(null); | ||
studentSurName.setError(null); | ||
setBorderVisibility(currentView, View.INVISIBLE); | ||
} | ||
|
||
private void onStudentClick(View view, int i) { | ||
if (index != -1) { | ||
if (checkFields()) | ||
if (isDataSaved()) | ||
Toast.makeText(MainActivity.this, "Вам следует сохранить изменения", Toast.LENGTH_SHORT).show(); | ||
else | ||
if (checkFields()) { | ||
if (isDataSaved()) { | ||
Toast.makeText(MainActivity.this, getResources().getString(R.string.userShouldSave_warning), Toast.LENGTH_SHORT).show(); | ||
} else { | ||
setStudent(view, i); | ||
} else | ||
} | ||
} | ||
} else { | ||
setStudent(view, i); | ||
} | ||
} | ||
|
||
private boolean isDataSaved() { | ||
return students.get(index).getFirstName().trim().length() == 0 || students.get(index).getSecondName().trim().length() == 0; | ||
} | ||
|
||
private boolean checkFields() { | ||
if (name.getText().toString().trim().length() > 0 && surName.getText().toString().trim().length() > 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше всегда расставлять фигурные скобки |
||
if (studentName.getText().toString().trim().length() > 0 && studentSurName.getText().toString().trim().length() > 0) | ||
return true; | ||
if (name.getText().toString().trim().length() == 0) | ||
name.setError("Имя не может быть пустым"); | ||
if (surName.getText().toString().trim().length() == 0) | ||
surName.setError("Фамилия не может быть пустой"); | ||
if (studentName.getText().toString().trim().length() == 0) | ||
studentName.setError(getResources().getString(R.string.studentName_error)); | ||
if (studentSurName.getText().toString().trim().length() == 0) | ||
studentSurName.setError(getResources().getString(R.string.studentSurName_error)); | ||
return false; | ||
} | ||
|
||
private void setStudent(View view, int i) { | ||
surName.setText(students.get(i).getSecondName()); | ||
name.setText(students.get(i).getFirstName()); | ||
studentSurName.setText(students.get(i).getSecondName()); | ||
studentName.setText(students.get(i).getFirstName()); | ||
gender.setChecked(students.get(i).isMaleGender()); | ||
avatar.setImageResource(students.get(i).getPhoto()); | ||
index = i; | ||
if (currentView != null) | ||
setBorderVisibility(currentView, View.INVISIBLE); | ||
if (view != null) | ||
setBorders(view); | ||
currentView = view; | ||
setBorderVisibility(view, View.VISIBLE); | ||
updateButtons(true); | ||
} | ||
|
||
private void setBorders(View view) { | ||
studentLeftBorder = view.findViewById(R.id.student_item__left); | ||
studentRightBorder = view.findViewById(R.id.student_item__right); | ||
studentTopBorder = view.findViewById(R.id.student_item__top); | ||
studentBottomBorder = view.findViewById(R.id.student_item__bottom); | ||
} | ||
|
||
private void updateButtons(boolean value) { | ||
name.setEnabled(value); | ||
surName.setEnabled(value); | ||
studentName.setEnabled(value); | ||
studentSurName.setEnabled(value); | ||
gender.setEnabled(value); | ||
findViewById(R.id.activity_main__save_student_button).setEnabled(value); | ||
findViewById(R.id.activity_main__delete_student_button).setEnabled(value); | ||
} | ||
|
||
private void setBorderVisibility(View view, int visible) { | ||
if (view != null) { | ||
view.findViewById(R.id.student_item__top).setVisibility(visible); | ||
view.findViewById(R.id.student_item__bottom).setVisibility(visible); | ||
view.findViewById(R.id.student_item__left).setVisibility(visible); | ||
view.findViewById(R.id.student_item__right).setVisibility(visible); | ||
studentTopBorder.setVisibility(visible); | ||
studentBottomBorder.setVisibility(visible); | ||
studentLeftBorder.setVisibility(visible); | ||
studentRightBorder.setVisibility(visible); | ||
} | ||
} | ||
|
||
|
@@ -159,28 +175,24 @@ private void onSaveClick() { | |
students.get(index).setSecondName(getLastName()); | ||
students.get(index).setMaleGender(gender.isChecked()); | ||
students.get(index).setPhoto(imageResource); | ||
Toast.makeText(MainActivity.this, "Студент сохранён", Toast.LENGTH_SHORT).show(); | ||
Toast.makeText(MainActivity.this, getResources().getString(R.string.student_saved), Toast.LENGTH_SHORT).show(); | ||
studentAdapter.notifyDataSetChanged(); | ||
} | ||
|
||
} | ||
|
||
private void onDeleteClick() { | ||
updateButtons(false); | ||
students.remove(index); | ||
studentAdapter.notifyDataSetChanged(); | ||
Toast.makeText(MainActivity.this, "Студент удалён", Toast.LENGTH_SHORT).show(); | ||
Toast.makeText(MainActivity.this, getResources().getString(R.string.student_removed), Toast.LENGTH_SHORT).show(); | ||
clearData(); | ||
|
||
} | ||
|
||
private String getFirstName() { | ||
return String.valueOf(name.getText()); | ||
return String.valueOf(studentName.getText()); | ||
} | ||
|
||
private String getLastName() { | ||
return String.valueOf(surName.getText()); | ||
return String.valueOf(studentSurName.getText()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1)Можно писать просто this
2) Toast.makeText может принимать вторым аргументом прямо id строки: R.string.userShouldSaveCreated_warning