MainActivity.java
import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
var counter = 0
var test = "0"
var pref : SharedPreferences? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
pref = getSharedPreferences("TABLE", Context.MODE_PRIVATE)
counter = pref?.getInt("counter",0)!!
test = pref?.getString("test","0")!!
tvResult.text = counter.toString()
tvRes2.text = test
}
fun onClickAdd(view: View)
{
counter++
tvResult.text = counter.toString()
saveData(counter)
}
fun saveData(res: Int)
{
val editor = pref?.edit()
editor?.putInt("counter", res)
editor?.putString("test", "Test")
editor?.apply()
}
fun deleteAll()
{
val editor = pref?.edit()
editor?.clear()
editor?.apply()
}
fun deleteIem(deletItem: String)
{
val editor = pref?.edit()
editor?.remove(deletItem)
editor?.apply()
}
fun onClickClear(view: View)
{
deleteAll()
}
fun onClickDeleteItem(view: View)
{
deleteIem("test")
}
}
activity_main.java
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="com.example.kotlinlesson16.MainActivity">
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.174" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="44dp"
android:onClick="onClickAdd"
android:text="add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:onClick="onClickClear"
android:text="Clear"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/button3" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="28dp"
android:onClick="onClickDeleteItem"
android:text="Delete Item"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toEndOf="@+id/button4"
app:layout_constraintStart_toStartOf="@+id/button4" />
<TextView
android:id="@+id/tvRes2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="0"
app:layout_constraintEnd_toEndOf="@+id/tvResult"
app:layout_constraintStart_toStartOf="@+id/tvResult"
app:layout_constraintTop_toBottomOf="@+id/tvResult" />
</androidx.constraintlayout.widget.ConstraintLayout>