Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Comments

Homework7#7

Open
AndreiGrek wants to merge 4 commits intomasterfrom
Homework7
Open

Homework7#7
AndreiGrek wants to merge 4 commits intomasterfrom
Homework7

Conversation

@AndreiGrek
Copy link
Owner

No description provided.

editText2.setHint("E-mail");
Toast.makeText(AddContact.this, "Режим ввода E-mail", Toast.LENGTH_SHORT).show();
} else if (isChecked== false){
editText2.setHint("+375(29) XXX-XX-XX");

Choose a reason for hiding this comment

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

лучше выносить текст в strings.xml

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
editText2.setHint("E-mail");
Toast.makeText(AddContact.this, "Режим ввода E-mail", Toast.LENGTH_SHORT).show();

Choose a reason for hiding this comment

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

лучше выносить текст в strings.xml

Toast.makeText(AddContact.this, "Режим ввода E-mail", Toast.LENGTH_SHORT).show();
} else if (isChecked== false){
editText2.setHint("+375(29) XXX-XX-XX");
Toast.makeText(AddContact.this, "Режим ввода телефона", Toast.LENGTH_SHORT).show();

Choose a reason for hiding this comment

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

лучше выносить текст в strings.xml

Comment on lines 26 to 27
AppDatabase db;
SharedPreferences sPref;

Choose a reason for hiding this comment

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

нет модификаторов доступа

AppDatabase db;
SharedPreferences sPref;
private int counter = 0;
private static List<Item> itemList;

Choose a reason for hiding this comment

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

этот лист явне не должен быть статическим

Comment on lines 66 to 81
if (requestCode == 1000 && resultCode == Activity.RESULT_OK && data != null) {
loadCounter();
Toast.makeText(MainActivity.this, "Новый контакт добавлен", Toast.LENGTH_SHORT).show();
itemList.add(counter, new Item(counter, data.getStringExtra("SAVEADDNAME"), data.getStringExtra("SAVEADDNUMBER")));
db.contactDao().insert(new Item(counter, data.getStringExtra("SAVEADDNAME"), data.getStringExtra("SAVEADDNUMBER")));
counter++;
saveCounter(counter);
itemListAdapter.notifyDataSetChanged();

} else if (requestCode == 2000 && resultCode == Activity.RESULT_OK && data != null) {
Toast.makeText(MainActivity.this, "Контакт изменен", Toast.LENGTH_SHORT).show();
position = data.getIntExtra("POSITION", 0);
itemList.set(position, new Item(position, data.getStringExtra("SAVEEDITNAME"), data.getStringExtra("SAVEEDITNUMBER")));
db.contactDao().update(new Item(position, data.getStringExtra("SAVEEDITNAME"), data.getStringExtra("SAVEEDITNUMBER")));
itemListAdapter.notifyDataSetChanged();
}

Choose a reason for hiding this comment

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

Я конечно понимаю, что так намного проще. Но лучше конечно делать все необходимые операции в соответствующей активити. Тем самым ты уменьшаешь связанность компонентов твоей системы.

Toast.makeText(AddContact.this, R.string.addNewContact, Toast.LENGTH_SHORT).show();
counter ++;
saveCounter(counter);
startActivity(intentSave);

Choose a reason for hiding this comment

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

Так а вот это тут лишнее. Не вижу смысла запускать новую активити. тебе по сути надо просто финишировать текущую

private Switch switch1;
private boolean ifChecked;
private AppDatabase db;
private int counter;

Choose a reason for hiding this comment

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

ты можешь отказаться от каунтера и просто сделать автоинкрементное поле для id или генерить слачайную строку

https://www.baeldung.com/kotlin-random-alphanumeric-string

findViewById(R.id.buttonAdd).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentSave = new Intent(AddContact.this, MainActivity.class);

Choose a reason for hiding this comment

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

по сути интент тут вообще не нужен

Comment on lines +65 to +76
void saveCounter(int counter) {
sPref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor ed = sPref.edit();
ed.putInt("COUNTER", counter);
ed.apply();
}

public int loadCounter() {
sPref = getPreferences(MODE_PRIVATE);
this.counter = sPref.getInt("COUNTER", 0);
return counter;
}

Choose a reason for hiding this comment

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

Если использовать автоинкремент, то эти операции сами собой отпадают

loadCounter();
db.contactDao().insert(new Item(counter, name, phone));
Toast.makeText(AddContact.this, R.string.addNewContact, Toast.LENGTH_SHORT).show();
counter ++;

Choose a reason for hiding this comment

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

Если использовать автоинкремент, то эти операции сами собой отпадают

final EditText editName = findViewById(R.id.editName);
final EditText editData = findViewById(R.id.editPhone);
Intent intentPosition = getIntent();
position = intentPosition.getIntExtra("POSITION", 0);

Choose a reason for hiding this comment

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

лучше передавать сюда не позицию, а id твоего контакта. Так более правильно, потому что ты по нему потом сможешь получить контакт из базы

data = editData.getText().toString();
db.contactDao().update(new Item(position, name, data));
Toast.makeText(EditContact.this, R.string.editContact, Toast.LENGTH_SHORT).show();
startActivity(intentSaved);

Choose a reason for hiding this comment

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

Так а вот это тут лишнее. Не вижу смысла запускать новую активити. тебе по сути надо просто финишировать текущую

findViewById(R.id.buttonSave).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentSaved = new Intent(EditContact.this, MainActivity.class);

Choose a reason for hiding this comment

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

по сути интент тут вообще не нужен

Intent intentSaved = new Intent(EditContact.this, MainActivity.class);
name = editName.getText().toString();
data = editData.getText().toString();
db.contactDao().update(new Item(position, name, data));

Choose a reason for hiding this comment

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

Лично я бы загружал бы контакт из базы в onCreate по id, потом тут обновлял бы его, и передавал бы в базу


@Override
protected void onResume() {
itemList = db.contactDao().getAll();

Choose a reason for hiding this comment

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

тут еще надо обновлять данные в адаптере

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants