0

I am struggling to populate my recycler view and I can't identify why. I trying to populate the view with data from Firebase Database, I can successfully get the data, but I can't figure out how to properly set up the recycler view, I am also using FirebaseUI. Here is my Activity containing the firebaseUI and database call:

public class LoggedInActivity extends AppCompatActivity {

private DatabaseReference mDatabase;

RecyclerView contentView;

private TextView mTextMessage;
        ImageButton positive;
        ImageButton balanced;
        ImageButton negative;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                mTextMessage.setText(R.string.title_home);

                return true;
            case R.id.navigation_dashboard:
                mTextMessage.setText(R.string.title_dashboard);

                return true;
            case R.id.navigation_notifications:
                mTextMessage.setText(R.string.title_notifications);

                return true;
        }
        return false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_logged_in);

    positive = findViewById(R.id.heartButton);
    balanced = findViewById(R.id.balancedButton);
    negative = findViewById(R.id.negativeButton);

    mTextMessage = findViewById(R.id.message);
    mTextMessage.setTextColor(Color.WHITE);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    mDatabase = FirebaseDatabase.getInstance().getReference();

    DatabaseReference mRef = mDatabase.child("posts");
    Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("posts")
            .limitToLast(50);

    query.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
            Post newPost = dataSnapshot.getValue(Post.class);
            System.out.println("Title: " + newPost.title);
            System.out.println("Body: " + newPost.body);
            System.out.println("Previous Post ID: " + prevChildKey);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {}

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });


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

    FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Post, PostHolder>(options) {
        @Override
        public PostHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Create a new instance of the ViewHolder, in this case we are using a custom
            // layout called R.layout.message for each item
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.post_content, parent, false);

            return new PostHolder(view);
        }

        @Override
        protected void onBindViewHolder(PostHolder holder, int position, Post model) {
            // Bind the Chat object to the ChatHolder
            // ...
           holder.bind


        }
    };

    contentView = findViewById(R.id.recyclerView);
    contentView.setAdapter(adapter);

}

}

I also created my class for retrieving Posts from the database:

@IgnoreExtraProperties
public class Post {

public String title;
public String body;
public String username;

public Post() {
    // Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public Post(String title, String body, String username) {
    title = title;
    body = body;
    username = username;
}

public String getTitle() { return title; }

public String getBody() { return body; }

public String getUsername() { return username; }

}

I am just trying to get the post Title at the moment, I have made this custom holder:

public class PostHolder extends RecyclerView.ViewHolder {

TextView title;
TextView body;
TextView username;

public PostHolder(@NonNull View itemView) {
    super(itemView);
    title = (TextView) itemView.findViewById(R.id.title)
}

}

Here is where I think I run into the problem, I am not sure how I bind the Post data to get it to show up on this Label that I created as post_content.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"/>

 </LinearLayout>

I would appreciate an explanation on this system as I am finding it a bit confusing in regards to the custom holder, binding and inflating portions. Where am I going wrong? Thank you.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Peter Ruppert
  • 1,067
  • 9
  • 24
  • 1
    look at the samples https://github.com/firebase/FirebaseUI-Android – Harkal Nov 15 '18 at 22:29
  • @HarKal I have been following that but I have gotten stuck trying to find what I'm getting wrong now, mainly with the actual binding of the post class data to the recycler – Peter Ruppert Nov 16 '18 at 11:31
  • **[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 Nov 16 '18 at 13:03

0 Answers0