I'm quite new to programming and was looking quite a while now for a solution - but as I am not sure what exactly I am looking for, I decided to ask you.
Depending on the Data on Firebase I was looking to build a button including text and an image.
So for example I have a Database including:
User
Max (with the fields: University, Age, City)
Lena (with the fields: University, Age, City)
Is it possible to build a button with this Data but only if the Users are in the Database? So as there are 2 users - build 2 buttons including all the text (University, Age, City), if there would be 3 users - build 3 buttons.
Edit: Using Android Studio
I would recommend using recycler view and implementing onClick for recycler view. This would take a lot of code:
recycler_view_item.xml
3.Create a class MyUser.java like below:
public class MyUser {
private String University, City, Age;
public User(){}
public User(String University, String City) {
this.University = University;
this.City = City;
this.Age = Age;
}
public String getUniversity() { return University;}
public void setUniversity(String University) {
this.University = University;
}
public String getCity() {
return City;
}
public void setCity(String City) {
this.City = City;
}
public String getAge() {
return Age;
}
public void setAge(String Age) {
this.Age = Age;
}
}
5.get user details from database in current activity :
private List<Book> userList;
userList = new ArrayList<>();
mRef = database.getReference().child("users");
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> bookData = dataSnapshot.getChildren();
for(DataSnapshot d : bookData){
MyUser myUser = d.getValue(User.class);
userList.add(myUser);
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Initialise the adapter with context and the userList
mAdapter = new MyRecyclerAdapter(getContext(), userList);
I have left out a lot of things like the adapter and view holder. comment if u are stuck
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With