так что у меня есть 3 Edit text и три кнопки, и когда я пишу значение первичного ключа, как это (, count + 2), как показано ниже
realm.beginTransaction()
count = realm.where(GoodStudents::class.java).findAll().size
val goodStudents =
realm.createObject(GoodStudents::class.java, count + 2) // here
//goodStudents.id = count.toLong()
goodStudents.name = name.text.toString()
try {
goodStudents.grade = grade.text.toString().toInt()
} catch (e: NumberFormatException) {
}
val readData = realm.where(GoodStudents::class.java).findAll()
readData.forEach { save ->
// text.text = "" + save.name + ": " + goodStudents.grade
chaptersList.add("" + save.id + ")" + save.name!! + ": " + save.grade)
}
realm.commitTransaction()
deleteButton.setOnClickListener {
val id = id1.text.toString().toInt()
// var id = 0
val deleteData= realm.where(GoodStudents::class.java).equalTo("id", id).findFirst()
realm.beginTransaction()
if (id != null) {
deleteData!!.deleteFromRealm() // it says that the issue is in this place but I cannot figure this out !!
}
else if (id1.text.isEmpty()){
Toast.makeText(this@MainActivity, "ضع رقم من أردت حذفة في خانة - بيانات -", Toast.LENGTH_LONG).show()
}
realm.commitTransaction()
val dataRealmDeleteReader = realm.where(GoodStudents::class.java).findAll()
dataRealmDeleteReader.forEach{ read ->
chaptersList.add("" + read.id + ")" + read.name!! + ": " + read.grade)
}
, это дает мне эту ошибку:
FATAL EXCEPTION: main
Process: com.bluetechnology.realm, PID: 8371
kotlin.KotlinNullPointerException
at com.bluetechnology.realm.MainActivity$onCreate$1$onItemSelected$2.onClick(MainActivity.kt:134)
at android.view.View.performClick(View.java:7870)
at android.widget.TextView.performClick(TextView.java:14952)
at android.view.View.performClickInternal(View.java:7839)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29363)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7811)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
и когда я изменяю значение первичного ключа на (count + 1), как в этом коде:
realm.beginTransaction()
count = realm.where(GoodStudents::class.java).findAll().size
val goodStudents =
realm.createObject(GoodStudents::class.java, count + 1) // here is the issue of the second problem
//goodStudents.id = count.toLong()
goodStudents.name = name.text.toString()
try {
goodStudents.grade = grade.text.toString().toInt()
} catch (e: NumberFormatException) {
}
deleteButton.setOnClickListener {
val id = id1.text.toString().toInt()
// var id = 0
val deleteData= realm.where(GoodStudents::class.java).equalTo("id", id).findFirst()
realm.beginTransaction()
if (id != null) {
deleteData!!.deleteFromRealm()
}
else if (id1.text.isEmpty()){
Toast.makeText(this@MainActivity, "ضع رقم من أردت حذفة في خانة - بيانات -", Toast.LENGTH_LONG).show()
}
, это дает мне эту ошибку
io.realm.exceptions.RealmPrimaryKeyConstraintException: Primary key value already exists: 9 .
(/Users/cm/Realm/realm-java-release/realm/realm-library/src/main/cpp/io_realm_internal_OsObject.cpp:197)
at io.realm.internal.OsObject.nativeCreateNewObjectWithLongPrimaryKey(Native Method)
at io.realm.internal.OsObject.createWithPrimaryKey(OsObject.java:208)
at io.realm.Realm.createObjectInternal(Realm.java:1049)
at io.realm.Realm.createObject(Realm.java:1024)
at com.bluetechnology.realm.MainActivity$onCreate$1$onItemSelected$1.onClick(MainActivity.kt:108)
at android.view.View.performClick(View.java:7870)
at android.widget.TextView.performClick(TextView.java:14952)
at android.view.View.performClickInternal(View.java:7839)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29363)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7811)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
P. C .. Я удалил некоторые элементы, которые у меня были в моем Recycler View