Basically my problem is that I had stored in firebase db a list of users with multiple attributes, some of them were private informations so I wanted to deny access to them. Rules aren't the answer because they can't be used as filters.
My solution was to create two new attributes for "users": "pvt" (private) and "pb" (public) and to store inside of them the correct attributes.
To create new users I used first:
mDatabase.child("users").child(prefs.getString("id firebase", " ")).child("public").setValue(newUserPublic);
mDatabase.child("users").child(prefs.getString("id firebase", " ")).child("private").setValue(newUserPrivate);
where newUserPublic and newUserPrivate are objects of simple custom classes that offers getters and setters for user's attributes (one for public and the other for private informations).
My final goal was to create a leaderboard that uses only public attributes of each user but I wadn't able to create a proper ListAdapter with this configuration.
My final try was to create a new class called User
public class User {
public UserDataPrivate getPvt() {
return pvt;
}
public void setPvt(UserDataPrivate pvt) {
this.pvt = pvt;
}
public UserDataPublic getPb() {
return pb;
}
public void setPb(UserDataPublic pb) {
this.pb = pb;
}
private UserDataPrivate pvt;
private UserDataPublic pb;
public void setId(String id) {
this.id = id;
}
private String id;
public User(String id, UserDataPublic pb, UserDataPrivate pvt){
this.pvt=pvt;
this.pb=pb;
this.id=id;
}
}
to create new user with:
User user = new User(newUserPublic, newUserPrivate);
mDatabase.child("users").child(prefs.getString("id firebase", " ")).setValue(user);
and the current adapter is
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users");
Query query = mDatabase.child("pb").orderByChild("points");
ListAdapter adapter = new CustomFirebaseAdapter<User>(this, User.class, R.layout.list_item, query) {
#Override
protected void populateView(View v, User model, int position) {
//code that uses model.getPb()
}
};
but it doesn't work (//code is never executed).
Do you have any idea how I can solve this?
This is a test user in Firebase:
EDIT:
Tried to user pb/score inside the query but it crashes.
I think the problem is that firebase can't handle complex objects or I'm missing something. I have other code portions wich retrive single user data so I use
final DatabaseReference userDatabase = mDatabase.child("users").child(prefs.getString("id firebase", " "));
userDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
//...
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
and it crashes when setting user. I thought that if I save users in my db using user objects I could retrive them in the same way but I'm probably wrong.
When you call orderByChild() on a location, Firebase takes each node directly under that location and orders it on the child you specify. Since you call orderByChild("points") on /users, it takes the nodes under /users and orders them on their (non-existing) points property.
To order each user on their pb/points property, use that path in the call to orderByChild(). So:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users");
Query query = mDatabase.orderByChild("pb/points");
ListAdapter adapter = new CustomFirebaseAdapter<User>(this, User.class, R.layout.list_item, query) {
#Override
protected void populateView(View v, User model, int position) {
//code that uses model.getPb()
}
};
This code:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users");
Query query = mDatabase.child("pb").orderByChild("points");
will point to /users/pb and order by users/pb/??/points. If you seek for query that point to /users/?? order by users/??/pb/points, then I think Firebase can't do that for now. Check this post
You can use a firebase listview adapter and that does a lot of the work for you. But you might want to change your user objects.
Query query = FirebaseDatabase.getInstance().getReference("users").orderByChild("pb/points");
FirebaseListAdapter adapter = new FirebaseListAdapter(activity, User.class, R.id.modelLayout, query) {
#Override
protected void populateView(View view, User user, int i) {
//view is current view
//user is current user
//i is position
TextView points = view.findViewById(R.id.pointsTextView);
String userPoints = user.getPb().getPoints();
points.setText(userPoints);
}
};
Then apply the adapter on a view
listview.setAdapter(adapter);
Related
I am trying to retrieve specific data from google firebase which is specific to each user that is logged in. Basically, the data that i want to retrieve is 'field' data listed under user ID's but in a separate class on google firebase realtime database. I want to be able to show the data of each field in a 'listview' in an activity on my application but whenever i try to run it, the application just closes.
I have already tried running the application under references to the 'users' class.But this didn't work.I also tried running under the 'fields' class but this also does not work.
Here is my FarmActivity.java where I want the 'listview' of the field data to show.
public class FarmActivity extends AppCompatActivity {
ListView listView;
FirebaseDatabase database;
DatabaseReference ref;
ArrayList<String> list;
ArrayAdapter<String> adapter;
FieldInformation fieldInformation;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_farm);
listView = (ListView )findViewById(R.id.listView);
database = FirebaseDatabase.getInstance();
ref = database.getReference("Fields");
list = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, R.layout.user_field_info, R.id.userInfo, list);
fieldInformation = new FieldInformation();
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds:dataSnapshot.getChildren()){
fieldInformation = ds.getValue(FieldInformation.class);
list.add(fieldInformation.getFieldName().toString() + " " + "\n"+ fieldInformation.getCrop().toString() + " ");
}
listView.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here is my FieldInformation.java where i have specified the strings for the FarmActivity
public class FieldInformation {
public String crop;
public String fieldName;
public FieldInformation(String crop, String fieldName) {
this.crop = crop;
this.fieldName = fieldName;
}
public FieldInformation(){
}
public String getCrop() {
return crop;
}
public void setCrop(String crop) {
this.crop = crop;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
}
Your adapter should have a list in it and your adapter should have a constructor that takes a list. You initialize your adapter in FarmActivity with the list in there and remove your set adapter then set it after you initialize adapter, lastly put adapter.notifydatasetchanged(); where you where setting your adapter. Feel free to post your adapter and ill help you fix it.
As refer in this link. I want to try in my coding apps. But it didnt retrieve any data from my firebase database. I just cant figure out how to fix it. Can someone please help me. Is there any idea, where did I miss?
My firebase database as show image below:-
Spinner coding:-
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.keepSynced(true);
mDatabase.child("Advertisement").child(mAuth.getUid()).addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
//final List<String> name = new ArrayList<String>();
ArrayList<String> identity = new ArrayList<>();
identity.add(0, "Choose Tuition Centre");
for (DataSnapshot nameSnapshot: dataSnapshot.getChildren())
{
String tuitionName = nameSnapshot.child("adstuitioname").getValue(String.class);
identity.add(tuitionName);
}
ArrayAdapter<String> nameAdapter = new ArrayAdapter<String>(RatingActivity.this, android.R.layout.simple_spinner_item, identity);
nameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mName.setAdapter(nameAdapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(position==0)
{
//Toast.makeText(getApplicationContext(),"No Item Selected",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),parent.getItemAtPosition(position) +" Selected",Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
Add listener as below :
DatabaseReference mDatabaseRef =
FirebaseDatabase.getInstance().getReference();
ArrayList<String> list=new ArrayList<>();
mDatabaseRef.child("Advertisement")
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot :dataSnapshot.getChildren())
{
Advertisementject advertisement=
snapshot.getValue(Advertisement.class);
//replace Object.class with your class name
//get your key value here from your "Custom class"
// which contains "adstutioname"
// add in list
list.add(advertisement.getAdstutuioname());
}
//set adapter
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
generate class like this
public class Advertisement{
#SerializedName("adstutuioname")
#Expose
private String adstutuioname;
public String getAdstutuioname() {
return adstutuioname;
}
public void setAdstutuioname(String adstutuioname) {
this.adstutuioname = adstutuioname;
}
}
Your class contains all your params and .
Replace Object with this class
I hope the problem is with your getValue() method. Firebase getValue method returns an Object, and you are expecting a String. Please modify this line to convert your Object to String -
String tuitionName = nameSnapshot.child("adstuitioname").getValue(String.class).toString();
It's not clear if you're trying to read once, or watch changes Difference between addValueEventListener() and addListenerForSingleValueEvent() of firebase
First things first, look if you are getting a database error
#Override
public void onCancelled(DatabaseError databaseError) {
// Toast or print the databaseError, don't ignore it
}
Then look where you're adding a listener - mDatabase.child("Advertisement").child(mAuth.getUid())., which from your picture is every element at the level of -LNZ6....
For that level, you have a list of object's, so you can need to use a loop, and as shown from some examples on Firebase site, it's recommended that you make an object, but you still can use child method for a certain field as you're trying to do
But if a field doesn't exist, you get null value, and Adapters cannot hold nulls, so you must ignore or set some default value.
ArrayList<String> identity = new ArrayList<>();
identity.add(0, "Choose Tuition Centre");
for (DataSnapshot nameSnapshot: dataSnapshot.getChildren()) {
String tuitionName = nameSnapshot.child("adstuitioname").getValue(String.class);
Log.d("DEBUG TUITION", String.valueOf(tuitionName); // look for this in your logs
identity.add(tuitionName == null ? "none" : tuitionName);
}
ArrayAdapter<String> nameAdapter =...
I was trying to search through the Firebase database from my material search dialog. I was successful in doing that but the problem is that I can search only through titles(my database contains title and descriptions too), although I tried quite a many things nothing worked for me.
At some point, I succeeded by making another search function for searching descriptions but then it made the searched results for titles to misbehave.
I am attaching the code below can, please have a look at it and let me know if there is anything that I can do to search through both titles and descriptions.
Declarations and code(I have skipped the unrequired parts from the activity code)
materialSearchBar = homeView.findViewById(R.id.searchBar);
materialSearchBar.setHint("Search Ad");
loadSuggest();
materialSearchBar.setLastSuggestions(suggestList);
materialSearchBar.setCardViewElevation(10);
materialSearchBar.addTextChangeListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
List<String> sugest = new ArrayList<String>();
for (String search : suggestList) {
if (search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
sugest.add(search);
}
materialSearchBar.setLastSuggestions(sugest);
}
#Override
public void afterTextChanged(Editable editable) {
}
});
materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
#Override
public void onSearchStateChanged(boolean enabled) {
if (!enabled)
mallUsers.setAdapter(firebaseRecyclerAdapter);
}
#Override
public void onSearchConfirmed(CharSequence text) {
startSearch(text);
}
#Override
public void onButtonClicked(int buttonCode) {
}
});
// start search and load suggestions methods
private void startSearch(CharSequence text) {
String searchText = text.toString();
Query query1 = mDatabase1.orderByChild("title").startAt(searchText).endAt(searchText + "\uf8ff");
Query query2 = mDatabase1.orderByChild("description").startAt(searchText).endAt(searchText + "\uf8ff");
FirebaseRecyclerOptions<Ad> firebaseRecyclerOptions2 = new FirebaseRecyclerOptions
.Builder<Ad>()
.setQuery(query1, Ad.class)
.build();
searchAdapter = new FirebaseRecyclerAdapter<Ad, UsersViewHolder1>(firebaseRecyclerOptions2) {
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder1 holder, int position, #NonNull Ad ad1) {
holder.setTitle(ad1.getTitle());
holder.setPrice(ad1.getPrice());
holder.setCategory(ad1.getCategory());
holder.setImage(ad1.getImage(), getContext());
holder.setTime(ad1.getTime());
String user_id = getRef(position).getKey();
final String kk = user_id.toString();
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mew = new Intent(getActivity(), ExpandActivity.class);
mew.putExtra("user_id", kk);
startActivity(mew);
}
});
}
#NonNull
#Override
public UsersViewHolder1 onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view1 = LayoutInflater.from(parent.getContext()).inflate(R.layout
.user_ad_layout, parent,
false);
return new UsersViewHolder1(view1);
}
};
searchAdapter.startListening();
mallUsers.setAdapter(searchAdapter);
}
private void loadSuggest() {
mDatabase1.orderByKey().addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postdataSnapshot : dataSnapshot.getChildren()) {
Ad item = postdataSnapshot.getValue(Ad.class);
if (item != null) {
suggestList.add(item.getTitle());
suggestList.add(item.getDescription());
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
According to this post, there is no way in which you can pass two queries to a single adapter. So you can pass either query1 or query2.
Unfortunately, Firebase Realtime database does not support queries on multiple properties (some people say "multiple where clauses" in SQL terms), supports only queries on a single child property. So you'll need to create an extra field to keep both fields. So to achieve this, you need to create a new field which in your database should look like this:
Firebase-root
|
--- itemId
|
--- title: "valueOfTitle"
|
--- description: "valueOfDescription"
|
--- title_description: "valueOfTitle_valueOfDescription"
So as you see, the title_description property combines the values that you want to filter on. But Firebase real-time database doesn't support native indexing or search for text fields in objects. Additionally, downloading an entire mode to search for fields client-side isn't practical. However, you can do it for small data sets but as I said before is not practical for large data sets. To enable full text search of your Firebase real-tme database, I recommend you to use a third-party search service like Algolia.
Unlike Firebase Realtime database, Cloud Firestore allows compound queries. You should take a look at this. So a query as the one below is allowed in Cloud Firestore without creating a combined property.
itemIdRef.whereEqualTo("title", "valueOfTitle").whereEqualTo("description", "valueOfDescription");
If you want to use Algolia in Cloud Firestore, I recommend you see my answer from this post. For more information, I also recommend you see this video.
You have already experienced the lowest feature of Firebase, querying. However you can query multiple values if you for example upload on firebase both title and description like this:
user:
post:
title: "cake"
description: "yummy"
titledescription: "cake_yummy"
something like this.
Another thing you can do is
private boolean hasValue = false;
private boolean hasValue1 = false;
set value is true inside onBindViewHolder and then using handler delay query values one at a time:
Handler myHandler1 = new Handler();
Runnable myRunnable1 = new Runnable() {
#Override
public void run() {
// query1 here
}
};
Handler myHandler2 = new Handler();
Runnable myRunnable2 = new Runnable() {
#Override
public void run() {
// query2 here
}
};
then you check them one at a time until it finds a value
if(!hasValue){
myHandler1.postDelayed(myRunnable1, 1000);
if(!hasValue1){
myHandler2.postDelayed(myRunnable2, 1500);
} else {
myHandler1.removeCallbacks(myRunnable1);
hasValue1 = false;
} else {
myHandler2.removeCallbacks(myRunnable2);
hasValue2 = false;
}
}
Hope it helps.
I have wandered YouTube and Stack Overflow in search of way to retrieve the teacherName value from the following database:
I have not yet found a solution for my problem, whether I use a Value or ChildEventListener. This is the code I'm using:
public class ViewTeachersActivity extends AppCompatActivity {
// Define the Teacher Firebase DatabaseReference
private DatabaseReference databaseTeachers;
// Define a String ArrayList for the teachers
private ArrayList<String> teachersList = new ArrayList<>();
// Define a ListView to display the data
private ListView listViewTeachers;
// Define an ArrayAdapter for the list
private ArrayAdapter<String> arrayAdapter;
/**
* onCreate method
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_teachers);
// Associate the Teacher Firebase Database Reference with the database's teacher object
databaseTeachers = FirebaseDatabase.getInstance().getReference();
databaseTeachers = databaseTeachers.child("teachers");
// Associate the teachers' list with the corresponding ListView
listViewTeachers = (ListView) findViewById(R.id.list_teachers);
// Set the ArrayAdapter to the ListView
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, teachersList);
listViewTeachers.setAdapter(arrayAdapter);
// Attach a ChildEventListener to the teacher database, so we can retrieve the teacher entries
databaseTeachers.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// Get the value from the DataSnapshot and add it to the teachers' list
String teacher = dataSnapshot.getValue(String.class);
teachersList.add(teacher);
// Notify the ArrayAdapter that there was a change
arrayAdapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I have also tried using a for loop inside the ChildEventListener, but that also didn't work. Can anyone point me to a solution?
String teacher = dataSnapshot.getValue(String.class);
You don't have a String there. You have an object.
So, you need a Java class
public class Teacher {
String teacherID, teacherEmail; // etc. all fieldnames in the database
public Teacher() {
}
// getters
// setters
#Override
public String toString() {
return this.teacherID + ": " + this.teacherEmail;
}
}
Then from Firebase, you can map the snapshot to that class and add to an adapter.
Teacher teacher = (Teacher) dataSnapshot.getValue(Teacher.class);
String teacherString = String.valueOf(teacher);
arrayAdapter.add(teacherString);
Refer: Firebase-UI | Using Firebase to populate ListView
Since your question is unclear if you want to listen for all teacherNames or just one.
This is an easy way to get all 'teacherName' for every entry (uuid key) under /teachers and adds a listener to this query:
FirebaseDatabase.getInstance()
.getReference()
.child("teachers")
.orderByChild("teacherName")
.addValueEventListener(listener); //listener example below
ValueEventListener listener = new new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
dataSnapshot.getChildren().forEach(
// assuming you are using RetroLambda, if not, implement Consumer<? super T> action
// update change ListView here
);
}
#Override
public void onCancelled(FirebaseError firebaseError)
{
//handle errors here
}
});
If you are looking for getting a specific teacher name, comment and I'll change it. Also, I haven't tested syntax as I'm on mobile, so treat this as pseudocode but it should get you going in the right direction
Check out the Firebase Query documentation, it is really straightforward and as of now can do really powerful stuff in a few lines of code. Be careful of outdated tutorials when searching, Firebase has added allot of its functionality in the last ~1 year
Simple thing I would like to do (see in the picture)
Display a view with info coming from 2 different places in Firebase so that it behaves in a professional way scrolling UP and DOWN
I have a list of movies and on each of them I would like the user to specify a rating and see it
In DB I created 2 structures to have the list of movies on one side and the ratings per user on the other
Problem using FirebaseRecyclerAdapter
My problem is that scrolling fast up and down the list, the visualization of the information coming from the second reference (the rating) is loaded on a different time (asynchronous call) and this is not acceptable to see this (little) delay building the view. Is this a limitation of FirebaseRecyclerView?
Because viewHolders are reused in the recycleView I reset and reload each time in populateView() the rating values and this doesn't help. Once retrieved I'm oblidged to get them again if the user scroll the view (see the setOnlistener in populateView()
Setting a listener in populateView cause also to have as many listener as the number of times populateView() is executed (if you scroll UP and DOWN it's many times).
Solutions / Workaround ?
Is there a correct way to do it preventing the problem? Or is it a limitation?
What about performance with my implementation where the listener is inside populateView() and there are MANY listener created?
Below some things I'm thinking on:
Prevent viewHolders to be recycled and just load once?
Override some other methods of RecyclerView? I tried with parseSnapshot() but it's the same problem...
Change the DB structure to have all the info in one list (I don't think it's the good one because it means adding rating information of each user to movie list)
Add a loading spinner on the rating part so that the rating is displayed only when the asyncrhonous call to firebase is completed (don't like it) without the today effect of: "changing star color in front of the user".
My Implementation
From FirebaseRecyclerAdapter
#Override
protected void populateViewHolder(final MovieViewHolder viewHolder, final Movie movie, final int position) {
String movieId = this.getRef(position).getKey();
// Oblidged to show no rating at the beginning because otherwise
// if a viewHolder is reused it has the values from another movie
viewHolder.showNoRating();
//---------------------------------------------
// Set values in the viewHolder from the model
//---------------------------------------------
viewHolder.movieTitle.setText(movie.getTitle());
viewHolder.movieDescription.setText(movie.getDescription());
//-----------------------------------------------------
// Ratings info are in another DB location... get them
// but call is asynchronous so PROBLEM when SCROLLING!
//-----------------------------------------------------
DatabaseReference ratingMovieRef = mDbRef.child(Constants.FIREBASE_LOCATION_RATINGS).child(currentUserId).child(movieId);
ratingQuoteRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
RatingMovie ratingMovie = dataSnapshot.getValue(RatingMovie.class);
Rating rating = Rating.NO_RATING;
if (ratingMovie != null) {
rating = Rating.valueOf(ratingMovie.getRating());
}
// Set the rating in the viewholder (through anhelper method)
viewHolder.showActiveRating(rating);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
from MovieViewHolder
public class QuoteViewHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public TextView movieTitle;
public TextView movieDescription;
public ImageView ratingOneStar;
public ImageView ratingTwoStar;
public ImageView ratingThreeStar;
public QuoteViewHolder(View itemView) {
super(itemView);
movieTitle = (TextView)itemView.findViewById(R.id.movie_title);
movieDescription = (TextView)itemView.findViewById(R.id.movie_descr);
// rating
ratingOneStar = (ImageView)itemView.findViewById(R.id.rating_one);
ratingTwoStar = (ImageView)itemView.findViewById(R.id.rating_two);
ratingThreeStar = (ImageView)itemView.findViewById(R.id.rating_three);
}
/**
* Helper to show the color on stars depending on rating value
*/
public void showActiveRating(Rating rating){
if (rating.equals(Rating.ONE)) {
// just set the good color on ratingOneStar and the others
...
}
else if (rating.equals(Rating.TWO)) {
// just set the good color
...
} else if (rating.equals(Rating.THREE)) {
// just set the good color
...
}
/**
* Initialize the rating icons to unselected.
* Important because the view holder can be reused and if not initalised values from other moviecan be seen
*/
public void initialiseNoRating(){
ratingOneStar.setColorFilter(ContextCompat.getColor(itemView.getContext(), R.color.light_grey));
ratingTwoStar.setColorFilter(....
ratingThreeStar.SetColorFilter(...
}
You can sort of cache the ratings using a ChildEventListener. Basically just create a separat one just for the Ratings node, and have it store the ratings in a Map. Then using the RecyclerAdapter you will retrieve from the Map if the rating is available, if it is not, have the rating listener update the recyclerview as soon as is has downloaded the rating. This is one strategy you could go about, doing it, you will have to manually copy/paste some classes from the FirebaseUI library and set some fields public for this to work.
Usage would be something like this
private MovieRatingConnection ratingConnection;
// inside onCreate
ratingConnection = new MovieRatingConnection(userId, new MovieRatingConnection.RatingChangeListener() {
#Override
public void onRatingChanged(DataSnapshot dataSnapshot) {
if (recyclerAdapter != null) {
if (dataSnapshot != null) {
int index = recyclerAdapter.snapshots.getIndexForKey(dataSnapshot.getKey());
recyclerAdapter.notifyItemChanged(index);
}
}
}
});
Query movieQuery = FirebaseDatabase.getInstance().getReference().child("Movies");
recyclerAdapter = new FirebaseRecyclerAdapter(movieQuery...) {
#Override
public void populateViewHolder(RecyclerView.ViewHolder viewHolder, Object model, int position) {
//...
final String key = getRef(position).getKey();
viewHolder.showActiveRating(ratingConnection.getRating(key));
}
};
and MovieRatingConnection would be a class like this
public class MovieRatingConnection {
private MovieRatingListener listener;
public MovieRatingConnection(String userId, RatingChangeListener changeListener) {
Query query = FirebaseDatabase.getInstance().getReference().child("MovieRatings").child(userId);
listener = new MovieRatingListener(query, changeListener);
}
public Rating getRating(String key) {
return listener.getRating(key);
}
public void cleanup() {
if (listener != null) {
listener.unregister();
}
}
public static class MovieRatingListener implements ChildEventListener {
public interface RatingChangeListener {
public void onRatingChanged(DataSnapshot snapshot);
}
private Query query;
private HashMap<String, Rating> ratingMap = new HashMap<>();
private RatingChangeListener changeListener;
public MovieRatingListener(Query query, RatingChangeListener changeListener) {
this.query = query;
this.changeListener = changeListener;
query.addChildEventListener(this);
}
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null) {
ratingMap.put(dataSnapshot.getKey(), dataSnapshot.getValue(Rating.class));
changeListener.onRatingChanged(dataSnapshot);
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null) {
ratingMap.put(dataSnapshot.getKey(), dataSnapshot.getValue(Rating.class));
changeListener.onRatingChanged(dataSnapshot);
}
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
ratingMap.remove(dataSnapshot.getKey());
changeListener.onRatingChanged(null);
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
public Rating getRating(String key) {
if (ratingMap.get(key) != null) {
return ratingMap.get(key);
} else {
return new Rating(); // default value/null object
}
}
public void unregister() {
query.removeEventListener(this);
}
}
}