如何使用firebase中的数据收集创建微调器项(在Kotlin中)
我需要一些代码方面的帮助。 我在下面附上了XML和kt代码 我有2个firebase数据收集(学生和教程)。 学生(学生ID、fname、lname、fk_教程) 教程(ID、类名、日期、时间) 这就是我想要实现的目标如何使用firebase中的数据收集创建微调器项(在Kotlin中),kotlin,spinner,Kotlin,Spinner,我需要一些代码方面的帮助。 我在下面附上了XML和kt代码 我有2个firebase数据收集(学生和教程)。 学生(学生ID、fname、lname、fk_教程) 教程(ID、类名、日期、时间) 这就是我想要实现的目标 从教程数据生成微调器(假设教程集合中有3个条目),然后它将生成3个项目,格式为className+“”+day+“”+time。其中教程(ID)对用户隐藏 当用户点击“保存”按钮时,系统将保存教程(ID),而不是className+“”+day+“”+time 如何从tutori
<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()
}
}
`au.edu.utas.tsphoa.kit607包
导入androidx.appcompat.app.appcompat活动
导入android.os.Bundle
导入android.util.Log
导入au.edu.utas.tsphoa.kit607.databinding.ActivityAddStudentBinding
导入com.google.firebase.firestore.ktx.firestore
导入com.google.firebase.firestore.ktx.toObject
导入com.google.firebase.ktx.firebase
val tuteItem=mutableListOf()
类添加\学生:AppCompatActivity(){
私有lateinit变量ui:ActivityAddStudentBinding
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
ui=ActivityAddStudentBinding.充气(LayoutFlater)
setContentView(ui.root)
var db=Firebase.firestore
val studentCollection=db.collection(“学生”)
val tutorialCollection=db.collection(“教程”)
教程集
.get()
.addOnSuccessListener{result->
Log.d(FIREBASE_标记,“--ALL class-->”)
用于(结果中的文档)
{
val tutorial=document.toObject()
tutorial.id=document.id
Log.d(FIREBASE_标记,tutorial.toString())
添加(教程)
}
}
//微调器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()
)
//给学生带来乐趣
有趣的学生(){
学生收藏
.add(新闻学生)
.addOnSuccessListener{
Log.d(FIREBASE_标记,“使用id${it.id}创建的文档”)
newStudent.id=it.id
}
.addOnFailureListener{
Log.e(FIREBASE_标签,“错误写入文档”,it)
}
}
addStudent()
完成()
}
}
这是科特林