How to populate RecyclerView with data from Firebase Database using FirebaseUI?










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.










share|improve this question



















  • 1





    look at the samples github.com/firebase/FirebaseUI-Android

    – Har Kal
    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 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















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.










share|improve this question



















  • 1





    look at the samples github.com/firebase/FirebaseUI-Android

    – Har Kal
    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 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












0








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.










share|improve this question
















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.







java android firebase firebase-realtime-database firebaseui






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 22:43









Frank van Puffelen

240k29384411




240k29384411










asked Nov 15 '18 at 22:26









Peter RuppertPeter Ruppert

231114




231114







  • 1





    look at the samples github.com/firebase/FirebaseUI-Android

    – Har Kal
    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 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












  • 1





    look at the samples github.com/firebase/FirebaseUI-Android

    – Har Kal
    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 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







1




1





look at the samples github.com/firebase/FirebaseUI-Android

– Har Kal
Nov 15 '18 at 22:29





look at the samples github.com/firebase/FirebaseUI-Android

– Har Kal
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





@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 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





This 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






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328750%2fhow-to-populate-recyclerview-with-data-from-firebase-database-using-firebaseui%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328750%2fhow-to-populate-recyclerview-with-data-from-firebase-database-using-firebaseui%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin