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 list #15
Open
yaroslaver
wants to merge
7
commits into
polis-vk:master
Choose a base branch
from
yaroslaver: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.
+488
−84
Open
Students list #15
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
194 changes: 192 additions & 2 deletions
194
app/src/main/java/ru/ok/technopolis/students/MainActivity.java
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 |
---|---|---|
@@ -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); | ||
|
||
@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(); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -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() { | ||
|
@@ -45,4 +47,12 @@ public int getPhoto() { | |
public void setPhoto(int photo) { | ||
this.photo = photo; | ||
} | ||
|
||
public void setInFocus(boolean focus) { | ||
this.inFocus = focus; | ||
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. Тут наверное стоит сделать название поля класса тоже |
||
} | ||
|
||
public boolean getInFocus() { | ||
return this.inFocus; | ||
} | ||
} |
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.
Лучше DEFAULT_PREV
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.
Почему вместо id фото стоит цвет?