How to Retrieve Data from Firebase to My Adapter

How can I retrieve data from Firebase to my adapter

In order to make it work correctly, I recommend you to do some changes in you model class as well as in your code. Your model class should look like:

public class BlogPost {
public String imageThumb, userId, imageUrl, desc;

public BlogPost() {}

public BlogPost(String imageThumb, String userId, String imageUrl, String desc) {
this.imageThumb = imageThumb;
this.userId = userId;
this.imageUrl = imageUrl;
this.desc = desc;
}

public String getImageThumb() {return imageThumb;}
public String getUserId() {return userId;}
public String getImageUrl() {return imageUrl;}
public String getDesc() {return desc;}
}

Please see the naming convention of the fields and getters.

In order to make it work, don't forget the remove the old data and add fresh one.

Assuming your have a .XML file for your activity that contains a RecyclerView which looks like this:

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>

And a .XML file for your item file, which looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image_thumb_text_view" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/user_id_text_view" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image_url_text_view" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/desc_text_view" />
</LinearLayout>

To display your data in a RecyclerView using a FriebaseRecyclerAdapter, please follow the next steps:

First, you need to find the RecyclerView in your activity and set the LinearLayoutManager like this:

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

Then you need to create the root reference of your Firebase database and a Query object like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Users");

Then you'll have to create a FirebaseRecyclerOptions object like this:

FirebaseRecyclerOptions<BlogPost> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<BlogPost>()
.setQuery(query, BlogPost.class)
.build();

In your activity class, create a holder class that looks like this:

private class BlogPostHolder extends RecyclerView.ViewHolder {
private TextView imageThumbtextView, userIdTextView, imageUrlTextView, descTextView;

BlogPostHolder(View itemView) {
super(itemView);
imageThumbtextView = itemView.findViewById(R.id.image_thumb_text_view);
userIdTextView = itemView.findViewById(R.id.user_id_text_view);
imageUrlTextView = itemView.findViewById(R.id.image_url_text_view);
descTextView = itemView.findViewById(R.id.desc_text_view);
}

void setBlogPost(BlogPost blogPost) {
String imageThumb = blogPost.getImageThumb();
imageThumbtextView.setText(imageThumb);
String userId = blogPost.getUserId();
userIdTextView.setText(userId);
String imageUrl = blogPost.getImageUrl();
imageUrlTextView.setText(imageUrl);
String desc = blogPost.getDesc();
descTextView.setText(desc);
}
}

Then create an adapter which is declared as global:

private FirebaseRecyclerAdapter<BlogPost, BlogPostHolder> firebaseRecyclerAdapter;

And instantiate it in your activity like this:

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<BlogPost, BlogPostHolder>(firebaseRecyclerOptions) {
@Override
protected void onBindViewHolder(@NonNull BlogPostHolder blogPostHolder, int position, @NonNull BlogPost blogPost) {
blogPostHolder.setBlogPost(blogPost);
}

@Override
public BlogPostHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);

return new BlogPostHolder(view);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);

In the end, don't forget to override the following two methods and start listening for changes:

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

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

if (firebaseRecyclerAdapter!= null) {
firebaseRecyclerAdapter.stopListening();
}
}

How to retrieve data from Firebase into a ListView (with custom Array Adapter)

You are getting a "blank screen" because the name of the properties in your class do not match the name of the properties in the database. See, you have a field named mPlaceTitle in your Place class, while in the database is called placeTitle and this is not correct. To solve this, you can simply change the name of all properties in your class to match the one in the database, which are named correct according to the Java Naming Conventions regarding variables.

retrieve data of specific value in Firebase recycle view in android studio

Try it this way.
Assuming that AdminOrders.class is your model class.

{
FirebaseRecyclerAdapter<AdminOrders, AdminNewOrdersActivity.AdminOrderViewHolder> adapter;

FirebaseRecyclerOptions<AdminOrders> options =
new FirebaseRecyclerOptions.Builder<AdminOrders>()
.setQuery(shippedOrdersRef.orderByChild("state").equalTo("shipped"), AdminOrders.class)
.build();

adapter = new FirebaseRecyclerAdapter<Products_Model, OrderProductsFragment.AdminOrderViewHolder>(options){
@SuppressLint("SetTextI18n")
@Override
protected void onBindViewHolder(@NonNull final AdminNewOrdersActivity.AdminOrderViewHolder holder, final int position, @NonNull final AdminOrders model) {
// Set your elements here


View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view, parent, false);
return new AdminOrderViewHolder(view);
}
@NonNull
@Override
public AdminOrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//Set your view here
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}

Sorry for the alignments and the brackets if I have mistaken.

FireBase Recycler Adapter doesn't retrieve data from FireBase

This code seems to be the problem:

cartList.orderByChild("userID").equalTo(userID)

This code takes every child nodes directly under cartList and orders them on their userID property. But there is no userID property in your screenshot, so this results in an empty list.

My best guess is that the key of each node under cartList is the user ID, in which case you can instead use:

cartList.child(userID)

So:

final DatabaseReference cartList = FirebaseDatabase.getInstance().getReference().child("Cart");
FirebaseRecyclerOptions<Cart> options =
new FirebaseRecyclerOptions.Builder<Cart>()
.setQuery(cartList.child(userID),Cart.class).build();

How to show the recyclerView data from firebase realtime database in fragments. Android studio

Okay, so looks like you are able to fetch the data from firebase, but are making a mistake in setting the data.

When you are calling adapter.notifyDataSetChanged(), you need to take care that you are passing the new users list, to the adapter.

You need to create a method inside your adapter class,

public void setUsersList(ArrayList<User> users) {
this.users = users;
notifyDatasetChanged();
}

and call it inside your fragment like, adapter.setUsersList(users), instead of calling adapter.notifyDataSetChanged().

Retrieving data from Firebase Realtime Database in Android - recyclerview

The error message is quite explicit on what you need to do:

Consider adding '".indexOn": "Name"' at Users

So you need to add ".indexOn": "Name" at the Users node in your rules.

So:

{
"rules": {
"Users": {
".indexOn": "Name"
}
}
}

The square brackets are fine ["Name"], but right now you're defining the index on the wrong level.

Unable to retrieve data from firebase database android, recycler view

When you try to map a node from the Realtime Database into an object of type "DeleteEditEventsModel", the name of the fields that exist in your class must match the name of your properties that exist in your database. Unfortunately, in your case, the fields don't match. See, the fields in your database start with "Event", while in the class are not, and this is not correct.

To solve this, you have two options, you either change the name of your properties in the database to match the one in the class, or you can use an annotation in front of the getters. For example, if you have a field called "date" and the property in the database is called "EventDate", your getter should look like this:

@PropertyName("EventDate")
public String getDate() {
return date;
}

In this way, you tell the compiler to look for a property called "EventDate" and not "date".



Related Topics



Leave a reply



Submit