Hi i am trying to recevie documentID from Firebase Firestore to use for deleting item from recyclerView. I tried to add it to my document first, then i will retrieve it and user could delete whichever item that user wants within docID. Firstly equalized the documentId variable to an empty string then added a snapshotListener in addData function, with for loop i equalized every document.id value to my empty documentID.
Problem is occuring there: documentId variable returns empty value everytime. Pls help.
Sorry for any infancy.
Thanks in advance.
addData Function:
fun addData(view: View) {
var chosenPair=ozelSharedPreferences.clickiAl().toString()
val currentU = Firebase.auth.currentUser
val dbCollection = currentU?.let {
it.email.toString()
}
var documentId= ""
val docRef = database.collection(dbCollection!!).document("Specified").collection("Pairs")
.document(chosenPair).collection("Analysis").orderBy("time")
docRef.addSnapshotListener { value, error ->
if (value != null && !value.isEmpty) {
val documents = value.documents
for (document in documents) {
documentId=document.id
}
}
}
val time= com.google.firebase.Timestamp.now()
val rr=rrText.text.toString()
var doubleRR=rr.toDoubleOrNull()
if (doubleRR==null) {
doubleRR=0.0
}
val analyzeDTO = AnalyzeDTO(
chartImage.text.toString(),
doubleRR,
reasonForText.text.toString(),
resultAddingText.text.toString(),
conceptText.text.toString(),
documentId,
time
)
viewModel.save(analyzeDTO)
val intent = Intent(this, PairDetailActivity::class.java)
startActivity(intent)
finish()
}
Retrieving Data Function:
fun retrieveData() {
var chosenPair=ozelSharedPreferences.clickiAl().toString()
val currentU = Firebase.auth.currentUser
val dbCollection = currentU?.let {
it.email.toString()
}
val docRef = database.collection(dbCollection!!).document("Specified").collection("Pairs")
.document(chosenPair).collection("Analysis").orderBy("time")
docRef.addSnapshotListener { value, error ->
if (value != null && !value.isEmpty) {
val documents = value.documents
for (document in documents) {
var docID= document.get("docID") as String
val url = document.get("tradingViewUrl") as String?
val rr = document.get("rrRatio") as Double?
val reason = document.get("reason") as String?
val result = document.get("result") as String?
val concept= document.get("concept") as String?
val time= document.get("time") as Timestamp
val downloadedAnalyze = AnalyzeDTO(url, rr, reason, result,concept, docID,time)
var analyzeList = arrayListOf(downloadedAnalyze)
list.value=analyzeList
}
} else if (error!= null) {
Toast.makeText(getApplication(),error.localizedMessage,Toast.LENGTH_LONG)
} else {
Toast.makeText(getApplication(),"Unknown Error", Toast.LENGTH_SHORT)
}
}
}