android_practical
android_practical
SQLiteOpenHelper
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.Cursor
import android.database.sqlite.SQLiteOpenHelper
companion object {
const val DATABASE_NAME = "MyDatabase.db"
const val DATABASE_VERSION = 1
const val TABLE_NAME = "users"
const val COLUMN_ID = "id"
const val COLUMN_NAME = "name"
const val COLUMN_AGE = "age"
}
********MainActivity**********
import android.database.Cursor
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
// Insert data
val userId = dbHelper.insertUser("John Doe", 30)
Toast.makeText(this, "User inserted with ID: $userId",
Toast.LENGTH_SHORT).show()
// Update data
val updatedRows = dbHelper.updateUser(userId.toInt(),
"John Smith", 31)
Toast.makeText(this, "Rows updated: $updatedRows",
Toast.LENGTH_SHORT).show()
// Delete data
val deletedRows = dbHelper.deleteUser(userId.toInt())
Toast.makeText(this, "Rows deleted: $deletedRows",
Toast.LENGTH_SHORT).show()
////////////////////////////////notification
code/////////////////////////////////////////
****MainActivity.kt:
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import android.os.Bundle
val notificationManager =
NotificationManagerCompat.from(this)
notificationManager.notify(1, builder.build()) // Notify with
ID = 1
}
}
/////////////////// progresss notification //////////////////////////
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import android.os.Bundle
import android.os.Handler
import android.os.Looper
**********build.gradle (app)***********
dependencies {
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-
gson:2.9.0'
**************MainActivity******************
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
userTextView = findViewById(R.id.userTextView)
call.enqueue(object : Callback<User> {
override fun onResponse(call: Call<User>, response:
Response<User>) {
if (response.isSuccessful) {
// If response is successful, display the user's info
val user = response.body()
userTextView.text = "ID: ${user?.id}\nName: $
{user?.name}\nEmail: ${user?.email}"
} else {
// Handle unsuccessful response
Toast.makeText(this@MainActivity, "Error: $
{response.code()}", Toast.LENGTH_SHORT).show()
}
}
////////////////////////moshi_library//////////////////
**********build.gradle ***********
dependencies {
implementation 'com.squareup.moshi:moshi:1.15.0' // Latest
version at the time
implementation 'com.squareup.moshi:moshi-kotlin:1.15.0'
implementation 'com.squareup.retrofit2:converter-
moshi:2.9.0' // if using Retrofit
}
*************json*************
{
"id": 1,
"name": "John Doe",
"email": "[email protected]"
}
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import com.squareup.moshi.Moshi
import
com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.squareup.moshi.JsonAdapter
fun main() {
// Initialize Moshi with Kotlin adapter factory
val moshi =
Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
// Serialize example
val user = User(1, "John Doe", "[email protected]")
val json = jsonAdapter.toJson(user)
println("Serialized JSON: $json")
// Deserialize example
val jsonString = """{"id":1,"name":"John
Doe","email":"[email protected]"}"""
val userFromJson = jsonAdapter.fromJson(jsonString)
println("Deserialized User: $userFromJson")
}
Output: