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
Version 1.0 Loginov #1
Open
l0gark
wants to merge
9
commits into
polis-vk:master
Choose a base branch
from
l0gark: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 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d8943c8
Students_Homework Логинов Аркадий
l0gark ee11d43
Update Student.java
l0gark bed07f7
Bitmap не тормозит, добавил редактирование
l0gark 323129c
remove .idea
l0gark bb0076e
Исправил замечания.
l0gark 14800fd
Исправил замечания.
l0gark 4145fc0
Исправил. Так и не понимаю как мне без контекста этому литсенеру пере…
l0gark f10ec72
убрал лишние импорты
l0gark f0f59dd
поставил setter для листенеров
l0gark 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
143 changes: 142 additions & 1 deletion
143
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,155 @@ | ||
package ru.ok.technopolis.students; | ||
|
||
import android.app.AlertDialog; | ||
import android.content.DialogInterface; | ||
import android.graphics.Bitmap; | ||
import android.support.design.widget.FloatingActionButton; | ||
import android.support.v4.app.DialogFragment; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.support.v7.widget.GridLayoutManager; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.widget.ImageView; | ||
import android.widget.LinearLayout; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
import java.util.ArrayList; | ||
import java.util.Random; | ||
|
||
public class MainActivity extends AppCompatActivity implements StudentAdderDialog.OnAdderStudentListener, StudentAdderDialog.OnUpdateStudentListener { | ||
|
||
private String[] mansFirstNames; | ||
private String[] womenFirstNames; | ||
private String[] lastNames; | ||
private int[] manPhotos; | ||
private int[] womanPhotos; | ||
private Random random; | ||
private int manPhotoIndex, womanPhotoIndex; | ||
private ArrayList<Student> students; | ||
private StudentsAdapter adapter; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
random = new Random(); | ||
manPhotoIndex = womanPhotoIndex = 0; | ||
mansFirstNames = getResources().getStringArray(R.array.mans_first_names); | ||
womenFirstNames = getResources().getStringArray(R.array.women_first_names); | ||
lastNames = getResources().getStringArray(R.array.last_names); | ||
manPhotos = new int[]{R.drawable.male_01, R.drawable.male_02, R.drawable.male_03}; | ||
womanPhotos = new int[]{R.drawable.female_01, R.drawable.female_02, R.drawable.female_03}; | ||
|
||
|
||
RecyclerView recyclerView = findViewById(R.id.activity_main__recycler_view); | ||
FloatingActionButton fabAdd = findViewById(R.id.activity_main__fab_add); | ||
|
||
fabAdd.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
DialogFragment dialog = new StudentAdderDialog(); | ||
dialog.show(getSupportFragmentManager(), "DialogAdder"); | ||
l0gark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}); | ||
|
||
students = createStudents(); | ||
adapter = new StudentsAdapter(students, new StudentsAdapter.OnStudentClickListener() { | ||
@Override | ||
public void onStudentClick(Student student) { | ||
showDialog(student); | ||
} | ||
}); | ||
recyclerView.setAdapter(adapter); | ||
LinearLayoutManager mLayoutManager = new GridLayoutManager(this, 2); | ||
recyclerView.setLayoutManager(mLayoutManager); | ||
} | ||
|
||
private void showDialog(final Student student) { | ||
ImageView view = new ImageView(this); | ||
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300); | ||
view.setScaleType(ImageView.ScaleType.CENTER_CROP); | ||
view.setLayoutParams(layoutParams); | ||
|
||
l0gark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (student.getBitmap() == null) { | ||
view.setImageResource(student.getPhoto()); | ||
} else { | ||
view.setImageBitmap(student.getBitmap()); | ||
} | ||
|
||
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); | ||
|
||
builder.setView(view); | ||
builder.setTitle(student.getFirstName() + " " + student.getSecondName()); | ||
builder.setMessage(student.isMaleGender() ? R.string.guy : R.string.girl); | ||
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface dialogInterface, int i) { | ||
dialogInterface.cancel(); | ||
} | ||
}); | ||
|
||
builder.setNegativeButton(R.string.edit, new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface dialogInterface, int i) { | ||
DialogFragment dialog = new StudentAdderDialog(); | ||
Bundle bundle = new Bundle(); | ||
bundle.putSerializable(StudentAdderDialog.STUDENT_TAG, student); | ||
dialog.setArguments(bundle); | ||
dialog.show(getSupportFragmentManager(), "DialogAdder"); | ||
dialogInterface.cancel(); | ||
} | ||
}); | ||
|
||
builder.setNeutralButton(R.string.delete, new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface dialogInterface, int i) { | ||
students.remove(student); | ||
adapter.notifyDataSetChanged(); | ||
dialogInterface.cancel(); | ||
} | ||
}); | ||
builder.create().show(); | ||
} | ||
|
||
|
||
ArrayList<Student> createStudents() { | ||
ArrayList<Student> students = new ArrayList<>(); | ||
for (int i = 0; i < 20; i++) { | ||
students.add(nextStudent(i % 2 == 0)); | ||
} | ||
return students; | ||
} | ||
|
||
|
||
Student nextStudent(boolean maleGender) { | ||
if (maleGender) { | ||
int nameIndex = random.nextInt(mansFirstNames.length); | ||
int lastNameIndex = random.nextInt(mansFirstNames.length); | ||
return new Student(mansFirstNames[nameIndex], lastNames[lastNameIndex], true, manPhotos[manPhotoIndex++ % 3]); | ||
} | ||
int nameIndex = random.nextInt(womenFirstNames.length); | ||
int lastNameIndex = random.nextInt(womenFirstNames.length); | ||
return new Student(womenFirstNames[nameIndex], lastNames[lastNameIndex] + "a", false, womanPhotos[womanPhotoIndex++ % 3]); | ||
} | ||
|
||
|
||
@Override | ||
public void add(String firstName, String lastName, boolean maleGender, Bitmap bitmap) { | ||
Student student = new Student(firstName, lastName, maleGender, maleGender ? manPhotos[manPhotoIndex++ % 3] : womanPhotos[womanPhotoIndex++ % 3]); | ||
if (bitmap != null) { | ||
student.setBitmap(bitmap); | ||
} | ||
students.add(student); | ||
adapter.notifyDataSetChanged(); | ||
} | ||
|
||
@Override | ||
public void update(Student oldStudent, Student newStudent) { | ||
int index = students.indexOf(oldStudent); | ||
students.remove(oldStudent); | ||
students.add(index, newStudent); | ||
adapter.notifyDataSetChanged(); | ||
} | ||
} |
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,11 +1,16 @@ | ||
package ru.ok.technopolis.students; | ||
|
||
public class Student { | ||
import android.graphics.Bitmap; | ||
|
||
import java.io.Serializable; | ||
|
||
public class Student implements Serializable { | ||
|
||
private String firstName; | ||
private String secondName; | ||
private boolean maleGender; | ||
private int photo; | ||
private Bitmap bitmap; | ||
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 Student(String firstName, String secondName, boolean maleGender, int photo) { | ||
this.firstName = firstName; | ||
|
@@ -14,6 +19,7 @@ public Student(String firstName, String secondName, boolean maleGender, int phot | |
this.photo = photo; | ||
} | ||
|
||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
@@ -42,7 +48,22 @@ public int getPhoto() { | |
return photo; | ||
} | ||
|
||
public void setBitmap(Bitmap bitmap) { | ||
this.bitmap = bitmap; | ||
} | ||
|
||
public Bitmap getBitmap() { | ||
return bitmap; | ||
} | ||
|
||
public void setPhoto(int photo) { | ||
this.photo = photo; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return firstName + " " + secondName + (isMaleGender() ? " М" : " Ж"); | ||
} | ||
|
||
|
||
} |
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.
А если у модели Student было бы 50 полей? Лучше хранить список студентов
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.
Это решил не исправлять?