Why does firebase hang inside a for loop - firebase

I don't have much experience in programming. I wanted to because the for loop prevents me from moving forward after onDataChange.
binding.inviaPubButton.setOnClickListener {
var numeroAutoId:Int = 2
for (i in 0..199){
estraiNumero(numeroAutoId)
numeroAutoId++
}
}
}
fun estraiNumero(numeroAutoId:Int) {
databaseReference = FirebaseDatabase.getInstance().getReference("formcaffeinpiazza")
eventListener =
databaseReference!!.orderByChild("dataNumberId").equalTo("$numeroAutoId")
//.addValueEventListener(object : ValueEventListener {
.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
snapshot.children.forEach {
//"it" is the snapshot
var numeroCelEstratto: String = it.child("dataPriority").toString()
inviaSms(numeroCelEstratto)
Toast.makeText(
this#InviaPubblicitaActivity,
"sms inviati",
Toast.LENGTH_SHORT
).show()
var intent =
Intent(this#InviaPubblicitaActivity, MainActivity::class.java)
startActivity(intent)
}
}
override fun onCancelled(error: DatabaseError) {
Toast.makeText(
this#InviaPubblicitaActivity,
"qualcosa รจ andato storto",
Toast.LENGTH_SHORT
).show()
}
})
}
It should filter the database by ID, then extrapolate the mobile number. To then send a text message to each number

Related

Can we create an ArrayList of parent nodes in firebase

I am a newbie in android development. I have searched google for answer to my problem but I could not find any that is why I am posting here.
Here is my db:
I want to retrieve list/Array list of all the "keys" and nested objects(0,1,2). Something like this {key1{object1,object2,..}key2{object1,object2,..}}. I can also use custom title/text instead of random keys.
private val _getquestlist = MutableLiveData<List<userquestlist>>()
val getquestlist: LiveData<List<userquestlist>>
get() = _getquestlist
fun fetchusertest(uname:String,sub:String,mon:String){
FirebaseDatabase.getInstance().getReference("UsersTest").child(sub).child(mon)
.addListenerForSingleValueEvent(object :ValueEventListener{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
var questionlist = mutableListOf<Question>()
for (keysnapshot in snapshot.children) {
val keystring: String? = keysnapshot.key
FirebaseDatabase.getInstance().getReference("UsersTest").child(sub).child(mon).child(keystring!!)
.addListenerForSingleValueEvent(object :ValueEventListener{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot) {
for (questSnapshot in snapshot.children) {
val question = questSnapshot.getValue(Question::class.java)
question?.let { questionlist.add(it) }
}
_getquestlist.value = questionlist
}
})
}
}
else _getquestlist.value=null
}
})
}
and this is Question class
data class Question(
var question:String?=null,
var opa:String?=null,
var opb:String?=null,
var opc:String?=null,
var time:Long?=null
)

How to query different references in firebase and check if one of the child is equal to the other childs value

I am not sure if the question seems perfect but I wIll appreciate any help
here is my firebase database
I have 2 different references.one is the user and the other is the item..its a buying and selling app..I want to set the users image to his item on the show item activity...
how do i go about the query..
if boyo#yahoo.com post an item,I want the other users to see his image and his full names under the showitemactivity
here is a screen showof my show activity item
thanks in anticipation
here is the code I am using
private fun getItemDetails(itemId: String?) {
val firebaseDatabase: FirebaseDatabase = FirebaseDatabase.getInstance()
var rootRef: DatabaseReference = firebaseDatabase.reference
rootRef.child("Items").child(itemId!!).addValueEventListener(object : ValueEventListener{
override fun onDataChange(p0: DataSnapshot) {
if (p0.exists()) {
val sellerid: String = p0.child("uid").value.toString()
(imagesArray as ArrayList<Items>).clear()
for (snapshot in p0.children) {
val items = p0.getValue(Items::class.java)
/* var decimalFormat: DecimalFormat = DecimalFormat("#,##0.00")
var formattedValue: String = decimalFormat.format(items!!.getpricee())*/
(imagesArray as ArrayList<Items>).add(items!!)
priceTV!!.text = items.getpricee()
titleTV!!.text = items.gettitlee()
descriptionTV!!.text = items.getdescriptionn()
categoryTV!!.text = items.getcategoryy()
subcategoryTV!!.text = items.getsubcategoryy()
state!!.text = items.getstatee()
lga!!.text = items.getlgaa()
date!!.text = items.getdatee()
time!!.text = items.gettimee()
rootRef.child("Users").addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
if(p0.exists()) {
for(snapshot in p0
.children) {
val users = snapshot.getValue(Users::class.java)
if(snapshot.equals(sellerid)) {
username!!.text = users!!.getFullnamee()
if(users.getImageee().isEmpty()) {
userImage!!.setImageResource(R.drawable.avatarplaceholder)
} else {
Picasso.get().load(users.getImageee())
.into(userImage)
}
}
}
}
}
})
}
}
sliderPageAdapter?.notifyDataSetChanged()
}
override fun onCancelled(p0: DatabaseError) {
}
})
}

Get data from a Firebase Realtime Database child

This my project I have 2 RecyclewView, the first selects the user I want to manipulate, which is working perfectly.
When selecting it I store your id and transfer it to a next screen that should show your medical history (using your id to search your database for your history).
But this recycler view remains blank, does not give an error but also does not return anything, maybe I'm missing the time to put the path to be taken to bring this history.
image BD Firestore: [1]: https://imgur.com/HOxQTsI
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_verificar_historico)
setSupportActionBar(toolbar_historico)
toolbar_historico.setTitle("Titulo Aqui")
userId = intent.extras?.getString("userId")
textViewX.setText(userId)
getLastKey()
val layoutManager = LinearLayoutManager(this)
recycler_view_historico.layoutManager = layoutManager
val dividerItemDecoration = DividerItemDecoration(recycler_view_historico.context, layoutManager.orientation)
recycler_view_historico.addItemDecoration(dividerItemDecoration)
adapter = HistoricoAdapter(this)
recycler_view_historico.adapter = adapter
getHistorico()
recycler_view_historico.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
total_item = layoutManager.itemCount
last_visible_item = layoutManager.findLastVisibleItemPosition()
if (isLoading && total_item <= last_visible_item + ITEM_COUNT) {
getHistorico()
isLoading = true
}
}
})
}
private fun getHistorico() {
if (!isMaxData) {
val query: Query
if (TextUtils.isEmpty(last_node))
query = FirebaseDatabase.getInstance().reference
.child("pacientes/$userId/historico")
.orderByKey()
.limitToFirst(ITEM_COUNT)
else
query = FirebaseDatabase.getInstance().reference
.child("pacientes/$userId/historico")
.orderByKey()
.startAt(last_node)
.limitToFirst(ITEM_COUNT)
query.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
if (p0.hasChildren()) {
val historicoLista = ArrayList<HistoricoRecycle>()
for (snapshot in p0.children)
historicoLista.add(snapshot.getValue(HistoricoRecycle::class.java)!!)
last_node = historicoLista[historicoLista.size - 1].data
if (!last_node.equals(last_key))
historicoLista.removeAt(historicoLista.size - 1)
else
last_node = "end"
adapter.addAll(historicoLista)
isLoading = false
} else {
isLoading = false
isMaxData = true
}
}
})
}
}
private fun getLastKey() {
val get_last_key = FirebaseDatabase.getInstance().getReference()
.child("pacientes/$userId/historico")
.orderByKey()
.limitToLast(1)
get_last_key.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
for (userSnapShot in p0.children)
last_key = userSnapShot.key
}
})
}

i got this error and cant resolve it: Expected a List while deserializing, but got a class java.lang.String;

i kown that is sunday but i got this problem, and i dont have any idea about how i fix it.
ps: the other questions in stackoverflow are in java, im using kotlin, and are about map error, not list.
my class User
#Parcelize
class User (val primeiroLogin:Boolean, val tutorialComplete:Boolean, val
username:String, val uid:String, val viewed:ArrayList<String>) : Parcelable {
constructor() : this(true,false,"","", ArrayList())
}
most part of the activity where i got the error
var viewed = ArrayList<String>()
private fun selectCategories(){
val ref = FirebaseDatabase.getInstance().getReference("/users")
ref.addListenerForSingleValueEvent(object: ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
var cUid = ""
val user = FirebaseAuth.getInstance().currentUser
user?.let {
val uid = user.uid
cUid = uid
}
p0.children.forEach{
val user = it.getValue(User::class.java)
if(user?.uid == cUid){
viewed = user.viewed
}
}
}
})
}
private fun fetchCategories(){
val ref = FirebaseDatabase.getInstance().getReference("/categories")
ref.addListenerForSingleValueEvent(object: ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
val adapter = GroupAdapter<ViewHolder>()
p0.children.forEach{
val category = it.getValue(Category::class.java)
if(category != null){
for (i in 0..viewed.size) {
if(category.real_name != viewed[i]){
adapter.add(categories(category))
}
}
}
}
adapter.setOnItemClickListener { item, view ->
val categories = item as categories
val intent = Intent(view.context, LearningFirstLibras::class.java)
intent.putExtra(CATEGORY_KEY, categories.category)
startActivity(intent)
}
new_words_categories.adapter = adapter
}
})
.....
thank you guys!

Cannot fetch messages from another user FirebaseDatabase

This is a simple chat messaging app, using Firebase Realtime Database and the Authentication. I have successfully created the database, but I cannot fetch the message from one user to another. Because, when I send the message/click the send button, it creates different nodes, I think its a string node with quotation mark in the name. The node structure I created is user-messages --> fromId(current user uid) --> toUid(to whom the messages are sent).
To make more clear explanation, this is the screenshot of what happened.
I think the problem is, it creates a different nodes with the "uid", which means string. So the database has two nodes, one with the "...", and the other the normal one. For some info, I use some library such as Picasso image load, groupie, and parcelize.
This is the source code for the ChatLogActivity
import...
class ChatLogActivity : AppCompatActivity() {
companion object {
val TAG = "ChatLog"
}
val adapter = GroupAdapter<ViewHolder>()
var toUser: User? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat_log)
recyclerview_chat_log.adapter = adapter
toUser = intent.getParcelableExtra(NewMessageActivity.USER_KEY)
titleChatLog.text = toUser?.nama
listenForMessages()
arrowBack.setOnClickListener {
finish()
}
send_button_chat_log.setOnClickListener {
Log.d(TAG, "Attempt to send message.....")
performSendMessage()
}
}
private fun listenForMessages() {
val fromId = FirebaseAuth.getInstance().uid
val toId = toUser?.uid
val ref = FirebaseDatabase.getInstance().getReference("/user-messages/$fromId/$toId")
ref.addChildEventListener(object : ChildEventListener {
override fun onChildAdded(p0: DataSnapshot, p1: String?) {
val chatMessage = p0.getValue(ChatMessage::class.java)
if (chatMessage != null) {
Log.d(TAG, chatMessage.text)
if (chatMessage.fromId == FirebaseAuth.getInstance().uid) {
val currentUser = LatestMessageActivity.currentUser?: return
adapter.add(ChatFromItem(chatMessage.text, currentUser))
} else {
adapter.add(ChatToItem(chatMessage.text, toUser!!))
}
}
}
override fun onCancelled(p0: DatabaseError) {
}
override fun onChildChanged(p0: DataSnapshot, p1: String?) {
}
override fun onChildMoved(p0: DataSnapshot, p1: String?) {
}
override fun onChildRemoved(p0: DataSnapshot) {
}
})
}
private fun performSendMessage() {
val text = edittext_chat_log.text.toString()
val fromId = FirebaseAuth.getInstance().uid
val user = intent.getParcelableExtra<User>(NewMessageActivity.USER_KEY)
val toId = user.uid
val ref = FirebaseDatabase.getInstance().getReference("/user-messages/$fromId/$toId").push()
val toRef = FirebaseDatabase.getInstance().getReference("/user-messages/$toId/$fromId ").push()
if (fromId == null) return
val chatMessage = ChatMessage(ref.key!!, text, fromId, toId, System.currentTimeMillis() / 1000)
ref.setValue(chatMessage)
.addOnSuccessListener {
Log.d(TAG, "Save our chat message: ${ref.key}")
edittext_chat_log.text.clear()
recyclerview_chat_log.scrollToPosition(adapter.itemCount - 1)
}
toRef.setValue(chatMessage)
}
}
class ChatFromItem(val text: String, val user: User) : Item<ViewHolder>() {
override fun getLayout(): Int {
return R.layout.chat_from_row
}
override fun bind(viewHolder: ViewHolder, position: Int) {
val uri = user.profileImageUrl
val targetImageView = viewHolder.itemView.imageViewFrom
Picasso.get().load(uri).into(targetImageView)
viewHolder.itemView.textViewFrom.text = text
}
}
class ChatToItem(val text: String, val user: User) : Item<ViewHolder>() {
override fun getLayout(): Int {
return R.layout.chat_to_row
}
override fun bind(viewHolder: ViewHolder, position: Int) {
val uri = user.profileImageUrl
val targetImageView = viewHolder.itemView.imageViewTo
Picasso.get().load(uri).into(targetImageView)
viewHolder.itemView.textViewTo.text = text
}
}
And this is the ChatMessage, class that store the messages.
class ChatMessage(val id:String, val text: String, val fromId: String, val toId: String, val timeStamp: Long) {
constructor(): this("", "", "", "", -1)
}
I'm very glad if anyone can help this problem. Thank you

Resources