Skocz do zawartości
kacper635

Crash aplikacji - Dyskusja Ogólna

    Rekomendowane odpowiedzi

    18 godzin temu, Majks napisał:

    attribute android:style not found.

    Zamiast:  android:style="@style/editStyle", powinno być:

    style="@style/editStyle"

    ;)

    • Piwko! 1

    Udostępnij tę odpowiedź


    Odnośnik do odpowiedzi
    Udostępnij na innych stronach

    Mam problem mały problem z SQLite.

    Zacząłem robić poradnik ( a właściwie modyfikować go żeby zwyczajnie nie przepisywać 1:1 ) i napotkałem się na problem.

     

    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.contactapp, PID: 4320
        android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: contacts.id (code 1299)
            at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
            at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:735)
            at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
            at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1754)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1732)
            at com.example.contactapp.MainActivity.addContact(MainActivity.java:136)
            at com.example.contactapp.MainActivity.onClick(MainActivity.java:147)
            at android.view.View.performClick(View.java:6294)
            at android.view.View$PerformClick.run(View.java:24770)
            at android.os.Handler.handleCallback(Handler.java:790)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

     

    To mój kod:

    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 int NOT NULL CONSTRAINT contacts_pk 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;
            }
        }
    
    }

     

    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>

     

    tutorial który wykonuje: https://www.simplifiedcoding.net/android-sqlite-database-example/

     

    Czy ktoś może wskazać jak rozwiążę ten problem ?

    Udostępnij tę odpowiedź


    Odnośnik do odpowiedzi
    Udostępnij na innych stronach
     mDatabase.execSQL(
                    "CREATE TABLE IF NOT EXISTS contacts (\n" +
                            "    id int NOT NULL CONSTRAINT contacts_pk 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" +
                            ");"
            );

    Tutaj zabrakło Ci AUTOINCREMENT przy kluczu głównym. Jak dodajesz coś do bazy to wszystkie pola oznaczone NOT NULL muszą być czymś wypełnione. Twój insert nie zawierał wartości dla id. Dlatego dostałeś wyjątek. AUTOINCREMENT powoduje że wartość dla pola jest dodawana automatycznie.

    Dla id powinno być:
     

    "    id int NOT NULL CONSTRAINT contacts_pk PRIMARY KEY AUTOINCREMENT,\n" 

     

    • Piwko! 1

    Udostępnij tę odpowiedź


    Odnośnik do odpowiedzi
    Udostępnij na innych stronach
    Napisano (edytowane)
    17 minut temu, Coders Lab napisał:
     mDatabase.execSQL(
                    "CREATE TABLE IF NOT EXISTS contacts (\n" +
                            "    id int NOT NULL CONSTRAINT contacts_pk 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" +
                            ");"
            );

    Tutaj zabrakło Ci AUTOINCREMENT przy kluczu głównym. Jak dodajesz coś do bazy to wszystkie pola oznaczone NOT NULL muszą być czymś wypełnione. Twój insert nie zawierał wartości dla id. Dlatego dostałeś wyjątek. AUTOINCREMENT powoduje że wartość dla pola jest dodawana automatycznie.

    Dla id powinno być:
     

    "    id int NOT NULL CONSTRAINT contacts_pk PRIMARY KEY AUTOINCREMENT,\n" 

     

     

    Dodałem AUTOINCREMENT tak jak wskazałeś, natomiast wywaliło jeszcze więcej 'strasznego czerwonego tekstu' ?

     

    E/SQLiteLog: (1) AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
    D/AndroidRuntime: Shutting down VM
    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.contactapp, PID: 5260
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactapp/com.example.contactapp.MainActivity}: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: CREATE TABLE IF NOT EXISTS contacts (
            id int NOT NULL CONSTRAINT contacts_pk PRIMARY KEY AUTOINCREMENT,
            namee varchar(200) NOT NULL,
            surnamee varchar(200) NOT NULL,
            cityy varchar(200) NOT NULL,
            streett varchar(200) NOT NULL,
            homeNumberr varchar(200) NOT NULL,
            houseNumberr varchar(200) NOT NULL,
            phoneNumberr varchar(200) NOT NULL,
            emaill varchar(200) NOT NULL,
            dateOfBirthh varchar(200) NOT NULL,
            groupp varchar(200) NOT NULL
        );
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: CREATE TABLE IF NOT EXISTS contacts (
            id int NOT NULL CONSTRAINT contacts_pk PRIMARY KEY AUTOINCREMENT,
            namee varchar(200) NOT NULL,
            surnamee varchar(200) NOT NULL,
            cityy varchar(200) NOT NULL,
            streett varchar(200) NOT NULL,
            homeNumberr varchar(200) NOT NULL,
            houseNumberr varchar(200) NOT NULL,
            phoneNumberr varchar(200) NOT NULL,
            emaill varchar(200) NOT NULL,
            dateOfBirthh varchar(200) NOT NULL,
            groupp varchar(200) NOT NULL
        );
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1752)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1682)
            at com.example.contactapp.MainActivity.createContactsTable(MainActivity.java:51)
            at com.example.contactapp.MainActivity.onCreate(MainActivity.java:47)
            at android.app.Activity.performCreate(Activity.java:7009)
            at android.app.Activity.performCreate(Activity.java:7000)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
            	... 9 more

     

    A no i oczywiście mam świadomość tego że wszystkie pola muszą być uzupełnione, ale przed tym uchroniłem się 

    private boolean inputsAreCorrect(String namee, String surnamee, String cityy, String streett, String homeNumberr, String houseNumberr, String phoneNumberr, String emaill, String dateOfBirthh)
    Edytowane przez EmAndEm

    Udostępnij tę odpowiedź


    Odnośnik do odpowiedzi
    Udostępnij na innych stronach
    2 minuty temu, EmAndEm napisał:

    Dodałem AUTOINCREMENT tak jak wskazałeś, natomiast wywaliło jeszcze więcej 'strasznego czerwonego tekstu' ?

    Ok, moje niedopatrzenie xD Powinno jeszcze być INTEGER zamiast int

    "    id INTEGER NOT NULL CONSTRAINT contacts_pk PRIMARY KEY AUTOINCREMENT,\n" 

    Mówi o tym pierwsza linia z błędu, który dostałeś:

    AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
    • Piwko! 1

    Udostępnij tę odpowiedź


    Odnośnik do odpowiedzi
    Udostępnij na innych stronach
    4 minuty temu, Coders Lab napisał:

    Ok, moje niedopatrzenie xD Powinno jeszcze być INTEGER zamiast int

    "    id INTEGER NOT NULL CONSTRAINT contacts_pk PRIMARY KEY AUTOINCREMENT,\n" 

    Mówi o tym pierwsza linia z błędu, który dostałeś:

    AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY

     

    Nastąpił powrót do błędów, które wkleiłem na początku postu 

    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.contactapp, PID: 5891
        android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: contacts.id (code 1299)
            at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
            at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:735)
            at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
            at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1754)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1732)
            at com.example.contactapp.MainActivity.addContact(MainActivity.java:136)
            at com.example.contactapp.MainActivity.onClick(MainActivity.java:147)
            at android.view.View.performClick(View.java:6294)
            at android.view.View$PerformClick.run(View.java:24770)
            at android.os.Handler.handleCallback(Handler.java:790)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

     

    Udostępnij tę odpowiedź


    Odnośnik do odpowiedzi
    Udostępnij na innych stronach

    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 ;)

    • Piwko! 1

    Udostępnij tę odpowiedź


    Odnośnik do odpowiedzi
    Udostępnij na innych stronach
    W dniu 8.05.2019 o 18:42, Coders Lab napisał:

    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 "<column name> 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="[email protected]"
                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 ? :)

    Udostępnij tę odpowiedź


    Odnośnik do odpowiedzi
    Udostępnij na innych stronach

    Jeżeli chodzi o UPDATE to podkreśla Ci ponieważ masz przecinek tutaj:

    "dateOfBirthh = ?, \n" +

    Przecinek dodaje się wyłącznie aby oddzielać pola które aktualizujesz.

    Co do DELETE. Jest jakiś komunikat błędu w logach?

    • Piwko! 1

    Udostępnij tę odpowiedź


    Odnośnik do odpowiedzi
    Udostępnij na innych stronach
    10 godzin temu, Coders Lab napisał:

    Jeżeli chodzi o UPDATE to podkreśla Ci ponieważ masz przecinek tutaj:

    "dateOfBirthh = ?, \n" +

    Przecinek dodaje się wyłącznie aby oddzielać pola które aktualizujesz.

    Co do DELETE. Jest jakiś komunikat błędu w logach?

    Zarówno na Update i Delete dostaje teraz errora pod tytułem

    java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference

    Czyli gdzieś się gubi na odnajdowaniu chyba XML'owych  ID (?)

    Delete:

    Spoiler
    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.contactapp, PID: 10781
        java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String, java.lang.Object[])' on a null object reference
            at com.example.contactapp.ContactsAdapter$2$1.onClick(ContactsAdapter.java:84)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

     

    Update:

    Spoiler
    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.contactapp, PID: 10932
        java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
            at com.example.contactapp.ContactsAdapter.updateContacts(ContactsAdapter.java:121)
            at com.example.contactapp.ContactsAdapter$1.onClick(ContactsAdapter.java:71)
            at android.view.View.performClick(View.java:6294)
            at android.view.View$PerformClick.run(View.java:24770)
            at android.os.Handler.handleCallback(Handler.java:790)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

     

     

    Udostępnij tę odpowiedź


    Odnośnik do odpowiedzi
    Udostępnij na innych stronach

    Jeśli chcesz dodać odpowiedź, zaloguj się lub zarejestruj nowe konto

    Jedynie zarejestrowani użytkownicy mogą komentować zawartość tej strony.

    Zarejestruj nowe konto

    Załóż nowe konto. To bardzo proste!

    Zarejestruj się

    Zaloguj się

    Posiadasz już konto? Zaloguj się poniżej.

    Zaloguj się

    • Ostatnio przeglądający   0 użytkowników

      Brak zarejestrowanych użytkowników przeglądających tę stronę.

    x