Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Depending on Data on Firebase - build buttons including Images and Data

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

like image 352
dunkiero Avatar asked Aug 24 '18 06:08

dunkiero


1 Answers

I would recommend using recycler view and implementing onClick for recycler view. This would take a lot of code:

  1. Create xml for each item of recycler view

recycler_view_item.xml

  1. Add recycler view to xml of current activity

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;
}

}

  1. Create adapter and view holder class

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

like image 114
sammy Avatar answered Nov 09 '22 05:11

sammy