How to get FirebaseDataBase Snapshot from current user?
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
add a comment |
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
add a comment |
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
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
android firebase firebase-realtime-database kotlin
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
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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:
- Query by key
- 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();
}
})
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- Query by key
- 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();
}
})
add a comment |
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:
- Query by key
- 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();
}
})
add a comment |
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:
- Query by key
- 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();
}
})
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:
- Query by key
- 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();
}
})
answered Nov 23 '18 at 21:45
Frank van PuffelenFrank van Puffelen
228k28374398
228k28374398
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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