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

ДЗ_2_Сагадеева Светлана #22

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
40 changes: 23 additions & 17 deletions app/src/main/java/ru/ok/technopolis/students/AdditionActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.Random;

public class AdditionActivity extends AppCompatActivity implements View.OnClickListener {
TextView secondName;
TextView firstName;
ImageView photo;
RadioButton isFemale;
RadioButton isMale;
Button saver;
private TextView secondName;
private TextView firstName;
private ImageView photo;
private RadioButton isFemale;
Copy link

Choose a reason for hiding this comment

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

  1. Зачем тебе это радиобаттон, если нигде не используешь? isFemale
  2. photo, saver, imageNames могу быть локальными переменными

private RadioButton isMale;
private Button saver;

private String[] image_names;
private String[] imageNames;
int pic_id;

@Override
Expand All @@ -33,29 +34,34 @@ protected void onCreate(Bundle savedInstanceState) {
this.setTitle("Addition");
setContentView(R.layout.student_addition);
Random random = new Random();
Intent data = getIntent();
secondName = (EditText) findViewById(R.id.student_add__et_second_name);
firstName = (EditText) findViewById(R.id.student_add__et_first_name);
image_names = getResources().getStringArray(R.array.image_names);
imageNames = getResources().getStringArray(R.array.image_names);
photo = findViewById(R.id.student_add__iv_photo);
isFemale = findViewById(R.id.student_add__rb_female);
isMale = findViewById(R.id.student_add__rb_male);
saver = findViewById(R.id.student_add__b_save);
pic_id = getResources().getIdentifier(image_names[random.nextInt(5)], "drawable", getPackageName());
pic_id = getResources().getIdentifier(imageNames[random.nextInt(5)], "drawable", getPackageName());
photo.setImageResource(pic_id);
saver.setOnClickListener(this);
}

@Override
public void onClick(View v) {

boolean male = isMale.isChecked();
Intent intent = new Intent();
intent.putExtra("secondName", secondName.getText().toString());
intent.putExtra("firstName", firstName.getText().toString());
intent.putExtra("sex", male);
intent.putExtra("photo", pic_id);
setResult(RESULT_OK, intent);
finish();
if ((secondName.getText().toString().trim().length() != 0) ||
Copy link

Choose a reason for hiding this comment

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

secondName.getText() может вернуть null

(firstName.getText().toString().trim().length() != 0)) {
intent.putExtra("secondName", secondName.getText().toString());
intent.putExtra("firstName", firstName.getText().toString());
intent.putExtra("sex", male);
intent.putExtra("photo", pic_id);
setResult(RESULT_OK, intent);
finish();
}
else {
setResult(RESULT_CANCELED, intent);
Toast.makeText(this, "Please fill the FirstName and the SecondName fields!", Toast.LENGTH_LONG).show();
}
}
}
52 changes: 29 additions & 23 deletions app/src/main/java/ru/ok/technopolis/students/InfoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.Random;
import android.widget.Toast;

public class InfoActivity extends AppCompatActivity implements View.OnClickListener {
TextView secondName;
TextView firstName;
ImageView photo;
RadioButton isFemale;
RadioButton isMale;
Button saver;
Button deleter;
private TextView secondName;
private TextView firstName;
private ImageView photo;
private RadioButton isFemale;
private RadioButton isMale;
private Button saver;
private Button deleter;

private String[] image_names;
private String[] imageNames;
Copy link

Choose a reason for hiding this comment

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

  1. imageNames нигде не используется
  2. photo, isFemale, isMale, saver, deleter могут быть локальными


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -31,20 +31,17 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.student_info);
secondName = (EditText) findViewById(R.id.student_info__et_second_name);
firstName = (EditText) findViewById(R.id.student_info__et_first_name);
image_names = getResources().getStringArray(R.array.image_names);
imageNames = getResources().getStringArray(R.array.image_names);
photo = findViewById(R.id.student_info__iv_photo);
isFemale = findViewById(R.id.student_info__rb_female);
isMale = findViewById(R.id.student_info__rb_male);
saver = findViewById(R.id.student_info__b_save);
deleter = findViewById(R.id.student_info__b_delete);
Intent data = getIntent();
int pic_id = data.getIntExtra("photo", 1);
// int id = getResources().getIdentifier(image_names[pic_id], "drawable", getPackageName());
photo.setImageResource(pic_id);
boolean male;
boolean female;
male = data.getBooleanExtra("male", false);
Copy link

Choose a reason for hiding this comment

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

Ключи экстра лучше определить в виде констант, что бы избежать опечаток в написании

Copy link

Choose a reason for hiding this comment

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

@gilgenbergg Все еще актуально

Copy link

Choose a reason for hiding this comment

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

Зачем тут инициализация в 2 строки? Можно сделать одной

female = !male;
secondName.setText(data.getStringExtra("second_name"));
firstName.setText(data.getStringExtra("first_name"));
if (male)
Copy link

Choose a reason for hiding this comment

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

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

Copy link

Choose a reason for hiding this comment

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

@gilgenbergg Все еще актуально

Expand All @@ -60,22 +57,31 @@ public void onClick(View v) {
Intent intent = new Intent();
switch (v.getId()) {
case R.id.student_info__b_save:
intent.putExtra("secondName", secondName.getText().toString());
intent.putExtra("firstName", firstName.getText().toString());
intent.putExtra("button_code", 1);
intent.putExtra("index", getIntent().getIntExtra("index", -1));
intent.putExtra("photo", getIntent().getIntExtra("photo", 1));
intent.putExtra("isMale", false);
// intent.putExtra("pic_id", 1);
if ((secondName.getText().toString().trim().length() != 0) &&
(firstName.getText().toString().trim().length() != 0)) {
intent.putExtra("secondName", secondName.getText().toString());
intent.putExtra("firstName", firstName.getText().toString());
intent.putExtra("button_code", 1);
intent.putExtra("index", getIntent().getIntExtra("index", -1));
intent.putExtra("photo", getIntent().getIntExtra("photo", 1));
intent.putExtra("isMale", false);
setResult(RESULT_OK, intent);
finish();
} else {
setResult(RESULT_CANCELED, intent);
Toast.makeText(this, "Please fill the FirstName and the SecondName fields!", Toast.LENGTH_LONG).show();
}
break;

case R.id.student_info__b_delete:
intent.putExtra("button_code", 2);
intent.putExtra("index", getIntent().getIntExtra("index", -1));
setResult(RESULT_OK, intent);
finish();
break;

default:
break;
}
setResult(RESULT_OK, intent);
finish();
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class MainActivity extends AppCompatActivity {
private ArrayList<Student> students;
Copy link

Choose a reason for hiding this comment

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

А нужно ли тебе здесь указывать тип? Лучше оперировать на уровне интерфейсов List students;

private StudentAdapter studentAdapter;
private String[] image_names;
private String[] imageNames;
Copy link

Choose a reason for hiding this comment

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

  1. Зачем image_names?
  2. Random стоит сделать private

Random random;

@Override
Expand All @@ -26,7 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.activity_main__rv_students);

image_names = getResources().getStringArray(R.array.image_names);
imageNames = getResources().getStringArray(R.array.image_names);
students = generateStudentsList();
studentAdapter = new StudentAdapter(this, students);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
Expand Down Expand Up @@ -77,10 +77,10 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
int pic_id = data.getIntExtra("photo", 1);
boolean isMale = data.getBooleanExtra("isMale", false);
Student student_to_change = students.get(index);
student_to_change.secondName = secondName;
student_to_change.firstName = firstName;
student_to_change.isMale = isMale;
student_to_change.photo = pic_id;
student_to_change.setSecondName(secondName);
student_to_change.setFirstName(firstName);
student_to_change.setIsMale(isMale);
student_to_change.setPhoto(pic_id);
students.set(index, student_to_change);
break;
}
Expand Down
40 changes: 36 additions & 4 deletions app/src/main/java/ru/ok/technopolis/students/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,47 @@

public class Student {

public String firstName;
public String secondName;
public boolean isMale;
public int photo;
private String firstName;
private String secondName;
private boolean isMale;
private int photo;

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

public String getSecondName() {
return secondName;
}

public String getFirstName() {
return firstName;
}

public int getPhoto() {
return photo;
}

public boolean isMale() {
return isMale;
}

public void setSecondName(String secondName) {
this.secondName = secondName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public void setPhoto(int photo) {
this.photo = photo;
}

public void setIsMale(boolean isMale) {
this.isMale = isMale;
}
}
16 changes: 8 additions & 8 deletions app/src/main/java/ru/ok/technopolis/students/StudentAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
@Override
public void onBindViewHolder(@NonNull StudentViewHolder studentViewHolder, int i) {
Student student = students.get(i);
studentViewHolder.photo.setImageResource(student.photo);
studentViewHolder.second_name.setText(students.get(i).secondName);
studentViewHolder.first_name.setText(students.get(i).firstName);
studentViewHolder.photo.setImageResource(student.getPhoto());
studentViewHolder.second_name.setText(students.get(i).getSecondName());
studentViewHolder.first_name.setText(students.get(i).getFirstName());
studentViewHolder.index = i;
studentViewHolder.pic_id = student.photo;
studentViewHolder.pic_id = student.getPhoto();
studentViewHolder.student = student;
}

Expand Down Expand Up @@ -82,10 +82,10 @@ public void onClick(View v) {

public void startInfoActivity(Student student) {
Intent intent = new Intent(context, InfoActivity.class);
intent.putExtra("second_name", student.secondName);
intent.putExtra("first_name", student.firstName);
intent.putExtra("male", student.isMale);
intent.putExtra("photo", student.photo);
intent.putExtra("second_name", student.getSecondName());
intent.putExtra("first_name", student.getFirstName());
intent.putExtra("male", student.isMale());
intent.putExtra("photo", student.getPhoto());
intent.putExtra("index", this.index);
intent.putExtra("pic_id", this.pic_id);

Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>

<!--suppress ALL -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/student.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
android:maxLines="2"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:text="Second Name"
android:text="@string/SecondName"
android:textSize="20sp"
app:fontFamily="sans-serif-medium"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -66,7 +66,7 @@
android:maxLines="2"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:text="First name"
android:text="@string/FirstName"
android:textSize="20sp"
app:fontFamily="sans-serif-medium"
app:layout_constraintBaseline_toBaselineOf="@id/student__tv_second_name"
Expand Down
12 changes: 5 additions & 7 deletions app/src/main/res/layout/student_addition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
android:id="@+id/student_add__cv_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_margin="10dp"
android:elevation="12dp"
Expand Down Expand Up @@ -45,7 +44,7 @@
android:maxLines="2"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:text="Second Name"
android:hint="@string/SecondName"
android:textSize="20sp"
app:fontFamily="sans-serif-medium" />

Expand All @@ -57,8 +56,7 @@
android:maxLines="2"
android:paddingStart="10dp"
android:paddingEnd="0dp"

android:text="First name"
android:hint="@string/FirstName"
android:textSize="20sp"
app:fontFamily="sans-serif-medium"/>

Expand All @@ -73,13 +71,13 @@
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="Ж" />
android:text="@string/F" />
<RadioButton
android:id="@+id/student_add__rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="М" />
android:text="@string/M" />
</RadioGroup>

</LinearLayout>
Expand All @@ -98,7 +96,7 @@
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="250dp"
android:text="Сохранить"
android:text="@string/Save"
/>

</LinearLayout>
Expand Down
Loading