I've checked each and every bit of code in order to successfully render the data on recyclerview from Firebase, but still cannot figure out the problem.
Nothing is show on the logcat when I use Log.d for getting the children of the snapshot(means line is not executed). Can anybody point out the error.
public class MyOrders extends AppCompatActivity {
private static final String TAG = "MyOrders";
DatabaseReference databaseReference;
FirebaseRecyclerAdapter firebaseRecyclerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_orders);
RecyclerView myOrderList = findViewById(R.id.myOrderList);
Query query = FirebaseDatabase.getInstance().getReference("Requests").orderByChild("name").equalTo(User.currentUser.getUserName());
FirebaseRecyclerOptions<Request> options = new FirebaseRecyclerOptions.Builder<Request>().setQuery(query, new SnapshotParser<Request>() {
#NonNull
#Override
public Request parseSnapshot(#NonNull DataSnapshot snapshot) {
Log.d(TAG, "onCreate: "+snapshot.getChildrenCount());
return new Request(snapshot.child("name").getValue().toString(), snapshot.child("phone").getValue().toString(), snapshot.child("address").getValue().toString(), snapshot.child("total").getValue().toString());
}
}).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Request, MyOrdersViewHolder>(options){
#NonNull
#Override
public MyOrdersViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_order_template, viewGroup, false);
return new MyOrdersViewHolder(v);
}
#Override
protected void onBindViewHolder(#NonNull MyOrdersViewHolder holder, int position, #NonNull Request model) {
Log.d(TAG, "onCreate: "+position);
holder.setOrder_id(firebaseRecyclerAdapter.getRef(position).getKey());
holder.setOrder_status(model.getOrderStatus());
holder.setOrder_items(model.getRequestedOrderList().size()+"");
holder.setItemClickLitener(new ItemClickLitener() {
#Override
public void onClick(View v, int pos, boolean isLongClick) {
Toast.makeText(MyOrders.this, ""+"Pending Action", Toast.LENGTH_SHORT).show();
}
});
}
};
myOrderList.setAdapter(firebaseRecyclerAdapter);
}
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
firebaseRecyclerAdapter.stopListening();
}
}
Related
i have a document inside firestore, that document also has a list inside is called "foods". I want to be able to load only that list of "foods" into a recyclerview, I'm not sure how to query the list for recycler options. Here is what the DB structure looks like:
Here is my Adapter
List<Order> myOrders;
public OrderDetailAdapter(#NonNull FirestoreRecyclerOptions<Order> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull OrderDetailAdapter.DetailsHolder holder, int position, #NonNull Order model) {
Order order = myOrders.get(position);
holder.product_name.setText(model.getProductName());
holder.product_price.setText(model.getPrice());
holder.product_quantity.setText(model.getQuantity());
holder.product_discount.setText(model.getDiscount());
}
#NonNull
#Override
public OrderDetailAdapter.DetailsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item_layout, parent,false);
return new DetailsHolder(v);
}
public class DetailsHolder extends RecyclerView.ViewHolder {
TextView product_name, product_price, product_discount, product_quantity;
public DetailsHolder(#NonNull View itemView) {
super(itemView);
product_name = itemView.findViewById(R.id.product_name);
product_price = itemView.findViewById(R.id.product_price);
product_discount = itemView.findViewById(R.id.product_discount);
product_quantity = itemView.findViewById(R.id.product_quantity);
}
}
#Override
public int getItemCount() {
return myOrders.size();
}
}
Here is my Activity:
private FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference documentReference;
TextView order_id, order_comment, order_date, order_total;
String order_id_value ="";
RecyclerView lstFoods;
RecyclerView.LayoutManager layoutManager;
OrderDetailAdapter orderAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_details);
if (getIntent() !=null)
order_id_value = getIntent().getStringExtra("order_number");
documentReference = db.collection("Requests").document(order_id_value);
lstFoods = findViewById(R.id.food_list);
lstFoods.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
lstFoods.setLayoutManager(layoutManager);
order_id.setText(order_id_value);
documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot documentSnapshot, #Nullable FirebaseFirestoreException e) {
if (documentSnapshot.exists()){
Order order = documentSnapshot.toObject(Order.class);
orderAdapter.notifyDataSetChanged();
lstFoods.setAdapter(orderAdapter);
}else{
Log.d("TAG", "ERROR");
}
}
});
}
#Override
protected void onStart() {
super.onStart();
orderAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
orderAdapter.stopListening();
}
}
When i try to run it i get the following error:
error: incompatible types: List cannot be converted to FirestoreRecyclerOptions
super(options);
Im attempting a FireBase Horizontal RecyclerView (like an Netflix clone).
I have looked at Git and Android Arsenal to find programing similar, but had been unsuccessful finding anything with a firebase backend. I had played with the adapter and the database. I have work on this for a couple days without finding a solution.
public class RestaurantList extends AppCompatActivity {
RecyclerView recyclerView1;
RecyclerView recyclerView2;
FirebaseRecyclerOptions<Restaurant> options = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference()
.child("Restaurants1")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter = new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
FirebaseRecyclerOptions<Restaurant> options2 = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference()
.child("Restaurants2")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter2= new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options2) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter2.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_list);
RelativeLayout relativeLayout = findViewById(R.id.root2_layout);
AnimationDrawable animationDrawable = (AnimationDrawable)relativeLayout.getBackground();
animationDrawable.setEnterFadeDuration(5);
animationDrawable.setExitFadeDuration(1000);
animationDrawable.start();
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
LinearLayoutManager layoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
recyclerView1 = (RecyclerView) findViewById(R.id.recycler_restaurant);
recyclerView1.setLayoutManager(layoutManager);
recyclerView2 = (RecyclerView) findViewById(R.id.recycler_restaurant2);
recyclerView1.setLayoutManager(layoutManager2);
loadAsianFood();
loadSteakHouse();
}
private void loadSteakHouse() {
adapter.startListening();
recyclerView1.setAdapter(adapter);
recyclerView1.getAdapter().notifyDataSetChanged();
recyclerView1.scheduleLayoutAnimation();
}
private void loadAsianFood() {
adapter2.startListening(); // Did you mean adapter2.startListening()?
recyclerView2.setAdapter(adapter2);
recyclerView2.getAdapter().notifyDataSetChanged();
recyclerView2.scheduleLayoutAnimation();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
adapter2.stopListening();;
}
#Override
protected void onResume() {
super.onResume();
if (adapter != null)
adapter.startListening();
adapter2.startListening();
}
}
It only runs the first category (Restaurant1) only.
A few problems I noticed, though I don't know if they'll solve your actual issue.
1 - You are calling startListening on the first adapter when it should be the second:
private void loadAsianFood() {
adapter.startListening(); // Did you mean adapter2.startListening()?
recyclerView2.setAdapter(adapter2);
recyclerView2.getAdapter().notifyDataSetChanged();
recyclerView2.scheduleLayoutAnimation();
}
2 - You are only stopping the first adapter, not the second.
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
3 - You are only resuming the first adapter, not the second:
#Override
protected void onResume() {
super.onResume();
if (adapter != null)
adapter.startListening();
}
RecyclerView recyclerView1;
RecyclerView recyclerView2;
FirebaseRecyclerOptions<Restaurant> options = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference("RestaurantCategory")
.child("01").child("SteakHouse")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter = new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
FirebaseRecyclerOptions<Restaurant> options2 = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference("RestaurantCategory")
.child("02").child("AsianFood")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter2= new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options2) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter2.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_list);
RelativeLayout relativeLayout = findViewById(R.id.root2_layout);
AnimationDrawable animationDrawable = (AnimationDrawable)relativeLayout.getBackground();
animationDrawable.setEnterFadeDuration(5);
animationDrawable.setExitFadeDuration(1000);
animationDrawable.start();
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
LinearLayoutManager layoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
recyclerView1 = (RecyclerView) findViewById(R.id.recycler_restaurant);
recyclerView1.setLayoutManager(layoutManager);
recyclerView2 = (RecyclerView) findViewById(R.id.recycler_restaurant2);
recyclerView1.setLayoutManager(layoutManager2);
loadAsianFood();
loadSteakHouse();
}
private void loadSteakHouse() {
adapter.startListening();
recyclerView1.setAdapter(adapter);
recyclerView1.getAdapter().notifyDataSetChanged();
recyclerView1.scheduleLayoutAnimation();
}
private void loadAsianFood() {
adapter2.startListening(); // Did you mean adapter2.startListening()?
recyclerView2.setAdapter(adapter2);
recyclerView2.getAdapter().notifyDataSetChanged();
recyclerView2.scheduleLayoutAnimation();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
adapter2.stopListening();;
}
#Override
protected void onResume() {
super.onResume();
if (adapter != null)
adapter.startListening();
adapter2.startListening();
}
}
I am trying to search for the information in my firebase db, and I am not able to do so.
This is the class in which I'm searching from:
private FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder> firebaseRecyclerAdapter;
public static Query query;
public static String searchData;
DatabaseReference mProfileDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendee_landing);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final RecyclerView recyclerView = findViewById(R.id.results_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final EditText t = (EditText) findViewById(R.id.DjNameET);
mProfileDatabase = FirebaseDatabase.getInstance().getReference();
t.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if(i == EditorInfo.IME_ACTION_SEARCH)
{
searchData = t.getText().toString();
return true;
}
return false;
}
});
ImageButton imgB= (ImageButton) findViewById(R.id.searchBtn);
imgB.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
query = mProfileDatabase.orderByChild("djprofile")
.startAt(searchData)
.endAt(searchData + "\uf8ff");
}
});
FirebaseRecyclerOptions<DjProfile> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DjProfile>()
.setQuery(query, DjProfile.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder>(firebaseRecyclerOptions)
{
#NonNull
#Override
public ResultsViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.searchitem_list, viewGroup, false);
return new ResultsViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull ResultsViewHolder holder, int position, #NonNull DjProfile model) {
holder.setDjProfile(model);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
#Override
protected void onStart()
{
super.onStart();
firebaseRecyclerAdapter.startListening();
}
Here is the exception I am getting: (says to be occuring in the onStart() method)
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
Any help as to why this is null would be greatly appreciated!
To be able to initialize the adapter, you need to have a valid query. And to be able to create the query, you need to have the searchData. This means that the initialization of the firebaseRecyclerAdapter should happen inside the onClick.
imgB.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
query = mProfileDatabase.orderByChild("djprofile")
.startAt(searchData)
.endAt(searchData + "\uf8ff");
FirebaseRecyclerOptions<DjProfile> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DjProfile>()
.setQuery(query, DjProfile.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder>(firebaseRecyclerOptions)
{
#NonNull
#Override
public ResultsViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.searchitem_list, viewGroup, false);
return new ResultsViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull ResultsViewHolder holder, int position, #NonNull DjProfile model) {
holder.setDjProfile(model);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
});
I am developing an app that shows in a RecyclerView an image and a text that comes from firebase.
When I open the application the recyclerview does not appear but if I turn off and then turn on the screen the recyclerview appears correctly with the image and text obtained from firebase. I need help please
ViewHolder:
public class ViewHolderServicio extends RecyclerView.ViewHolder {
View mView;
public ViewHolderServicio(#NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(Context ctx, String nombreServicio, String imagenPerfil) {
TextView mNombreServicio = mView.findViewById(R.id.NombreServicio);
ImageView mImagenPerfil = mView.findViewById(R.id.imageViewServicio);
mNombreServicio.setText(nombreServicio);
Picasso.get().load(imagenPerfil).into(mImagenPerfil);
}
}
Model:
public class ModelServicio {
String idUsuario, nombreServicio, descripcion, imagenPerfil;
public ModelServicio(){}
public String getIdUsuario() {
return idUsuario;
}
public void setIdUsuario(String idUsuario) {
this.idUsuario = idUsuario;
}
public String getNombreServicio() {
return nombreServicio;
}
public void setNombreServicio(String nombreServicio) {
this.nombreServicio = nombreServicio;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public String getImagenPerfil() {
return imagenPerfil;
}
public void setImagenPerfil(String imagenPerfil) {
this.imagenPerfil = imagenPerfil;
}
}
class activity:
public class DetalleServicio extends AppCompatActivity {
RecyclerView RVbot;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
FirebaseRecyclerAdapter<ModelServicio, ViewHolderServicio> firebaseRecyclerAdapter;
FirebaseRecyclerOptions<ModelServicio> options;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detalle_servicio);
RVbot = findViewById(R.id.RVBot);
RVbot.setHasFixedSize(true);
RVbot.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Servicios");
mostrarDatos();
}
private void mostrarDatos() {
options = new FirebaseRecyclerOptions.Builder<ModelServicio>().setQuery(mRef, ModelServicio.class).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ModelServicio, ViewHolderServicio>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolderServicio holder, int position, #NonNull ModelServicio model) {
holder.setDetails(getApplicationContext(), model.getNombreServicio(), model.getImagenPerfil());
}
#NonNull
#Override
public ViewHolderServicio onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.servicioitem, viewGroup, false);
ViewHolderServicio viewHolder = new ViewHolderServicio(itemView);
return viewHolder;
}
};
RVbot.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
}
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
RVbot.setAdapter(firebaseRecyclerAdapter);
}
}
Have you tried
firebaseRecyclerAdapter.notifyDataSetChanged();
after calling an Adapter?
I have a firebase strucuture as
The recycler view is only displaying the last added or say latest added node to the database instead of displaying each and every node
the database reference i am using is
r2 = FirebaseDatabase.getInstance().getReference().child("Uploads").child("Wheat").child(S).child(D).child(T).child(FirebaseAuth.getInstance().getUid());
and the code for recycler view is as
public class MyPost extends AppCompatActivity {
private RecyclerView recyclerView;
private DatabaseReference r, r2, r3;
private String S, D, T;
FirebaseRecyclerAdapter<RecyclerCropView, ViewHolder2> Fbra1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_post);
r = FirebaseDatabase.getInstance().getReference().child("User_Data").child(MainDashboard.type).child(FirebaseAuth.getInstance().getUid());
recyclerView = (RecyclerView) findViewById(R.id.R_view2);
r.addListenerForSingleValueEvent(rr);
}
private ValueEventListener rr = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
S = dataSnapshot.child("State").getValue().toString();
D = dataSnapshot.child("City").getValue().toString();
T = dataSnapshot.child("Tehsil").getValue().toString();
// Toast.makeText(MyPost.this,S+D+T.toString(),Toast.LENGTH_LONG).show();
r2 = FirebaseDatabase.getInstance().getReference().child("Uploads").child("Wheat").child(S).child(D).child(T).child(FirebaseAuth.getInstance().getUid());
// Toast.makeText(MyPost.this,r2.toString(),Toast.LENGTH_LONG).show();
r2.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
r3=r2.child(dataSnapshot.getKey());
Toast.makeText(MyPost.this, r3.toString(), Toast.LENGTH_LONG).show();
FirebaseRecyclerOptions<RecyclerCropView> options = new FirebaseRecyclerOptions.Builder<RecyclerCropView>().setQuery(r3, RecyclerCropView.class).build();
Fbra1 = new FirebaseRecyclerAdapter<RecyclerCropView, ViewHolder2>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolder2 holder, int position, #NonNull RecyclerCropView model) {
holder.setProductImage(model.getProduct_Image());
holder.setProduct(model.getProduct());
holder.setMax(model.getMaximumPrice());
holder.setQuantityUnit(model.getQuantityUnit());
holder.setQuantity(model.getQuantity());
}
#NonNull
#Override
public ViewHolder2 onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_post, parent, false);
return new ViewHolder2(v);
}
};
Fbra1.notifyDataSetChanged();
recyclerView.hasFixedSize();
recyclerView.setLayoutManager(new LinearLayoutManager(MyPost.this));
Fbra1.startListening();
recyclerView.setAdapter(Fbra1);
}
#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) {
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
#Override
protected void onRestart() {
super.onRestart();
finish();
}
}
Recycler view is only showing very last item that was added to database.
Note:-
The item.xml file has width set to wrap content.
if i remove the childEventListener the recyclerView shows multiple items with
null on the place of TextView(These are those textView on which data retrived was supposed to be set)
Here is the problem: r2.addChildEventListener
Classes implementing this interface can be used to receive events about changes in the child locations of a given DatabaseReference ref. Attach the listener to a location using addChildEventListener(ChildEventListener) and the appropriate method will be triggered when changes occur.
Firebase Docs
What you should use is a r2.addListenerForSingleValueEvent
This returns the children of the specific node. But now since you are using FirebaseUI library, you will not need to listen as it is a listener itself.
So your code should listen to the root of the posts. This r3=r2.child(dataSnapshot.getKey()); is unnecessary because it only gets one entry
Change to this
FirebaseRecyclerOptions<RecyclerCropView> options = new FirebaseRecyclerOptions.Builder<RecyclerCropView>().setQuery(r2, RecyclerCropView.class).build();
Fbra1 = new FirebaseRecyclerAdapter<RecyclerCropView, ViewHolder2>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolder2 holder, int position, #NonNull RecyclerCropView model) {
holder.setProductImage(model.getProduct_Image());
holder.setProduct(model.getProduct());
holder.setMax(model.getMaximumPrice());
holder.setQuantityUnit(model.getQuantityUnit());
holder.setQuantity(model.getQuantity());
}
#NonNull
#Override
public ViewHolder2 onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_post, parent, false);
return new ViewHolder2(v);
}
};
Fbra1.notifyDataSetChanged();
recyclerView.hasFixedSize();
recyclerView.setLayoutManager(new LinearLayoutManager(MyPost.this));
Fbra1.startListening();
recyclerView.setAdapter(Fbra1);