微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用来自 firebase 的 dataCollection 创建微调项在 Kotlin 中

如何解决如何使用来自 firebase 的 dataCollection 创建微调项在 Kotlin 中

我需要一些关于我的代码的帮助。 我附上了 XML 和下面的 kt 代码

我有 2 个 firebase 数据收集(学生和教程)。 学生 (studentID,fname,lname,fk_tutorial) 教程(ID、className、日期、时间) 这是我想要实现的目标,

  1. 从教程数据生成微调器(假设教程集合中有 3 个条目),然后它将生成 3 个项目,格式为 className + " " + day " " + time。其中 Tutorial(ID) 对用户隐藏。
  2. 用户点击“SAVE”按钮时,系统将保存教程(ID),而不是 className + " " + day " " + time。

如何从 tutorialCollection 生成微调器? 并保存 ID 而不是 selectedItem.toString()。

我需要使用 kotlin。提前致谢!!

<Spinner
    android:id="@+id/input_class"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:minWidth="300dp"
    app:layout_constraintStart_toStartOf="@+id/label_chooseClass"
    app:layout_constraintTop_toBottomOf="@+id/label_chooseClass" />

<EditText
    android:id="@+id/input_studentID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="30dp"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintStart_toEndOf="@+id/label_studentID"
    app:layout_constraintTop_toBottomOf="@+id/input_class" />

<EditText
    android:id="@+id/input_lastName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:ems="10"
    android:inputType="textPersonName"
    app:layout_constraintStart_toStartOf="@+id/input_studentID"
    app:layout_constraintTop_toBottomOf="@+id/input_studentID" />

<EditText
    android:id="@+id/input_firstName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:ems="10"
    android:inputType="textPersonName"
    app:layout_constraintStart_toStartOf="@+id/input_studentID"
    app:layout_constraintTop_toBottomOf="@+id/input_lastName" />

<Button
    android:id="@+id/button_saveStudent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="30dp"
    android:layout_marginLeft="30dp"
    android:layout_marginBottom="50dp"
    android:text="@string/save"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="@+id/label_firstName" />

这是 XML

 `package au.edu.utas.tsphoa.kit607
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import au.edu.utas.tsphoa.kit607.databinding.ActivityAddStudentBinding
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.firestore.ktx.toObject
import com.google.firebase.ktx.Firebase


val tuteItem = mutablelistof<Tutorial>()

class add_student : AppCompatActivity() {
    private lateinit var ui: ActivityAddStudentBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ui = ActivityAddStudentBinding.inflate(layoutInflater)
        setContentView(ui.root)

        var db = Firebase.firestore
        val studentCollection = db.collection("student")
        val tutorialCollection = db.collection("tutorial")

        tutorialCollection
            .get()
            .addOnSuccessListener { result ->
                Log.d(FIREBASE_TAG,"--ALL CLASSES--")
                for (document in result)
                {
                    val tutorial = document.toObject<Tutorial>()
                    tutorial.id = document.id
                    Log.d(FIREBASE_TAG,tutorial.toString())

                    tuteItem.add(tutorial)
                }
            }
        //spinner ID = 

    }
ui.buttonSaveStudent.setonClickListener{
                val newStudent = StudentDetails(
                        studentID = ui.inputStudentID.text.toString().toInt(),lastName = ui.inputLastName.text.toString(),firstName = ui.inputFirstName.text.toString(),tutorialClass = ui.inputClass.selectedItem.toString()
                )
                //fun to addStudent
                fun addStudent() {
                    studentCollection
                            .add(newStudent)
                            .addOnSuccessListener {
                                Log.d(FIREBASE_TAG,"Document created with id ${it.id}")
                                newStudent.id = it.id
                            }
                            .addOnFailureListener {
                                Log.e(FIREBASE_TAG,"Error writing document",it)
                            }
                }
                addStudent()
                finish()
            }
}

这是科特林

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。