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

Igor Lopatinskiy #23

Open
wants to merge 6 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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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'
implementation 'com.android.support:recyclerview-v7: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
130 changes: 130 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,143 @@

import android.support.v7.app.AppCompatActivity;
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.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

private StudentAdapter adapter;
private ImageView photo;
private int photoId;
private EditText firstNameField;
private EditText lastNameField;
private RadioGroup radioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RecyclerView recyclerView = findViewById(R.id.activity_main__list);
adapter = new StudentAdapter(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

photo = findViewById(R.id.activity_main__photo);
firstNameField = findViewById(R.id.activity_main__firstName);
lastNameField = findViewById(R.id.activity_main__lastName);
radioGroup = findViewById(R.id.activity_main__radio);

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
randomizePhoto();
}
});

Button addButton = findViewById(R.id.activity_main__add);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearFields();
adapter.setActiveStudent(StudentAdapter.NO_STUDENT);
}
});

Button saveButton = findViewById(R.id.activity_main__save);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveStudent();
}
});

Button deleteButton = findViewById(R.id.activity_main__delete);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteStudent();
}
});
}

private void deleteStudent() {
adapter.deleteStudent();
}

private void saveStudent() {
String firstNameStr = firstNameField.getText() == null ? "" : firstNameField.getText().toString();
String lastNameStr = lastNameField.getText() == null ? "" : lastNameField.getText().toString();
Boolean gender = null;
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.activity_main__radio_male:
gender = true;
break;
case R.id.activity_main__radio_woman:
gender = false;
break;
}

if (firstNameStr.isEmpty() || lastNameStr.isEmpty() || gender == null) {
Toast.makeText(this, "Заполните все поля", Toast.LENGTH_SHORT).show();
return;
}

Student student = new Student(
firstNameStr,
lastNameStr,
gender,
photoId
);
adapter.saveStudent(student);
}

private void randomizePhoto() {
int[] imgMale = {R.drawable.male_1, R.drawable.male_2, R.drawable.male_3};
int[] imgFemale = {R.drawable.female_1, R.drawable.female_2, R.drawable.female_3};

int i;
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.activity_main__radio_male:
i = new Random().nextInt(3);
photo.setImageDrawable(getResources().getDrawable(imgMale[i]));
photoId = imgMale[i];
break;
case R.id.activity_main__radio_woman:
i = new Random().nextInt(3);
photo.setImageDrawable(getResources().getDrawable(imgFemale[i]));
photoId = imgFemale[i];
break;
default:
photoId = imgMale[0];
break;
}
}

public void clearFields() {
photo.setImageDrawable(null);
firstNameField.setText("");
lastNameField.setText("");
radioGroup.clearCheck();
}

public void setActiveStudent(Student student) {
photo.setImageDrawable(getResources().getDrawable(student.getPhoto()));
firstNameField.setText(student.getFirstName());
lastNameField.setText(student.getSecondName());
if (student.isMale()) {
radioGroup.check(R.id.activity_main__radio_male);
} else {
radioGroup.check(R.id.activity_main__radio_woman);
}
}

}
29 changes: 23 additions & 6 deletions app/src/main/java/ru/ok/technopolis/students/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@

public class Student {

private Boolean selected;
private String firstName;
private String secondName;
private boolean maleGender;
private boolean male;
private int photo;

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

public Boolean isSelected() {
return selected;
}

public void setSelected(Boolean selected) {
this.selected = selected;
}

public String getFirstName() {
return firstName;
}
Expand All @@ -30,12 +39,12 @@ public void setSecondName(String secondName) {
this.secondName = secondName;
}

public boolean isMaleGender() {
return maleGender;
public boolean isMale() {
return male;
}

public void setMaleGender(boolean maleGender) {
this.maleGender = maleGender;
public void setMale(boolean male) {
this.male = male;
}

public int getPhoto() {
Expand All @@ -45,4 +54,12 @@ public int getPhoto() {
public void setPhoto(int photo) {
this.photo = photo;
}

public void copy(Student student) {
this.firstName = new String(student.firstName);
Copy link

Choose a reason for hiding this comment

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

Зачем тут создаются новые объекты?

this.secondName = new String(student.secondName);
this.male = new Boolean(student.male);
this.photo = new Integer(student.photo);
}

}
116 changes: 116 additions & 0 deletions app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package ru.ok.technopolis.students;

import android.content.Context;
import android.graphics.Color;
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.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentViewHolder> {

public static final int NO_STUDENT = -1;
private MainActivity context;
Copy link

Choose a reason for hiding this comment

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

Плохой паттерн передавать активити в адаптер. Для работы с активити у тебя есть лисенеры

private List<Student> students = new ArrayList<>();
private int activeStudent = NO_STUDENT;

public StudentAdapter(Context context) {
this.context = (MainActivity) context;
}

public void saveStudent(Student student) {
if (activeStudent == NO_STUDENT) {
students.add(student);
activeStudent = students.size() - 1;
student.setSelected(true);
notifyItemInserted(activeStudent);
} else {
Student oldStudent = students.get(activeStudent);
oldStudent.copy(student);
notifyItemChanged(activeStudent);
}
}

public void deleteStudent() {
if (activeStudent != NO_STUDENT) {
students.remove(activeStudent);
notifyItemRemoved(activeStudent);
activeStudent = NO_STUDENT;
}
context.clearFields();
}

public void setActiveStudent(int activeStudent) {
if (this.activeStudent != NO_STUDENT){
Student student = students.get(this.activeStudent);
student.setSelected(false);
}
this.activeStudent = activeStudent;
notifyDataSetChanged();
}

@NonNull
@Override
public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater
.from(viewGroup.getContext())
.inflate(R.layout.recycler_item, viewGroup, false);
return new StudentViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull StudentViewHolder studentViewHolder, int i) {
studentViewHolder.bind(students.get(i));
}

@Override
public int getItemCount() {
return students.size();
}

public class StudentViewHolder extends RecyclerView.ViewHolder {
private ImageView face;
private TextView fullName;
private LinearLayout mainLayout;

public StudentViewHolder(@NonNull View itemView) {
super(itemView);

mainLayout = itemView.findViewById(R.id.recycler_item__main);
face = itemView.findViewById(R.id.recycler_item__image);
fullName = itemView.findViewById(R.id.recycler_item__text);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (activeStudent != NO_STUDENT) {
Student oldStudent = students.get(activeStudent);
oldStudent.setSelected(false);
}
activeStudent = getAdapterPosition();
Student student = students.get(getAdapterPosition());
student.setSelected(true);
context.setActiveStudent(student);
notifyDataSetChanged();
}
});
}

private void bind(Student student) {
if (student.isSelected())
Copy link

Choose a reason for hiding this comment

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

Лучше всегда ставить фигурные скобки

mainLayout.setBackgroundColor(Color.BLUE);
else
mainLayout.setBackgroundColor(Color.DKGRAY);
face.setImageDrawable(context.getResources().getDrawable(student.getPhoto()));
fullName.setText(student.getFirstName() + " " + student.getSecondName());
}
}

}
Loading