我想用用户从日期选择器对话框中选择的日期命名一个FireStore文档(为字符串)。
“ docRef = db.collection(”时隙列表“).document( 用户选择的日期 )”
问题出在,而不是获取名为“ 22/02/2019”的文档 我得到下面的屏幕快照中显示的内容。
创建了2个文档(25和null),然后创建了一个名为(02)的集合,然后创建了一个名为(2019)的文档!!
代码:
class ReserveTimeFragment : Fragment() {
private var timeSlotsList: ArrayList<TimeSlot>? = null
private var mRecyclerView: RecyclerView? = null
private var mAdapter: TimeSlotAdapter? = null
private var mLayoutManager: RecyclerView.LayoutManager? = null
private var viewHolder: TimeSlotAdapter.TimeSlotViewHolder? = view?.tag as TimeSlotAdapter.TimeSlotViewHolder?
private lateinit var db: FirebaseFirestore
private val dateFormat = SimpleDateFormat("dd / MM / yyyy")
private var dateString: String? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_reserve_time, container, false)
db = FirebaseFirestore.getInstance()
mRecyclerView = view.findViewById(R.id.recyclerViewLO)
pickDate()
callTimeSlotsList()
return view
}
**private fun pickDate() {
val cal = Calendar.getInstance()
var date: String
val datePick = DatePickerDialog(this.context!!, DatePickerDialog.OnDateSetListener { _, mYear, mMonth, mDay
->
cal.set(Calendar.YEAR, mYear)
cal.set(Calendar.MONTH, mMonth)
cal.set(Calendar.DAY_OF_MONTH, mDay)
dateTV.text = dateFormat.format(cal.time)
dateString = dateFormat.format(cal.time)
Toast.makeText(context!!, "$dateString is selected!", Toast.LENGTH_SHORT).show()
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH))
datePick.show()
datePick.setCancelable(false)
}**
private fun callTimeSlotsList(){
val docRef = db.collection("Time Slots List").document("$dateString")
docRef.addSnapshotListener(EventListener<DocumentSnapshot> { documentSnapshot, e ->
if (e != null) {
Log.w(TAG, "Time Slots List Listen failed.", e)
return@EventListener
}
if (documentSnapshot != null && documentSnapshot.exists()) {
docRef.get().addOnSuccessListener { documentSnapshot ->
val listObjectFromFB = documentSnapshot.toObject(ListObjectToFireStore::class.java)
val ListToRVAdaptet = listObjectFromFB?.listToFireStore
timeSlotsList = ListToRVAdaptet
buildRecyclerView()
Log.d(TAG, "FireStore Time Slots List Called Successfully")
}
} else {
Log.d(TAG, "New Time Slots List Loaded!")
loadTimeSlotsList()
buildRecyclerView()
}
})
}
private fun buildRecyclerView() {
mRecyclerView!!.setHasFixedSize(true)
mLayoutManager = LinearLayoutManager(context!!)
mAdapter = TimeSlotAdapter(timeSlotsList!!, context!!, db)
mRecyclerView!!.layoutManager = mLayoutManager
mRecyclerView!!.adapter = mAdapter
viewHolder?.cardview?.setOnClickListener {
}
}
private fun loadTimeSlotsList() {
val timeSlotsListToLoad: ArrayList<TimeSlot> = ArrayList()
timeSlotsListToLoad.add(TimeSlot("07:30", "Available", false, 2))
timeSlotsListToLoad.add(TimeSlot("08:00", "Available", false, 2))
val timeSlotsListToFB = ListObjectToFireStore(timeSlotsListToLoad)
db.collection("Time Slots List").document("$dateString").set(timeSlotsListToFB).addOnSuccessListener {
Log.w(TAG, "time Slots List Uploaded Successfully")
}.addOnFailureListener{
Log.w(TAG, "time Slots List Upload Failed")
}
timeSlotsList = timeSlotsListToLoad
}
有人可以帮我吗?
答案 0 :(得分:0)
/字符是Firestore中的路径分隔符。由于它具有特殊含义,因此不能成为文档或集合名称的一部分。阅读limits of collections, documents and fields上的文档:
对集合ID的约束
- 必须是有效的UTF-8字符
- 不得超过1,500个字节
- 不能包含正斜杠(/)
- 不能仅包含一个句点(。)或两个句点(..)
- 无法匹配正则表达式。*
在将其用作文档ID之前,请考虑从字符串中删除斜杠,或者寻找其他方式来识别文档。