dimanche 20 décembre 2020

Storing a checkbox in Firebase Realtime database using kotlin

i just started to use Firebase as my app backend. My goal is to use the realtime database to store the tasks of my productivity app. I am trying to store checkboxes but i can't find a solution for it in tutorials. I only found for my editexts. Basically, my idea is to have a checbox called 'No due date' to use as a condition if a user doesn't have a predifined date for his task. I am storing my date as a string according to the iso format. But i can't find any clear documentation about checboxes. I tried booleans and string but nothing really convinced me. In the code, i tried to create some loop but i am not sure it is well coded. My code is based according to this tutorial https://www.youtube.com/watch?v=I485b7LzYkM&t=523s If you have any suggestions, i'll be more than happy to hear about them! Thank you so much for your time.

Here is my code of ScheduleActivity:


import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Spinner
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.gms.tasks.Task
import com.google.android.material.navigation.NavigationView
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.IgnoreExtraProperties
import com.google.firebase.database.ktx.database
import com.google.firebase.ktx.Firebase
import kotlinx.android.synthetic.main.activity_schedule.*
import java.util.*



class ScheduleActivity : AppCompatActivity() {

    lateinit var taskname:EditText
    lateinit var taskdescription:EditText
    lateinit var datebutton:Button
    lateinit var noduedate:CheckBox
    lateinit var category: Spinner
    lateinit var addreminder:CheckBox
    lateinit var tasktype: Spinner
    lateinit var taskstatus: Spinner
    lateinit var cancelbutton:Button
    lateinit var createbutton:Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_schedule)
        setSupportActionBar(findViewById(R.id.toolbar))
        val drawer = findViewById<DrawerLayout>(R.id.drawer)
        val drawerToggle = ActionBarDrawerToggle(this, drawer, R.string.open, R.string.close)
        drawer.addDrawerListener(drawerToggle)
        drawerToggle.syncState()
        val navView = findViewById<NavigationView>(R.id.navigation_view)

        taskname.findViewById<EditText>(R.id.taskname)
        taskdescription.findViewById<EditText>(R.id.taskdescription)
        datebutton.findViewById<Button>(R.id.datebutton)
        noduedate.findViewById<CheckBox>(R.id.noduedate)
        category.findViewById<Spinner>(R.id.category)
        addreminder.findViewById<CheckBox>(R.id.addreminder)
        tasktype.findViewById<Spinner>(R.id.tasktype)
        taskstatus.findViewById<Spinner>(R.id.taskstatus)
        cancelbutton.findViewById<Button>(R.id.cancelbutton)
        createbutton.findViewById<Button>(R.id.createbutton)

        navView.setNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.nav_profile -> {
                    var intent = Intent(this, ProfileActivity::class.java)
                    startActivity(intent)
                }
                R.id.nav_home -> {
                    var intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)
                }
                R.id.nav_schedule -> {
                    var intent = Intent(this, ScheduleActivity::class.java)
                    startActivity(intent)
                }
                R.id.nav_settings -> {
                    var intent = Intent(this, SettingsActivity::class.java)
                    startActivity(intent)
                }
                R.id.nav_tutorial -> {
                    var intent = Intent(this, TutorialActivity::class.java)
                    startActivity(intent)
                }
                R.id.nav_about -> {
                    var intent = Intent(this, AboutActivity::class.java)
                    startActivity(intent)
                }
                R.id.nav_procrastination -> {
                    var intent = Intent(this, ProcrastinationActivity::class.java)
                    startActivity(intent)
                }
                R.id.nav_premium -> {
                    var intent = Intent(this, PremiumActivity::class.java)
                    startActivity(intent)
                }
            }
            true
        }

        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        createbutton.setOnClickListener{
            saveTask()
        }
    }

    private fun saveTask(){
        val name = taskname.text.toString().trim()

        if (name.isEmpty()){
            taskname.error="Please enter a name"
            return
        }

        val date = datebutton.text.toString().trim()

        if(noduedate.isChecked()) {
            val nodudate=noduedate.text.toString().trim()
        } else{
            datebutton.error = "Please enter a date"
        }
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val drawer = findViewById<DrawerLayout>(R.id.drawer)
        val drawerToggle = ActionBarDrawerToggle(this, drawer, R.string.open, R.string.close)
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onBackPressed() {
        val drawer = findViewById<DrawerLayout>(R.id.drawer)
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }
}``` 



Aucun commentaire:

Enregistrer un commentaire