Display filtered Firebase Realtime Database in RecyclerView [duplicate]










0
















This question already has an answer here:



  • How to return DataSnapshot value as a result of a method?

    3 answers



I am performing multiple times of filter to the data in Firebase Realtime Database. For example, I first filter for people less than age 20, if the result count is less than 50, I will need to re-visit the database to filter again for people less than age 30, etc... Hence, I need to use addChildEventListener multiple times.



The list of people who are selected will then be displayed in a RecyclerView.



I do that using this:



adapter = new Adapter(this, getListFromFirebase());
view= findViewById(R.id.my_view);
view.setLayoutManager(manager);
view.setAdapter(adapter);


Where getListFromFirebase() is a function that use addChildEventListener to get data from Firebase Realtime Database.



However, since Firebase Realtime Database is asynchronous, it always returns an empty list to the RecyclerView when the RecyclerView is initialized. Is there any way that I can make sure that I can successfully get the list that I want from Firebase Realtime Database before entering the RecyclerView?










share|improve this question















marked as duplicate by Grimthorr, Alex Mamo android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 12:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Take a look at this answer:stackoverflow.com/questions/53293212/…

    – PradyumanDixit
    Nov 15 '18 at 9:40











  • Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback.

    – Alex Mamo
    Nov 15 '18 at 12:19















0
















This question already has an answer here:



  • How to return DataSnapshot value as a result of a method?

    3 answers



I am performing multiple times of filter to the data in Firebase Realtime Database. For example, I first filter for people less than age 20, if the result count is less than 50, I will need to re-visit the database to filter again for people less than age 30, etc... Hence, I need to use addChildEventListener multiple times.



The list of people who are selected will then be displayed in a RecyclerView.



I do that using this:



adapter = new Adapter(this, getListFromFirebase());
view= findViewById(R.id.my_view);
view.setLayoutManager(manager);
view.setAdapter(adapter);


Where getListFromFirebase() is a function that use addChildEventListener to get data from Firebase Realtime Database.



However, since Firebase Realtime Database is asynchronous, it always returns an empty list to the RecyclerView when the RecyclerView is initialized. Is there any way that I can make sure that I can successfully get the list that I want from Firebase Realtime Database before entering the RecyclerView?










share|improve this question















marked as duplicate by Grimthorr, Alex Mamo android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 12:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Take a look at this answer:stackoverflow.com/questions/53293212/…

    – PradyumanDixit
    Nov 15 '18 at 9:40











  • Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback.

    – Alex Mamo
    Nov 15 '18 at 12:19













0












0








0









This question already has an answer here:



  • How to return DataSnapshot value as a result of a method?

    3 answers



I am performing multiple times of filter to the data in Firebase Realtime Database. For example, I first filter for people less than age 20, if the result count is less than 50, I will need to re-visit the database to filter again for people less than age 30, etc... Hence, I need to use addChildEventListener multiple times.



The list of people who are selected will then be displayed in a RecyclerView.



I do that using this:



adapter = new Adapter(this, getListFromFirebase());
view= findViewById(R.id.my_view);
view.setLayoutManager(manager);
view.setAdapter(adapter);


Where getListFromFirebase() is a function that use addChildEventListener to get data from Firebase Realtime Database.



However, since Firebase Realtime Database is asynchronous, it always returns an empty list to the RecyclerView when the RecyclerView is initialized. Is there any way that I can make sure that I can successfully get the list that I want from Firebase Realtime Database before entering the RecyclerView?










share|improve this question

















This question already has an answer here:



  • How to return DataSnapshot value as a result of a method?

    3 answers



I am performing multiple times of filter to the data in Firebase Realtime Database. For example, I first filter for people less than age 20, if the result count is less than 50, I will need to re-visit the database to filter again for people less than age 30, etc... Hence, I need to use addChildEventListener multiple times.



The list of people who are selected will then be displayed in a RecyclerView.



I do that using this:



adapter = new Adapter(this, getListFromFirebase());
view= findViewById(R.id.my_view);
view.setLayoutManager(manager);
view.setAdapter(adapter);


Where getListFromFirebase() is a function that use addChildEventListener to get data from Firebase Realtime Database.



However, since Firebase Realtime Database is asynchronous, it always returns an empty list to the RecyclerView when the RecyclerView is initialized. Is there any way that I can make sure that I can successfully get the list that I want from Firebase Realtime Database before entering the RecyclerView?





This question already has an answer here:



  • How to return DataSnapshot value as a result of a method?

    3 answers







java android firebase firebase-realtime-database android-recyclerview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:31









Grimthorr

4,48442237




4,48442237










asked Nov 15 '18 at 9:14









mumumumu

313




313




marked as duplicate by Grimthorr, Alex Mamo android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 12:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Grimthorr, Alex Mamo android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 12:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Take a look at this answer:stackoverflow.com/questions/53293212/…

    – PradyumanDixit
    Nov 15 '18 at 9:40











  • Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback.

    – Alex Mamo
    Nov 15 '18 at 12:19

















  • Take a look at this answer:stackoverflow.com/questions/53293212/…

    – PradyumanDixit
    Nov 15 '18 at 9:40











  • Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback.

    – Alex Mamo
    Nov 15 '18 at 12:19
















Take a look at this answer:stackoverflow.com/questions/53293212/…

– PradyumanDixit
Nov 15 '18 at 9:40





Take a look at this answer:stackoverflow.com/questions/53293212/…

– PradyumanDixit
Nov 15 '18 at 9:40













Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback.

– Alex Mamo
Nov 15 '18 at 12:19





Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback.

– Alex Mamo
Nov 15 '18 at 12:19












1 Answer
1






active

oldest

votes


















0














Your method getListFromFirebase() will always return null, because of the firebase async task. Instead create a new method where you notifyAdapter, which you can call from onChildAdded method.



Try this,



Declare a variable:



ArrayList<YourModel> list = new ArrayList<>();


Pass it to adapter even if it is empty:



adapter = new Adapter(this, list);
view= findViewById(R.id.my_view);
view.setLayoutManager(manager);
view.setAdapter(adapter);
getListFromFirebase(); // Dont return anything from this method. declare it void


void getListFromFirebase()
//firebaselistener task
onChildAdded()

YourModel yourModel = dataSnapshot.getValue();
mAdapter.addNewData(yourModel); //create a new method to update recyclerview Adapter




Adapter:



void addNewData(YourModel newData)
list.add(newData); //assign the list value to adapter list
notifyAdapterChange();






share|improve this answer

























  • Thanks for your help! However, it still doesnt show anything in the recycler view. Can I know why? Is there anything I missed out?

    – mumu
    Nov 16 '18 at 6:05











  • try to print yourModel data in your addNewData method to check whether your data is getting passed. I have also updated the code, try again and let us know.

    – ManishPrajapati
    Nov 16 '18 at 6:31

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your method getListFromFirebase() will always return null, because of the firebase async task. Instead create a new method where you notifyAdapter, which you can call from onChildAdded method.



Try this,



Declare a variable:



ArrayList<YourModel> list = new ArrayList<>();


Pass it to adapter even if it is empty:



adapter = new Adapter(this, list);
view= findViewById(R.id.my_view);
view.setLayoutManager(manager);
view.setAdapter(adapter);
getListFromFirebase(); // Dont return anything from this method. declare it void


void getListFromFirebase()
//firebaselistener task
onChildAdded()

YourModel yourModel = dataSnapshot.getValue();
mAdapter.addNewData(yourModel); //create a new method to update recyclerview Adapter




Adapter:



void addNewData(YourModel newData)
list.add(newData); //assign the list value to adapter list
notifyAdapterChange();






share|improve this answer

























  • Thanks for your help! However, it still doesnt show anything in the recycler view. Can I know why? Is there anything I missed out?

    – mumu
    Nov 16 '18 at 6:05











  • try to print yourModel data in your addNewData method to check whether your data is getting passed. I have also updated the code, try again and let us know.

    – ManishPrajapati
    Nov 16 '18 at 6:31















0














Your method getListFromFirebase() will always return null, because of the firebase async task. Instead create a new method where you notifyAdapter, which you can call from onChildAdded method.



Try this,



Declare a variable:



ArrayList<YourModel> list = new ArrayList<>();


Pass it to adapter even if it is empty:



adapter = new Adapter(this, list);
view= findViewById(R.id.my_view);
view.setLayoutManager(manager);
view.setAdapter(adapter);
getListFromFirebase(); // Dont return anything from this method. declare it void


void getListFromFirebase()
//firebaselistener task
onChildAdded()

YourModel yourModel = dataSnapshot.getValue();
mAdapter.addNewData(yourModel); //create a new method to update recyclerview Adapter




Adapter:



void addNewData(YourModel newData)
list.add(newData); //assign the list value to adapter list
notifyAdapterChange();






share|improve this answer

























  • Thanks for your help! However, it still doesnt show anything in the recycler view. Can I know why? Is there anything I missed out?

    – mumu
    Nov 16 '18 at 6:05











  • try to print yourModel data in your addNewData method to check whether your data is getting passed. I have also updated the code, try again and let us know.

    – ManishPrajapati
    Nov 16 '18 at 6:31













0












0








0







Your method getListFromFirebase() will always return null, because of the firebase async task. Instead create a new method where you notifyAdapter, which you can call from onChildAdded method.



Try this,



Declare a variable:



ArrayList<YourModel> list = new ArrayList<>();


Pass it to adapter even if it is empty:



adapter = new Adapter(this, list);
view= findViewById(R.id.my_view);
view.setLayoutManager(manager);
view.setAdapter(adapter);
getListFromFirebase(); // Dont return anything from this method. declare it void


void getListFromFirebase()
//firebaselistener task
onChildAdded()

YourModel yourModel = dataSnapshot.getValue();
mAdapter.addNewData(yourModel); //create a new method to update recyclerview Adapter




Adapter:



void addNewData(YourModel newData)
list.add(newData); //assign the list value to adapter list
notifyAdapterChange();






share|improve this answer















Your method getListFromFirebase() will always return null, because of the firebase async task. Instead create a new method where you notifyAdapter, which you can call from onChildAdded method.



Try this,



Declare a variable:



ArrayList<YourModel> list = new ArrayList<>();


Pass it to adapter even if it is empty:



adapter = new Adapter(this, list);
view= findViewById(R.id.my_view);
view.setLayoutManager(manager);
view.setAdapter(adapter);
getListFromFirebase(); // Dont return anything from this method. declare it void


void getListFromFirebase()
//firebaselistener task
onChildAdded()

YourModel yourModel = dataSnapshot.getValue();
mAdapter.addNewData(yourModel); //create a new method to update recyclerview Adapter




Adapter:



void addNewData(YourModel newData)
list.add(newData); //assign the list value to adapter list
notifyAdapterChange();







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 6:28

























answered Nov 15 '18 at 9:40









ManishPrajapatiManishPrajapati

221315




221315












  • Thanks for your help! However, it still doesnt show anything in the recycler view. Can I know why? Is there anything I missed out?

    – mumu
    Nov 16 '18 at 6:05











  • try to print yourModel data in your addNewData method to check whether your data is getting passed. I have also updated the code, try again and let us know.

    – ManishPrajapati
    Nov 16 '18 at 6:31

















  • Thanks for your help! However, it still doesnt show anything in the recycler view. Can I know why? Is there anything I missed out?

    – mumu
    Nov 16 '18 at 6:05











  • try to print yourModel data in your addNewData method to check whether your data is getting passed. I have also updated the code, try again and let us know.

    – ManishPrajapati
    Nov 16 '18 at 6:31
















Thanks for your help! However, it still doesnt show anything in the recycler view. Can I know why? Is there anything I missed out?

– mumu
Nov 16 '18 at 6:05





Thanks for your help! However, it still doesnt show anything in the recycler view. Can I know why? Is there anything I missed out?

– mumu
Nov 16 '18 at 6:05













try to print yourModel data in your addNewData method to check whether your data is getting passed. I have also updated the code, try again and let us know.

– ManishPrajapati
Nov 16 '18 at 6:31





try to print yourModel data in your addNewData method to check whether your data is getting passed. I have also updated the code, try again and let us know.

– ManishPrajapati
Nov 16 '18 at 6:31





這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands