Why my constructor in RecyclerView is getting called late? - android

I am creating a recyclerView for my project for which I am using 2 functions in onCreate,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
System.out.println("dsdsds");
pref = getSharedPreferences("pref", Context.MODE_PRIVATE);
dbReff = FirebaseDatabase.getInstance().getReference().child("CartItem");
dbToken = FirebaseDatabase.getInstance().getReference().child("TokenNumber");
CreateList();
BuildRecyclerView();
}
Here I am creating a list which is set in adapter in the first function,
cartItemArrayList = new ArrayList<>();
dbReff.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
if(!data.child("name").getValue().toString().equals("notToDisplayThisItem")) {
cartItemArrayList.add(new CartItemIdQuant(
data.child("id").getValue().toString(),
data.child("quant").getValue().toString(),
data.child("name").getValue().toString(),
data.child("price").getValue().toString()
));
Log.e("THE DATA", data.toString());
}
}
Log.e("INHERE","");
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
and the 2nd function is as followed,
recyclerView = findViewById(R.id.recyclerCartView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
cartAdapter = new CartAdapter(cartItemArrayList);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(cartAdapter);
log.e("Message"," from build recyclerVIew");
and my class for the data item is as followed,
public class CartItemIdQuant {
private String ID;
private String Quant;
private String Name;
private String Price;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getQuant() {
return Quant;
}
public void setQuant(String quant) {
Quant = quant;
}
public CartItemIdQuant(String NewID, String NewQuant,String NewName,String NewPrice){
ID = NewID;
Quant = NewQuant;
Name = NewName;
Price = NewPrice;
Log.e("the" , "Method Called From Constructor");
}
public CartItemIdQuant(){
}
}
here the log of Data that I receive and print in the 1st function is printed and the log I print in the constructor is then printed after 2 seconds. I want to know the reason for the delay in the call, as I have to put a delay for it to work properly which I do not want.
Please help me, thank you in advance.
EDIT:
here as per the comment, is the logcat in which the message from the 2nd function is called before the list could be created.
2020-02-21 15:20:03.434 547-547/com.chinuEvent.chinuevent E/Message: from build recyclerVIew
2020-02-21 15:20:03.837 547-547/com.chinuEvent.chinuevent E/the: Method Called From Constructor
2020-02-21 15:20:03.838 547-547/com.chinuEvent.chinuevent E/THE DATA: DataSnapshot { key = 02, value = {price=15, name=Orange, id=02, quant=3} }

It is actually completely normal to set the adapter to the view before the data is loaded. In many cases (some of) the data is loaded (or modified) asynchronously, and you'll often want to show those updates as they come in.
All you need to do for that is notify the adapter that its underlying data has changed. The adapter will then notify the view(s), and those will repaint with the updated data.
To notify the adapter, call its notifyDataSetChanged() in your onDataChange handler after you've modified cartItemArrayList:
dbReff.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
if(!data.child("name").getValue().toString().equals("notToDisplayThisItem")) {
cartItemArrayList.add(new CartItemIdQuant(
data.child("id").getValue().toString(),
data.child("quant").getValue().toString(),
data.child("name").getValue().toString(),
data.child("price").getValue().toString()
));
}
}
cartAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException(); // never ignore possible errors
}
});

Related

I am fetching data from Firebase Realtime database but I get null object

I am developing an android application using Firebase Realtime Database. But I get Null Object every time. I am getting value in a Static Object, declare and Initialize in a Common class which extends the Application class;
This is my database Structure
Here Is my Code for fetching data
void getLicRequest(String iid)
{
DatabaseReference ref= FirebaseDatabase.getInstance().getReference("LicenseUpRequests");
Query qu=ref.orderByChild("id").equalTo(iid);
qu.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
Commons k=(Commons)getApplicationContext();
k.licRequest=snapshot.getValue(LicenseModel.class);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
```
This is my Model Class
public class LicenseModel {
public String frontSide;
public String backSide;
public String id;
public String date;
public String getFrontSide() {
return frontSide;
}
public void setFrontSide(String frontSide) {
this.frontSide = frontSide;
}
public String getBackSide() {
return backSide;
}
public void setBackSide(String backSide) {
this.backSide = backSide;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
```
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
Your onDataChange will need to navigate this list, by looping over snapshot.getChildren():
qu.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
Commons k=(Commons)getApplicationContext();
for (DataSnapshot requestSnapshot: snapshot.getChildren()) {
k.licRequest=requestSnapshot.getValue(LicenseModel.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
throw error.toException(); // Never ignore errors
}
});

How to increment value in Firebase database android

Hopefully someone can help me with this. Lets say I have a post and I want to track down the number of clicks it gets. How do I go about incrementing the value in the database (Firebase). For example the post has one click that count as 1 then it has another click that counts as 2 then so on and so fourth. I tried doing something but I'm getting this error. I really need your help on this. Thanks in advance
Error message
Can't convert object of type java.lang.Long to type com.myapp.poopy.Model_Information
My code
databaseReference = FirebaseDatabase.getInstance("https://poopy-43981.firebaseio.com/").getReference().child("Post").child(getid);
int count;
count = count ++;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
databaseReference.child("clicks").setValue(count+1);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
});
Json
{
"Post" : {
"lG71ennq86UIZ5a5Oqh8oaYAXe03" : {
"-MHNWmVK3rBnvnr0qcph" : {
"clicks" : 0,
"id" : "-MHNWmVK3rBnvnr0qcph",
}
}
},
Model_class
public Model_Information(String category, String headline, String mImageUrl,String created,String time,String id,String status ) {
this.mImageUrl = mImageUrl;
this.category = category;
this.headline = headline;
this.created=created;
this.time=time;
this.id=id;
this.status=status;
}
public Model_Information() {
}
public String getstatus() {
return status;
}
public void setstatus(String status) {
status = status;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getHeadline() {
return headline;
}
public void setHeadline(String headline) {
this.headline = headline;
}
//Class using model class
databaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String previousChildName) {
if (dataSnapshot.exists()) {
progressBar.setVisibility(View.GONE);
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
myUploads.clear();
Model_Information upload = postsnapshot.getValue(Model_Information.class);
myUploads.add(upload);
aAdapter = new Adapter(MainActivity2.this, myUploads);
recyclerView.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
recyclerView.invalidate();
}
}
}
To be able to update the clicks value you need to know the complete path to that value. In your use-case there seem to be two variables in that path:
the UID
a push ID
Once you know both values, increment the value can be done with the (relatively new) ServerValue.increment operation like this:
DatabaseReference rootReference = FirebaseDatabase.getInstance("https://poopy-43981.firebaseio.com/");
DatabaseReference userReference = rootReference.child("Post").child(getid);
DatabaseReference countReference = userReference.child("-MHNWmVK3rBnvnr0qcph").child("clicks");
countReference.setValue(ServerValue.increment(1));

How to retrieve nested data from firebase

This is the structure of data from my firebase:
I have problem to retrieve all "menukedai" data. By using the code below, data from "menukedai" didnt come out. For your information, the data of "menukedai" will come from the user.
This is my attempt:
foodDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads").child("menukedai");
foodDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
foodmenu foodmenu = postSnapshot.getValue(foodmenu.class);
fooddata.add(foodmenu);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(foodlist.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
This is my foodmenu class.
package com.example.trialfoodbuddy;
public class foodmenu {
private String Foodimage;
private String Foodname;
private CharSequence Foodprice;
public foodmenu(){
//empty constructor needed
}
public foodmenu(String foodimage, String foodname, CharSequence foodprice){
Foodimage = foodimage;
Foodname = foodname;
Foodprice = foodprice;
}
public String getFoodimage(){
return Foodimage;
}
public String getFoodname(){
return Foodname;
}
public CharSequence getFoodprice(){
return Foodprice;
}
public void setFoodimage(String foodimage){
Foodimage = foodimage;
}
public void setFoodname(String foodname){
Foodname = foodname;
}
public void setFoodprice(CharSequence foodprice){
Foodprice = foodprice;
}
}
I Believe you want to get the information nested under "menukedai" so you will need to add another loop for that information
foodDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
foodDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot uploadSnapshot : dataSnapshot.getChildren()) {
// this is where you actually getting the information
for(DataSnapshot menuItemSnapshot : uploadSnapshot.child("menukedai").getChildren()){
foodmenu foodmenu = menuItemSnapshot.getValue(foodmenu.class);
fooddata.add(foodmenu);
}
}
}
// Log the values and check them to make sure you getting the data you want if always nice so I added it for you here
Log.i("Values","My values are:"+foodmenu);
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(foodlist.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
Normally speaking, You should always use the variable names in your class in your Database so when you pulling the information, your constructor would know what value assign to which variable in your class.

Not able to retrieve data from Firebase real time database in Android studio

I'm new to the firebase and android studio. I'm working on a project in which I have to fetch details of the current user and display the name. It is throwing "user doesn't exist' toast even if I'm logged in I'm attaching everything below. Maybe I have done some mistakes please let me know.
Code to fetch details of the current user
public class Succesfully_sign_up extends AppCompatActivity {
DatabaseReference reff;
TextView name;
Button signout;
FirebaseUser curr_user;
String curr_user_str;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_succesfully_sign_up);
name = findViewById(R.id.name);
curr_user=FirebaseAuth.getInstance().getCurrentUser();
if(curr_user!=null)
{
curr_user_str = curr_user.getUid();
reff = FirebaseDatabase.getInstance().getReference("userdetails");
reff.child(curr_user_str).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
HelperClass data = dataSnapshot.getValue(HelperClass.class);
if (data != null) {
name.setText(data.getFullname_hc());
} else {
Toast.makeText(Succesfully_sign_up.this, "User does't exist", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
else
{
startActivity(new Intent(Succesfully_sign_up.this,student_signup.class));
}
}
}
My HelperClass
public class HelperClass {
String fullname_hc,amizone_id_hc,mobile_num_hc,email_id_hc;
public HelperClass()
{
}
public HelperClass(String fullname_hc, String amizone_id_hc, String mobile_num_hc, String email_id_hc) {
this.fullname_hc = fullname_hc;
this.amizone_id_hc = amizone_id_hc;
this.mobile_num_hc = mobile_num_hc;
this.email_id_hc = email_id_hc;
}
public String getFullname_hc() {
return fullname_hc;
}
public void setFullname_hc(String fullname_hc) {
this.fullname_hc = fullname_hc;
}
public String getAmizone_id_hc() {
return amizone_id_hc;
}
public void setAmizone_id_hc(String amizone_id_hc) {
this.amizone_id_hc = amizone_id_hc;
}
public String getMobile_num_hc() {
return mobile_num_hc;
}
public void setMobile_num_hc(String mobile_num_hc) {
this.mobile_num_hc = mobile_num_hc;
}
public String getEmail_id_hc() {
return email_id_hc;
}
public void setEmail_id_hc(String email_id_hc) {
this.email_id_hc = email_id_hc;
}
}
I think you have created the database manually, your user id 8448.... is not generated by the Firebase android, so it is showing that error. If not then please share the code of creation of that above database structure.
How about looping through the children and accessing data:
//the reference
reff = FirebaseDatabase.getInstance().getReference("userdetails");
//the reading
reff.child.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//loop through every possible child under "userdetails"
for(DataSnapshot ds : dataSnapshot.getChildren()){
String fullName = ds.child("fullname_hc").getValue(String.class);
name.setText(fullName);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

Show list of data on my firebase database

Hello I'm trying to show the data's that i have inputted in my activity, but the problem is i don't have any idea how to, I'm at lost on the data snapshot one. I'm trying to like only show the name and email
My database
my code and progress so far
public class AdminActivity extends AppCompatActivity {
private DrawerLayout mdrawerl;
private ActionBarDrawerToggle mtoggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
collectName((Map<String,Object>)dataSnapshot.getValue());
}
private void collectName(Map<String, Object> value) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
reference that i used How to get all child's data in firebase database?
1- Create a model class that will hold the objects of user. In your case it would be
public class User {
private String email;
private String name;
private int type;
public User() {
}
public User(String email, String name, int type) {
this.email= email;
this.name= name;
this.type= type;
}
}
2 - Create a ArrayList that will hold items of User
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<User> userList = new ArrayList();
for (DataSnapshot child :
dataSnapshot.getChildren()) {
User user = child.getValue(User.class);
userList.add(user )
}
//Here you have all user you can pass ```userList``` to your method furthur
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Hope this will help you.

Categories

Resources