Coders_Lab
Spróbuj tak:
"id INTEGER PRIMARY KEY,"
Przetestowałem tworzenie tabeli i dodawanie rekordu za pomocą takiego skryptu:
DROP Table people;
CREATE TABLE IF NOT EXISTS people (
id INTEGER PRIMARY KEY,
first_name text NOT NULL,
last_name text NOT NULL
);
INSERT INTO people (
first_name,
last_name
)
VALUES
(
'Johnathan',
'Smith'
);
SELECT * from people;
Na tej stronie: http://www.sqlitetutorial.net/tryit/query/sqlite-autoincrement/#4
Jak id było tworzone w ten sposób:
id INTEGER NOT NULL CONSTRAINT contacts_pk PRIMARY KEY AUTOINCREMENT
To dostałem taki sam błąd jak Ty 😉
Dzięki ! To faktycznie pomogło.
Mógłbyś jeszcze zerknąć w mój kod i podpowiedzieć co mam źle (bo może jestem ślepy i nie dostrzegam, albo między moją apką a tutorialową jest znowu jakaś rozbieżność) w kwestii update i delete?
Jeżeli chodzi o delete to notification działa, ale wykonywanie 'Tak' już niestety nie działa.
W update podkreśla przy WHERE "<columnname> excepted, got WHERE"
Wrzucam niżej wszystkie kody:
XML:
activity_main.xml:
Spoiler
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="Dodaj nowy kontakt"
android:textAlignment="center"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textSize="30dp" />
<EditText
android:id="@+id/ETname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Imie"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/ETsurname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Nazwisko"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/ETcity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Miasto"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/ETstreet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Ulica"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/EThomeNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Numer domu"
android:inputType="number|textPersonName" />
<EditText
android:id="@+id/EThouseNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Numer mieszkania"
android:inputType="number|textPersonName" />
<EditText
android:id="@+id/ETphoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Numer telefonu"
android:inputType="textPersonName|phone" />
<EditText
android:id="@+id/ETemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="E-mail"
android:inputType="textEmailSubject" />
<EditText
android:id="@+id/ETdateOfBirth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Data urodzenia"
android:inputType="date" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Wybierz grupę do której przypisać kontakt"
android:textAlignment="center"
android:layout_marginTop="20dp"
android:textSize="20dp" />
<Spinner
android:id="@+id/SPgroupContacts"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:entries="@array/groups"
android:layout_height="wrap_content" />
<Button
android:id="@+id/BTNaddContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dodaj kontakt"
android:layout_marginTop="10dp"
android:textSize="20dp" />
<TextView
android:id="@+id/TVlistContacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lista kontaktow"
android:textAlignment="center"
android:layout_marginTop="5dp"
android:textSize="18dp" />
</LinearLayout>
</ScrollView>
activity_contacts.xml:
Spoiler
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContactsActivity">
<ListView
android:id="@+id/listViewContacts"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
list_layout_contacts.xml
Spoiler
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonEditContacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edytuj" />
<Button
android:id="@+id/buttonDeleteContacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Usun" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/TVname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="10dp"
android:text="Maksymilian"
android:textAlignment="textEnd"
android:textColor="#292929"
android:textSize="25dp" />
<TextView
android:id="@+id/TVsurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="Wojciechowski"
android:textAlignment="textStart"
android:textColor="#292929"
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/TVgroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rodzina"
android:textAlignment="center"
android:textSize="20dp" />
<TextView
android:id="@+id/TVdateOfBirth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="23/01/1986"
android:textAlignment="center" />
<TextView
android:id="@+id/TVcity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aleksandrów Łódzki"
android:textAlignment="center"
android:textSize="20dp" />
<TextView
android:id="@+id/textView16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ulica / Numer domu / Numer mieszkania"
android:textAlignment="center"
android:textSize="17dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp">
<TextView
android:id="@+id/TVstreet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text="Leszzczynskiego"
android:textAlignment="center"
android:textSize="15dp" />
<TextView
android:id="@+id/TVhomeNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="25"
android:textAlignment="center"
android:textSize="20dp" />
<TextView
android:id="@+id/TVhouseNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:textAlignment="center"
android:textSize="20dp" />
</LinearLayout>
<TextView
android:id="@+id/TVphoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="665556132"
android:textAlignment="center"
android:textSize="20dp" />
<TextView
android:id="@+id/TVemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="maks.wojciechowski@gmai.com"
android:textAlignment="center"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
dialog_update_contacts.xml
Spoiler
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="Zaktualizuj kontakt"
android:textAlignment="center"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textSize="30dp" />
<EditText
android:id="@+id/ETname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Imie"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/Surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Nazwisko"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/ETcity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Miasto"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/ETstreet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Ulica"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/EThomeNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Numer domu"
android:inputType="number|textPersonName" />
<EditText
android:id="@+id/EThouseNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Numer mieszkania"
android:inputType="number|textPersonName" />
<EditText
android:id="@+id/ETphoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Numer telefonu"
android:inputType="textPersonName|phone" />
<EditText
android:id="@+id/ETemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="E-mail"
android:inputType="textEmailSubject" />
<EditText
android:id="@+id/ETdateOfBirth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Data urodzenia"
android:inputType="date" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Wybierz grupę do której przypisać kontakt"
android:textAlignment="center"
android:layout_marginTop="20dp"
android:textSize="20dp" />
<Spinner
android:id="@+id/SPgroupContacts"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:entries="@array/groups"
android:layout_height="wrap_content" />
<Button
android:id="@+id/updateContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Zaktualizuj"
android:layout_marginTop="10dp"
android:textSize="20dp" />
</LinearLayout>
</ScrollView>
Java Class:
MainActivity.java:
Spoiler
package com.example.contactapp;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String DATABASE_NAME = "mycontacsdatabase";
EditText ETname, ETsurname, ETcity, ETstreet, EThomeNumber, EThouseNumber, ETphoneNumber, ETemail, ETdateOfBirth;
TextView TVlistContacts;
Spinner SPgroupContacts;
SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ETname = (EditText) findViewById(R.id.ETname);
ETsurname = (EditText) findViewById(R.id.ETsurname);
ETcity = (EditText) findViewById(R.id.ETcity);
ETstreet = (EditText) findViewById(R.id.ETstreet);
EThomeNumber = (EditText) findViewById(R.id.EThomeNumber);
EThouseNumber = (EditText) findViewById(R.id.EThouseNumber);
ETphoneNumber = (EditText) findViewById(R.id.ETphoneNumber);
ETemail = (EditText) findViewById(R.id.ETemail);
ETdateOfBirth = (EditText) findViewById(R.id.ETdateOfBirth);
TVlistContacts = (TextView) findViewById(R.id.TVlistContacts);
SPgroupContacts = (Spinner) findViewById(R.id.SPgroupContacts);
findViewById(R.id.BTNaddContact).setOnClickListener(this);
TVlistContacts.setOnClickListener(this);
mDatabase = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
createContactsTable();
}
private void createContactsTable() {
mDatabase.execSQL(
"CREATE TABLE IF NOT EXISTS contacts (\n" +
" id INTEGER PRIMARY KEY,\n" +
" namee varchar(200) NOT NULL,\n" +
" surnamee varchar(200) NOT NULL,\n" +
" cityy varchar(200) NOT NULL,\n" +
" streett varchar(200) NOT NULL,\n" +
" homeNumberr varchar(200) NOT NULL,\n" +
" houseNumberr varchar(200) NOT NULL,\n" +
" phoneNumberr varchar(200) NOT NULL,\n" +
" emaill varchar(200) NOT NULL,\n" +
" dateOfBirthh varchar(200) NOT NULL,\n" +
" groupp varchar(200) NOT NULL\n" +
");"
);
}
private boolean inputsAreCorrect(String namee, String surnamee, String cityy, String streett, String homeNumberr, String houseNumberr, String phoneNumberr, String emaill, String dateOfBirthh) {
if (namee.isEmpty()) {
ETname.setError("Wpisz imie");
ETname.requestFocus();
return false;
}
if (surnamee.isEmpty()) {
ETsurname.setError("Wpisz naziwsko");
ETsurname.requestFocus();
return false;
}
if (cityy.isEmpty()) {
ETcity.setError("Wpisz miasto");
ETcity.requestFocus();
return false;
}
if (streett.isEmpty()) {
ETstreet.setError("Wpisz ulice");
ETstreet.requestFocus();
return false;
}
if (homeNumberr.isEmpty()) {
EThomeNumber.setError("Wpisz numer domu");
EThomeNumber.requestFocus();
return false;
}
if (houseNumberr.isEmpty()) {
EThouseNumber.setError("Wpisz numer mieszkania");
EThomeNumber.requestFocus();
return false;
}
if (phoneNumberr.isEmpty()) {
ETphoneNumber.setError("Wpisz numer telefonu");
ETphoneNumber.requestFocus();
return false;
}
if (emaill.isEmpty()) {
ETemail.setError("Wpisz email");
ETemail.requestFocus();
return false;
}
if (dateOfBirthh.isEmpty()) {
ETdateOfBirth.setError("Wpisz date urodzenia");
ETdateOfBirth.requestFocus();
return false;
}
return true;
}
private void addContact() {
String namee = ETname.getText().toString().trim();
String surnamee = ETsurname.getText().toString().trim();
String cityy = ETcity.getText().toString().trim();
String streett = ETstreet.getText().toString().trim();
String homeNumberr = EThomeNumber.getText().toString().trim();
String houseNumberr = EThouseNumber.getText().toString().trim();
String phoneNumberr = ETphoneNumber.getText().toString().trim();
String emaill = ETemail.getText().toString().trim();
String dateOfBirthh = ETdateOfBirth.getText().toString().trim();
String groupp = SPgroupContacts.getSelectedItem().toString();
if (inputsAreCorrect(namee, surnamee, cityy, streett, homeNumberr, houseNumberr, phoneNumberr, emaill, dateOfBirthh)) {
String insertSQL = "INSERT INTO contacts \n" +
"(namee, surnamee, cityy, streett, homeNumberr, houseNumberr, phoneNumberr,emaill,dateOfBirthh, groupp)\n" +
"VALUES \n" +
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
mDatabase.execSQL(insertSQL, new String[]{namee, surnamee, cityy, streett, homeNumberr, houseNumberr, phoneNumberr, emaill, dateOfBirthh, groupp});
Toast.makeText(this, "Dodano kontakt!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.BTNaddContact:
addContact();
break;
case R.id.TVlistContacts:
startActivity(new Intent(this, ContactsActivity.class));
break;
}
}
}
Contacts.java:
Spoiler
package com.example.contactapp;
public class Contacts {
int id;
String namee, surnamee, cityy, streett, homeNumberr, houseNumberr, phoneNumberr,emaill,dateOfBirthh, groupp;
public Contacts(int id, String namee, String surnamee, String cityy, String streett, String homeNumberr, String houseNumberr, String phoneNumberr, String emaill, String dateOfBirthh, String groupp){
this.id = id;
this.namee = namee;
this.surnamee = surnamee;
this.cityy = cityy;
this.streett = streett;
this.homeNumberr = homeNumberr;
this.houseNumberr = houseNumberr;
this.phoneNumberr = phoneNumberr;
this.emaill = emaill;
this.dateOfBirthh = dateOfBirthh;
this.groupp = groupp;
}
public int getId() {
return id;
}
public String getNamee() {
return namee;
}
public String getSurnamee() {
return surnamee;
}
public String getCityy() {
return cityy;
}
public String getStreett() {
return streett;
}
public String getHomeNumberr() {
return homeNumberr;
}
public String getHouseNumberr() {
return houseNumberr;
}
public String getPhoneNumberr() {
return phoneNumberr;
}
public String getEmaill() {
return emaill;
}
public String getDateOfBirthh() {
return dateOfBirthh;
}
public String getGroupp() {
return groupp;
}
}
ContactsAcvivity.java:
Spoiler
package com.example.contactapp;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class ContactsActivity extends AppCompatActivity {
List<Contacts> contactsList;
SQLiteDatabase mDatabase;
ListView listViewContacts;
ContactsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
listViewContacts = (ListView) findViewById(R.id.listViewContacts);
contactsList = new ArrayList<>();
mDatabase = openOrCreateDatabase(MainActivity.DATABASE_NAME, MODE_PRIVATE, null);
showContactsFromDatabase();
}
private void showContactsFromDatabase() {
Cursor cursorContacts = mDatabase.rawQuery("SELECT * FROM contacts", null);
if (cursorContacts.moveToFirst()) {
do {
contactsList.add(new Contacts(
cursorContacts.getInt(0),
cursorContacts.getString(1),
cursorContacts.getString(2),
cursorContacts.getString(3),
cursorContacts.getString(4),
cursorContacts.getString(5),
cursorContacts.getString(6),
cursorContacts.getString(7),
cursorContacts.getString(8),
cursorContacts.getString(9),
cursorContacts.getString(10)
));
} while (cursorContacts.moveToNext());
}
cursorContacts.close();
adapter = new ContactsAdapter(this,R.layout.list_layout_contacts,contactsList);
listViewContacts.setAdapter(adapter);
}
}
i tu gdzie występuje problem, czyli ContactsAdapter.java:
Spoiler
package com.example.contactapp;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class ContactsAdapter extends ArrayAdapter<Contacts> {
Context mCtx;
int listLayoutRes;
List<Contacts> contactsList;
SQLiteDatabase mDatabase;
public ContactsAdapter(Context mCtx, int listLayoutRes, List<Contacts> contactsList) {
super(mCtx, listLayoutRes, contactsList);
this.mCtx = mCtx;
this.listLayoutRes = listLayoutRes;
this.contactsList = contactsList;
this.mDatabase = mDatabase;
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(listLayoutRes, null);
final Contacts contacts = contactsList.get(position);
TextView TVname = view.findViewById(R.id.TVname);
TextView TVsurname = view.findViewById(R.id.TVsurname);
TextView TVgroup = view.findViewById(R.id.TVgroup);
TextView TVdateOfBirth = view.findViewById(R.id.TVdateOfBirth);
TextView TVcity = view.findViewById(R.id.TVcity);
TextView TVstreet = view.findViewById(R.id.TVstreet);
TextView TVhomeNumber = view.findViewById(R.id.TVhomeNumber);
TextView TVhouseNumber = view.findViewById(R.id.TVhouseNumber);
TextView TVphoneNumber = view.findViewById(R.id.TVphoneNumber);
TextView TVemail = view.findViewById(R.id.TVemail);
TVname.setText(contacts.getNamee());
TVsurname.setText(contacts.getSurnamee());
TVgroup.setText(contacts.getGroupp());
TVdateOfBirth.setText(contacts.getDateOfBirthh());
TVcity.setText(contacts.getCityy());
TVstreet.setText(contacts.getStreett());
TVhomeNumber.setText(contacts.getHomeNumberr());
TVhouseNumber.setText(contacts.getHouseNumberr());
TVphoneNumber.setText(contacts.getPhoneNumberr());
TVemail.setText(contacts.getEmaill());
Button buttonDelete = view.findViewById(R.id.buttonDeleteContacts);
Button buttonEdit = view.findViewById(R.id.buttonEditContacts);
buttonEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateContacts(contacts);
}
});
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(mCtx);
builder.setTitle("Jestes pewny, że chcesz usunąć ten kontakt?");
builder.setPositiveButton("Tak", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
String sql = "DELETE FROM contacts WHERE id = ?";
mDatabase.execSQL(sql, new Integer[]{contacts.getId()});
reloadContactsFromDatabase();
}
});
builder.setNegativeButton("Nie", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
return view;
}
public void updateContacts(final Contacts contacts) {
final AlertDialog.Builder builder = new AlertDialog.Builder(mCtx);
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.dialog_update_contacts, null);
builder.setView(view);
final EditText ETname = view.findViewById(R.id.ETname);
final EditText ETsurname = view.findViewById(R.id.ETsurname);
final EditText ETcity = view.findViewById(R.id.ETcity);
final EditText ETstreet = view.findViewById(R.id.ETstreet);
final EditText EThomeNumber = view.findViewById(R.id.EThomeNumber);
final EditText EThouseNumber = view.findViewById(R.id.EThouseNumber);
final EditText ETphoneNumber = view.findViewById(R.id.ETphoneNumber);
final EditText ETemail = view.findViewById(R.id.ETemail);
final EditText ETdateOfBirth = view.findViewById(R.id.ETdateOfBirth);
final Spinner SPgroupContacts = view.findViewById(R.id.SPgroupContacts);
ETname.setText(contacts.getNamee());
ETsurname.setText(contacts.getSurnamee());
ETcity.setText(contacts.getCityy());
ETstreet.setText(contacts.getStreett());
EThomeNumber.setText(contacts.getHomeNumberr());
EThouseNumber.setText(contacts.getHouseNumberr());
ETphoneNumber.setText(contacts.getPhoneNumberr());
ETemail.setText(contacts.getEmaill());
ETdateOfBirth.setText(contacts.getDateOfBirthh());
final AlertDialog dialog = builder.create();
dialog.show();
view.findViewById(R.id.updateContact).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String namee = ETname.getText().toString().trim();
String surnamee = ETsurname.getText().toString().trim();
String cityy = ETcity.getText().toString().trim();
String streett = ETstreet.getText().toString().trim();
String phoneNumberr = ETphoneNumber.getText().toString().trim();
String houseNumberr = EThouseNumber.getText().toString().trim();
String homeNumberr = EThomeNumber.getText().toString().trim();
String emaill = ETemail.getText().toString().trim();
String dateOfBirthh = ETdateOfBirth.getText().toString().trim();
String groupp = SPgroupContacts.getSelectedItem().toString();
if (namee.isEmpty()) {
ETname.setError("Wpisz imie");
ETname.requestFocus();
return;
}
if (surnamee.isEmpty()) {
ETsurname.setError("Wpisz naziwsko");
ETsurname.requestFocus();
return;
}
if (cityy.isEmpty()) {
ETcity.setError("Wpisz miasto");
ETcity.requestFocus();
return;
}
if (streett.isEmpty()) {
ETstreet.setError("Wpisz ulice");
ETstreet.requestFocus();
return;
}
if (homeNumberr.isEmpty()) {
EThomeNumber.setError("Wpisz numer domu");
EThomeNumber.requestFocus();
return;
}
if (houseNumberr.isEmpty()) {
EThouseNumber.setError("Wpisz numer mieszkania");
EThomeNumber.requestFocus();
return;
}
if (phoneNumberr.isEmpty()) {
ETphoneNumber.setError("Wpisz numer telefonu");
ETphoneNumber.requestFocus();
return;
}
if (emaill.isEmpty()) {
ETemail.setError("Wpisz email");
ETemail.requestFocus();
return;
}
if (dateOfBirthh.isEmpty()) {
ETdateOfBirth.setError("Wpisz date urodzenia");
ETdateOfBirth.requestFocus();
return;
}
String sql = "UPDATE contacts \n" +
"SET namee = ?, \n" +
"groupp = ?, \n" +
"surnamee = ?, \n" +
"cityy = ?, \n" +
"streett = ?, \n" +
"homeNumberr = ?, \n" +
"houseNumberr = ?, \n" +
"phoneNumberr = ?, \n" +
"emaill = ?, \n" +
"dateOfBirthh = ?, \n" +
"WHERE id = ?;\n";
mDatabase.execSQL(sql, new String[]{namee, groupp, surnamee, cityy, streett, homeNumberr, houseNumberr, phoneNumberr, emaill, dateOfBirthh, String.valueOf(contacts.getId())});
Toast.makeText(mCtx, "Kontakt zaktualizowany", Toast.LENGTH_SHORT).show();
reloadContactsFromDatabase();
dialog.dismiss();
}
});
}
private void reloadContactsFromDatabase() {
Cursor cursorContacts = mDatabase.rawQuery("SELECT * FROM contacts", null);
if (cursorContacts.moveToFirst()) {
contactsList.clear();
do {
contactsList.add(new Contacts(
cursorContacts.getInt(0),
cursorContacts.getString(1),
cursorContacts.getString(2),
cursorContacts.getString(3),
cursorContacts.getString(4),
cursorContacts.getString(5),
cursorContacts.getString(6),
cursorContacts.getString(7),
cursorContacts.getString(8),
cursorContacts.getString(9),
cursorContacts.getString(10)
));
} while (cursorContacts.moveToNext());
cursorContacts.close();
notifyDataSetChanged();
}
}
}
Poratujesz ? 🙂