0

I am using FirebaseUI to get some values from my real-time database to Firebase RecyclerView. So.. my data looks like that:

users:
      userid:
            info:
                 Name:Muhammad

I don't know how to get the value of Name which means what exactly should I do in the Users class? Here is my code (I think that the problem is in class user, I just don't know how to access child info)

public class User {
    private String name;
    private String email;
    private String state;
    private String image;
    private String thumbnail;

    public User(String name, String state, String image) {
        this.name = name;
        this.state = state;
        this.image = image;
    }

    public User() {
    }

    public String getName() {
        return name;
    }

    public String getState() {
        return state;
    }

    public String getImage() {
        return image;
    }
}

my Main Activity myDB refer to and on start method (updated after SUPERCILEX comment )

mDb = FirebaseDatabase.getInstance().getReference().child(App_Constants.USERS_COLUMN);



 @Override
    protected void onStart() {
        super.onStart();

        Query query = mDb;

        FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>()
                .setQuery(query, new SnapshotParser<User>() {
                    @NonNull
                    @Override
                    public User parseSnapshot(@NonNull DataSnapshot snapshot) {
                        String Name = snapshot.child(App_Constants.INFO_COLUMN).child(App_Constants.NAME_COLUMN).getValue().toString();
                        String State = snapshot.child(App_Constants.INFO_COLUMN).child(App_Constants.STATE_COLUMN).getValue().toString();
                        String Image = snapshot.child(App_Constants.INFO_COLUMN).child(App_Constants.IMAGE_COLUMN).getValue().toString();
                        User user = new User(Name,State,Image);

                        return user;
                    }
                }).build();

        FirebaseRecyclerAdapter<User,Users_ViewHolder> adapter = new FirebaseRecyclerAdapter<User, Users_ViewHolder>(options) {
            @NonNull
            @Override
            public Users_ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_user,parent,false);
                return new Users_ViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(@NonNull Users_ViewHolder holder, int position, @NonNull User model) {

                holder.Set_Name(model.getName());
                holder.Set_Image(model.getImage());
                holder.Set_State(model.getState());
            }
        };
        mUsers.setAdapter(adapter);
    }

my ViewHolder

public class Users_ViewHolder extends RecyclerView.ViewHolder{

    View mView;
    public Users_ViewHolder(View itemView) {
        super(itemView);

        mView = itemView;
    }

    public void Set_Name(String Name)
    {
        TextView mName = mView.findViewById(R.id.tv_single_user_name);
        mName.setText(Name);
    }


    public void Set_Image(String url)
    {
        CircleImageView mImage = mView.findViewById(R.id.iv_single_user);
        Picasso.get().load(url).placeholder(R.drawable.profile).into(mImage);
    }


    public void Set_State(String State)
    {
        TextView mState = mView.findViewById(R.id.tv_single_user_state);
        mState.setText(State);
    }
}

thanks

  • 1
    **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo May 30 '18 at 05:27
  • mohamed what do you get when you call the `getName()` method? –  May 30 '18 at 05:43
  • thanks and sorry for late ... Alex mamo the problem still in how can i access the info child... the data is in info child i cant access it ... @eminem its give me the number of fields equal the number of users and thats right but the name and state and image still null its cant access it – mohamed ebraheem May 30 '18 at 23:43
  • @AlexMamo any idea about this one? : https://stackoverflow.com/questions/56159855/how-do-i-make-my-button-which-is-fixed-at-the-bottom-of-a-layout-scroll-once-th?noredirect=1#com –  May 30 '19 at 22:01
  • @AngelaHeely I'll take a look and if I'll know the answer, I'll write it to you. – Alex Mamo May 31 '19 at 12:10

2 Answers2

0

I believe it'll come in as a Map<String, Object> on the field info. However, you can always use a custom SnapshotParser to build your model to your liking: https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-the-firebaserecycleradapter.

SUPERCILEX
  • 3,929
  • 4
  • 32
  • 61
  • i did what is in the documentation and still nothing happened but this time give me ablank page (updated the post) – mohamed ebraheem May 31 '18 at 04:43
  • `snapshot.child("info").child("Name").getValue()` should work. As for the state, you'll want to get that off the root: `snapshot.child("state").getValue()`. Make sure your capitalization is correct since the RTDB is case sensitive. – SUPERCILEX May 31 '18 at 16:40
  • thank u ... and for this reasons iam using a constant variables and iam sure that every thing is well (case sensitive and the letter),, but the problem is i cant access the info field ... – mohamed ebraheem Jun 01 '18 at 12:17
0

Some Advices :would you mind changing the set methods names? from Set_State to setState,I too had similar problems please respect Java naming conventions

Also add the set methods in your custom model

Second:

 FirebaseRecyclerOptions<User> options =
                new FirebaseRecyclerOptions.Builder<User>()
                        .setQuery(query, User.class)
                        .build();

Start Listening:

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

stopListening() call removes the event listener and all data in the adapter

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}

Here you have a very good explained example

  • thank you .. i did like what u said but when i did that .setQuery(query, User.class) its gave me this ..https://ibb.co/b6DmSd... and thats because the name and state and image are in the info field not userid field .. so how can i access to info field – mohamed ebraheem Jun 01 '18 at 12:49
  • the best solution is to remove the `info` I think its redundant,however using the model above it would be : `DatabaseReference keyRef = FirebaseDatabase.getInstance().getReference().child("userid").child(info);` –  Jun 01 '18 at 13:29