Android studio Firebase how to show other users information? [closed] - android

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to show a profile page of other users with their data. I have data stored in the Realtime Database. I can load my own data but have do I do that for specific users and show it?
For example the current user I use the following code to get the datasnapshot
firebaseDatabase.getReference("users").child(firebaseAuth.currentUser!!.uid)

Not sure how you database is structured but you should be ale to do something like this:
val userRef = database.getReference("users")
userRef.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val data = snapshot.value as? Map<String, *>?
// data is a map of { uid to User }
}
override fun onCancelled(error: DatabaseError) {
Log.e(TAG, "Loading user data failed: \n" + error.message)
}
})
Basically do it the same way you get the individual user, but pull from the parent node to get all of them in the form of a map

Related

Make buttons disappear due to user roles (kotlin) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Is it possible to make a button disappear in my app when a user is a viewer? I have 3 types of users (viewer, editor and admin) and I want the same layout for the 3 of them but, the viewer cannot have access to some buttons. Is it possible to make the buttons disappear or should I make the exact same layouts but without the buttons? Thanks!
edit
here's a screenshot of firebase database
The roles are stored in realtime database so you would have to first read that and then hide the buttons based on the role. Right now you are comparing the UID of user with the role "Viewer" - if(FirebaseAuth.getInstance().currentUser!!.uid == "Viewer")
val user = Firebase.auth.currentUser
val uid = user.uid
mDatabase.child("users").child(uid).child("Role").get().addOnSuccessListener {
Log.i("firebase", "Got role ${it.value}")
if (it.value == "Viewer") {
newDashButton.visibility = View.GONE
}
}.addOnFailureListener{
Log.e("firebase", "Error getting data", it)
}

How to read nested arrays and maps in Firestore? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to read nested array and maps in android studio (Java) from firestore but could not able to iterate through it. I am new to it so can anyone please help me. Thank you :)
Edited: How can I iterate through "options" as it is an array in map.
I've achieved the rest by saving them in HashMap and iterating through it.
First, make a data class for your JSON. You can do it with this plugin
Let's say you have something like that (this example done in kotlin):
data class Data(
val id: Int,
val lists: List<Company>,
val name: String
)
data class Company(
val q: String,
val type: String,
val optType: Int?,
val data: List<Options>?
)
data class Options(
val option : String
)
After that, you can do a loop following
for (Company list : data.getLists()) { // data is your object. GetList returns first array
for (Options datum : list.getData()) { // from list you will get next array.
System.out.println(datum.getOption()); // here you will get item from nested array
}
}

Firebase Android showing and modify element per single User [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm making this android app using Firebase where I have some books and every user user can set them as read or reading.
I don't know how to do it since Firebase is noSQL and there are no Relations between entities.
There are these three activities: in the first one the user can see all the books and eventually set one as read or reading, in the second there are only books marked ad read and in the third the books marked as reading.
They must be separated, every user has its own.
How can I do this using Firebase realtime database and storage?
I'd likely create two top-level nodes, for the two types that you have:
Users
Books
And then create two additional top-level nodes for the relations between these:
UserBooks - which is where we tie each user to their books, and track their reading status.
BookUsers - which ties each book to the users. You only need this if you want to show the users for a specific book, but I typically recommend adding it right away.
So a simple model like this could look like:
Users: {
"uidOfFio": { ... },
"uidOfPuf": { ... }
},
Books: {
"idOfBook1": { ... },
"idOfBook2": { ... }
},
UserBooks: {
"uidOfFio": {
"idOfBook1": "Read",
"idOfBook2": "Reading"
},
"uidOfPuf": {
"idOfBook1": "Reading",
}
},
BookUsers: {
"idOfBook1": {
"uidOfFio": "Read",
"uidOfPuf": "Reading"
},
"idOfBook2": {
"uidOfFio": "Reading"
},
}
For more on this, I recommend reading:
Many to Many relationship in Firebase
Firebase query if child of child contains a value

How to insert multiple data in firebase using loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any way to insert multiple data in firebase using loop in android studio.
As
for (DataSnapshot ds : dataSnapshot.getChildren())
{
String Name = ds.child("Name").getValue().toString();
list.add(Name);
}
DataSnapshot loop is used to get the multiple children. How can i insert multiple data using loop.
I am guessing you want to update various children of your database at once and for that you do not need to actually use a loop to insert multiple data at once in Firebase.
You may use maps for that, which can update multiple fields of your database in one go. The code for using maps, looks something like this:
Map<String,Object> taskMap = new HashMap<>();
taskMap.put("age", "12");
taskMap.put("gender", "male");
taskMap.put("name", "Someone");
taskMap.put("surname", "no-one");
reference.updateChildren(taskMap);
This depends a lot on your database structure, and you can edit it for your need, according to your database structure.
To know more about, how to update database using maps, go through the following links:
Link1
and
Link2.
Also, if you could make the question a bit more precise, you can get more help from here.

display data of specific/current/ logged in user in firebase cloud [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
anyone can you give your opinion about this kind structure in Firestore?
I want to display the data of current logged in user.
Let's say i have some users in my app:
I want the Query like that firebaseFirestore.collection("Complaints").document(user_id).addSnapshotListener(new EventListener()
This is my code:
firebaseFirestore.collection("Complaints").addSnapshotListener(new EventListener() {
#Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for (DocumentChange doc: documentSnapshots.getDocumentChanges())
{
if (doc.getType() == DocumentChange.Type.ADDED)
{
Complaints complaints = doc.getDocument().toObject(Complaints.class);
complaintsList.add(complaints);
complaintsRecyclerAdapter.notifyDataSetChanged();
}
}
}
});
Generally this is a good structure
Users
- userID
- User Info
- User Roles (if using role based authorization)
Other Data Collections
- Other Data Documents
- Other Sub Collections

Categories

Resources