How to get FirebaseDataBase Snapshot from current user?












0














private fun getUserInfo() {


val userkey = FirebaseAuth.getInstance().uid ?: ""
val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
ref.addListenerForSingleValueEvent(object: ValueEventListener {

override fun onDataChange(p0: DataSnapshot) {
val adapter = GroupAdapter<ViewHolder>()

p0.children.forEach {
Log.d("getUserInfo", it.toString())
val user = it.getValue(User::class.java)
if (user != null) {
adapter.add(UserItem(user))
}
}

UserInfo_RCView.adapter = adapter
}

override fun onCancelled(p0: DatabaseError) {

}
})
}
}

class UserItem(val user: User): Item<ViewHolder>() {
override fun bind(viewHolder: ViewHolder, position: Int) {
viewHolder.itemView.UsernameSettings_txt.text = user.username

Picasso.get().load(user.profileImageUrl).into(viewHolder.itemView.ProfileImage_View)
}

override fun getLayout(): Int {
return R.layout.userinfo
}
}


I wanted to make a snapshot from my current user like this, every time I put the $userkey in my ref value the app crashes with this error:




2018-11-23 22:04:48.414 3871-3871/my.app E/RecyclerView: No adapter attached; skipping layout
2018-11-23 22:04:49.797 3871-3871/my.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: nobrand.ljb.musicshare, PID: 3871
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type myapp.User




When I didn't put the $userkey in my ref value. The app functions as normal put displays all users as expected, but as I said I don't want that.
I'll be thankful for every answer and suggestion!










share|improve this question





























    0














    private fun getUserInfo() {


    val userkey = FirebaseAuth.getInstance().uid ?: ""
    val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
    ref.addListenerForSingleValueEvent(object: ValueEventListener {

    override fun onDataChange(p0: DataSnapshot) {
    val adapter = GroupAdapter<ViewHolder>()

    p0.children.forEach {
    Log.d("getUserInfo", it.toString())
    val user = it.getValue(User::class.java)
    if (user != null) {
    adapter.add(UserItem(user))
    }
    }

    UserInfo_RCView.adapter = adapter
    }

    override fun onCancelled(p0: DatabaseError) {

    }
    })
    }
    }

    class UserItem(val user: User): Item<ViewHolder>() {
    override fun bind(viewHolder: ViewHolder, position: Int) {
    viewHolder.itemView.UsernameSettings_txt.text = user.username

    Picasso.get().load(user.profileImageUrl).into(viewHolder.itemView.ProfileImage_View)
    }

    override fun getLayout(): Int {
    return R.layout.userinfo
    }
    }


    I wanted to make a snapshot from my current user like this, every time I put the $userkey in my ref value the app crashes with this error:




    2018-11-23 22:04:48.414 3871-3871/my.app E/RecyclerView: No adapter attached; skipping layout
    2018-11-23 22:04:49.797 3871-3871/my.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: nobrand.ljb.musicshare, PID: 3871
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type myapp.User




    When I didn't put the $userkey in my ref value. The app functions as normal put displays all users as expected, but as I said I don't want that.
    I'll be thankful for every answer and suggestion!










    share|improve this question



























      0












      0








      0


      0





      private fun getUserInfo() {


      val userkey = FirebaseAuth.getInstance().uid ?: ""
      val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
      ref.addListenerForSingleValueEvent(object: ValueEventListener {

      override fun onDataChange(p0: DataSnapshot) {
      val adapter = GroupAdapter<ViewHolder>()

      p0.children.forEach {
      Log.d("getUserInfo", it.toString())
      val user = it.getValue(User::class.java)
      if (user != null) {
      adapter.add(UserItem(user))
      }
      }

      UserInfo_RCView.adapter = adapter
      }

      override fun onCancelled(p0: DatabaseError) {

      }
      })
      }
      }

      class UserItem(val user: User): Item<ViewHolder>() {
      override fun bind(viewHolder: ViewHolder, position: Int) {
      viewHolder.itemView.UsernameSettings_txt.text = user.username

      Picasso.get().load(user.profileImageUrl).into(viewHolder.itemView.ProfileImage_View)
      }

      override fun getLayout(): Int {
      return R.layout.userinfo
      }
      }


      I wanted to make a snapshot from my current user like this, every time I put the $userkey in my ref value the app crashes with this error:




      2018-11-23 22:04:48.414 3871-3871/my.app E/RecyclerView: No adapter attached; skipping layout
      2018-11-23 22:04:49.797 3871-3871/my.app E/AndroidRuntime: FATAL EXCEPTION: main
      Process: nobrand.ljb.musicshare, PID: 3871
      com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type myapp.User




      When I didn't put the $userkey in my ref value. The app functions as normal put displays all users as expected, but as I said I don't want that.
      I'll be thankful for every answer and suggestion!










      share|improve this question















      private fun getUserInfo() {


      val userkey = FirebaseAuth.getInstance().uid ?: ""
      val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
      ref.addListenerForSingleValueEvent(object: ValueEventListener {

      override fun onDataChange(p0: DataSnapshot) {
      val adapter = GroupAdapter<ViewHolder>()

      p0.children.forEach {
      Log.d("getUserInfo", it.toString())
      val user = it.getValue(User::class.java)
      if (user != null) {
      adapter.add(UserItem(user))
      }
      }

      UserInfo_RCView.adapter = adapter
      }

      override fun onCancelled(p0: DatabaseError) {

      }
      })
      }
      }

      class UserItem(val user: User): Item<ViewHolder>() {
      override fun bind(viewHolder: ViewHolder, position: Int) {
      viewHolder.itemView.UsernameSettings_txt.text = user.username

      Picasso.get().load(user.profileImageUrl).into(viewHolder.itemView.ProfileImage_View)
      }

      override fun getLayout(): Int {
      return R.layout.userinfo
      }
      }


      I wanted to make a snapshot from my current user like this, every time I put the $userkey in my ref value the app crashes with this error:




      2018-11-23 22:04:48.414 3871-3871/my.app E/RecyclerView: No adapter attached; skipping layout
      2018-11-23 22:04:49.797 3871-3871/my.app E/AndroidRuntime: FATAL EXCEPTION: main
      Process: nobrand.ljb.musicshare, PID: 3871
      com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type myapp.User




      When I didn't put the $userkey in my ref value. The app functions as normal put displays all users as expected, but as I said I don't want that.
      I'll be thankful for every answer and suggestion!







      android firebase firebase-realtime-database kotlin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 14:36









      Jayson Minard

      38.1k15107170




      38.1k15107170










      asked Nov 23 '18 at 21:21









      Just A Normal GuyJust A Normal Guy

      277




      277
























          1 Answer
          1






          active

          oldest

          votes


















          1














          If you directly access the correct child node, the loop in your code is not needed anymore.



          This means there are two simple ways to do this:




          1. Query by key

          2. Remove the loop


          I'd recommend removing the loop as there's really no need for it anymore, but wanted to show both options.



          Query by key



          val ref = FirebaseDatabase.getInstance().getReference("/users")
          ref.orderByKey().equalTo(userkey).addListenerForSingleValueEvent(object: ValueEventListener {

          override fun onDataChange(p0: DataSnapshot) {
          val adapter = GroupAdapter<ViewHolder>()

          p0.children.forEach {
          Log.d("getUserInfo", it.toString())
          val user = it.getValue(User::class.java)
          if (user != null) {
          adapter.add(UserItem(user))
          }
          }

          UserInfo_RCView.adapter = adapter
          }

          override fun onCancelled(p0: DatabaseError) {
          throw p0.toException();
          }
          })


          Remove the loop



          val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
          ref.addListenerForSingleValueEvent(object: ValueEventListener {

          override fun onDataChange(p0: DataSnapshot) {
          val adapter = GroupAdapter<ViewHolder>()

          val user = p0.getValue(User::class.java)
          if (user != null) {
          adapter.add(UserItem(user))
          }

          UserInfo_RCView.adapter = adapter
          }

          override fun onCancelled(p0: DatabaseError) {
          throw p0.toException();
          }
          })





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453055%2fhow-to-get-firebasedatabase-snapshot-from-current-user%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            If you directly access the correct child node, the loop in your code is not needed anymore.



            This means there are two simple ways to do this:




            1. Query by key

            2. Remove the loop


            I'd recommend removing the loop as there's really no need for it anymore, but wanted to show both options.



            Query by key



            val ref = FirebaseDatabase.getInstance().getReference("/users")
            ref.orderByKey().equalTo(userkey).addListenerForSingleValueEvent(object: ValueEventListener {

            override fun onDataChange(p0: DataSnapshot) {
            val adapter = GroupAdapter<ViewHolder>()

            p0.children.forEach {
            Log.d("getUserInfo", it.toString())
            val user = it.getValue(User::class.java)
            if (user != null) {
            adapter.add(UserItem(user))
            }
            }

            UserInfo_RCView.adapter = adapter
            }

            override fun onCancelled(p0: DatabaseError) {
            throw p0.toException();
            }
            })


            Remove the loop



            val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
            ref.addListenerForSingleValueEvent(object: ValueEventListener {

            override fun onDataChange(p0: DataSnapshot) {
            val adapter = GroupAdapter<ViewHolder>()

            val user = p0.getValue(User::class.java)
            if (user != null) {
            adapter.add(UserItem(user))
            }

            UserInfo_RCView.adapter = adapter
            }

            override fun onCancelled(p0: DatabaseError) {
            throw p0.toException();
            }
            })





            share|improve this answer


























              1














              If you directly access the correct child node, the loop in your code is not needed anymore.



              This means there are two simple ways to do this:




              1. Query by key

              2. Remove the loop


              I'd recommend removing the loop as there's really no need for it anymore, but wanted to show both options.



              Query by key



              val ref = FirebaseDatabase.getInstance().getReference("/users")
              ref.orderByKey().equalTo(userkey).addListenerForSingleValueEvent(object: ValueEventListener {

              override fun onDataChange(p0: DataSnapshot) {
              val adapter = GroupAdapter<ViewHolder>()

              p0.children.forEach {
              Log.d("getUserInfo", it.toString())
              val user = it.getValue(User::class.java)
              if (user != null) {
              adapter.add(UserItem(user))
              }
              }

              UserInfo_RCView.adapter = adapter
              }

              override fun onCancelled(p0: DatabaseError) {
              throw p0.toException();
              }
              })


              Remove the loop



              val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
              ref.addListenerForSingleValueEvent(object: ValueEventListener {

              override fun onDataChange(p0: DataSnapshot) {
              val adapter = GroupAdapter<ViewHolder>()

              val user = p0.getValue(User::class.java)
              if (user != null) {
              adapter.add(UserItem(user))
              }

              UserInfo_RCView.adapter = adapter
              }

              override fun onCancelled(p0: DatabaseError) {
              throw p0.toException();
              }
              })





              share|improve this answer
























                1












                1








                1






                If you directly access the correct child node, the loop in your code is not needed anymore.



                This means there are two simple ways to do this:




                1. Query by key

                2. Remove the loop


                I'd recommend removing the loop as there's really no need for it anymore, but wanted to show both options.



                Query by key



                val ref = FirebaseDatabase.getInstance().getReference("/users")
                ref.orderByKey().equalTo(userkey).addListenerForSingleValueEvent(object: ValueEventListener {

                override fun onDataChange(p0: DataSnapshot) {
                val adapter = GroupAdapter<ViewHolder>()

                p0.children.forEach {
                Log.d("getUserInfo", it.toString())
                val user = it.getValue(User::class.java)
                if (user != null) {
                adapter.add(UserItem(user))
                }
                }

                UserInfo_RCView.adapter = adapter
                }

                override fun onCancelled(p0: DatabaseError) {
                throw p0.toException();
                }
                })


                Remove the loop



                val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
                ref.addListenerForSingleValueEvent(object: ValueEventListener {

                override fun onDataChange(p0: DataSnapshot) {
                val adapter = GroupAdapter<ViewHolder>()

                val user = p0.getValue(User::class.java)
                if (user != null) {
                adapter.add(UserItem(user))
                }

                UserInfo_RCView.adapter = adapter
                }

                override fun onCancelled(p0: DatabaseError) {
                throw p0.toException();
                }
                })





                share|improve this answer












                If you directly access the correct child node, the loop in your code is not needed anymore.



                This means there are two simple ways to do this:




                1. Query by key

                2. Remove the loop


                I'd recommend removing the loop as there's really no need for it anymore, but wanted to show both options.



                Query by key



                val ref = FirebaseDatabase.getInstance().getReference("/users")
                ref.orderByKey().equalTo(userkey).addListenerForSingleValueEvent(object: ValueEventListener {

                override fun onDataChange(p0: DataSnapshot) {
                val adapter = GroupAdapter<ViewHolder>()

                p0.children.forEach {
                Log.d("getUserInfo", it.toString())
                val user = it.getValue(User::class.java)
                if (user != null) {
                adapter.add(UserItem(user))
                }
                }

                UserInfo_RCView.adapter = adapter
                }

                override fun onCancelled(p0: DatabaseError) {
                throw p0.toException();
                }
                })


                Remove the loop



                val ref = FirebaseDatabase.getInstance().getReference("/users/$userkey")
                ref.addListenerForSingleValueEvent(object: ValueEventListener {

                override fun onDataChange(p0: DataSnapshot) {
                val adapter = GroupAdapter<ViewHolder>()

                val user = p0.getValue(User::class.java)
                if (user != null) {
                adapter.add(UserItem(user))
                }

                UserInfo_RCView.adapter = adapter
                }

                override fun onCancelled(p0: DatabaseError) {
                throw p0.toException();
                }
                })






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 21:45









                Frank van PuffelenFrank van Puffelen

                228k28374398




                228k28374398






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453055%2fhow-to-get-firebasedatabase-snapshot-from-current-user%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)