first commit
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
package com.example.saferoute.contacts;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.example.saferoute.R;
|
||||
import com.example.saferoute.SettingActivity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ContactActivity extends AppCompatActivity {
|
||||
|
||||
private ListView listView;
|
||||
private EditText inputName;
|
||||
private EditText inputNumber;
|
||||
private Button btnAdd;
|
||||
|
||||
private ContactAdapter adapter;
|
||||
private List<Contact> contacts = new ArrayList<>();
|
||||
private ContactStorage contactStorage;
|
||||
|
||||
// private SharedPreferences sharedPreferences;
|
||||
// private Set<String> names = new HashSet<>();
|
||||
// private String SAVED_NUMBERS = "SAVED_NUMBERS";
|
||||
|
||||
public void getBack2(View view){
|
||||
Intent intent = new Intent(ContactActivity.this, SettingActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
@SuppressLint("MissingInflatedId")
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_contact);
|
||||
|
||||
|
||||
// Инициализация View
|
||||
listView = findViewById(R.id.list_contact);
|
||||
inputName = findViewById(R.id.name1);
|
||||
inputNumber = findViewById(R.id.number1);
|
||||
btnAdd = findViewById(R.id.add_contact);
|
||||
|
||||
contactStorage = new ContactStorage(this);
|
||||
|
||||
// Загружаем контакты из памяти
|
||||
contacts = contactStorage.loadContacts();
|
||||
|
||||
// Если контактов нет, добавим тестовые
|
||||
if (contacts.isEmpty()) {
|
||||
// addTestContacts();
|
||||
}
|
||||
|
||||
// Настраиваем адаптер
|
||||
adapter = new ContactAdapter(this, contacts);
|
||||
listView.setAdapter(adapter);
|
||||
|
||||
|
||||
btnAdd.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
addContact();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Обработка нажатия на элемент списка
|
||||
listView.setOnItemClickListener((parent, view, position, id) -> {
|
||||
showDeleteDialog(position);
|
||||
});
|
||||
}
|
||||
|
||||
private void addContact() {
|
||||
String name = inputName.getText().toString().trim();
|
||||
String phone = inputNumber.getText().toString().trim();
|
||||
|
||||
// Валидация полей
|
||||
if (TextUtils.isEmpty(name)) {
|
||||
inputName.setError("Введите имя");
|
||||
inputName.requestFocus();
|
||||
return;
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(phone)) {
|
||||
inputNumber.setError("Введите номер телефона");
|
||||
inputNumber.requestFocus();
|
||||
return;
|
||||
}
|
||||
|
||||
// Создаем новый контакт
|
||||
Contact newContact = new Contact(name, phone);
|
||||
|
||||
// Добавляем в список
|
||||
contacts.add(newContact);
|
||||
|
||||
// Сохраняем в SharedPreferences
|
||||
contactStorage.saveContacts(contacts);
|
||||
|
||||
// Обновляем ListView
|
||||
adapter.notifyDataSetChanged();
|
||||
|
||||
// Очищаем поля ввода
|
||||
inputName.setText("");
|
||||
inputNumber.setText("");
|
||||
|
||||
// Прокручиваем ListView к последнему элементу
|
||||
listView.smoothScrollToPosition(contacts.size() - 1);
|
||||
|
||||
Toast.makeText(this, "Контакт добавлен: " + name, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private void showDeleteDialog(int position) {
|
||||
Contact contact = contacts.get(position);
|
||||
|
||||
new AlertDialog.Builder(this)
|
||||
.setTitle("Удаление контакта")
|
||||
.setMessage("Вы уверены, что хотите удалить контакт \"" + contact.getName() + "\"?")
|
||||
.setPositiveButton("ДА", (dialog, which) -> {
|
||||
deleteContact(position);
|
||||
})
|
||||
.setNegativeButton("НЕТ", (dialog, which) -> {
|
||||
dialog.dismiss();
|
||||
})
|
||||
.show();
|
||||
}
|
||||
|
||||
private void deleteContact(int position) {
|
||||
// Удаляем из списка
|
||||
contacts.remove(position);
|
||||
|
||||
// Сохраняем в памяти (SharedPreferences)
|
||||
contactStorage.saveContacts(contacts);
|
||||
|
||||
// Обновляем ListView
|
||||
adapter.notifyDataSetChanged();
|
||||
|
||||
Toast.makeText(this, "Контакт удален", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private void addTestContacts() {
|
||||
contacts.add(new Contact("Мама", "+79115716167"));
|
||||
contactStorage.saveContacts(contacts);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user