retrieve data from firebase using getUid() in a alertDialog box - android

Want to retrieve username and its contact number from the firebase database. I tried doing it but in the logcat it throws an Null Pointer
Logcat:-
Process: com.example.bhavya.epark, PID: 27351
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.bhavya.epark.userreg.getName()' on a null object reference
at com.example.bhavya.epark.MapSelect$1$1.onDataChange(MapSelect.java:51)
at com.google.firebase.database.obfuscated.zzap.zza(com.google.firebase:firebase-database##16.0.3:75)
at com.google.firebase.database.obfuscated.zzca.zza(com.google.firebase:firebase-database##16.0.3:63)
at com.google.firebase.database.obfuscated.zzcd$1.run(com.google.firebase:firebase-database##16.0.3:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
MapSelect.java
mAuth = FirebaseAuth.getInstance();
udetail = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = udetail.getReference(mAuth.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
userreg userprofile = dataSnapshot.getValue(userreg.class);
alertDialog1.setMessage("Name is:" + userprofile.getName());
alertDialog1.setMessage("Your Contact No:" + userprofile.getPnum());
alertDialog1.setMessage("Your Vehicle No is:" + userprofile.getVeclno());
alertDialog1.show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MapSelect.this,databaseError.getCode(),Toast.LENGTH_SHORT).show();
}
});
userreg.java
package com.example.bhavya.epark;
public class userreg {
public String name,mailid,pnum,veclno;
public userreg(){
}
public userreg(String name,String mailid,String pnum,String veclno){
this.name=name;
this.mailid=mailid;
this.pnum=pnum;
this.veclno=veclno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMailid() {
return mailid;
}
public void setMailid(String mailid) {
this.mailid = mailid;
}
public String getPnum() {
return pnum;
}
public void setPnum(String pnum) {
this.pnum = pnum;
}
public String getVeclno() {
return veclno;
}
public void setVeclno(String veclno) {
this.veclno = veclno;
}
}
Database Strucure:-
Image reference
https://i.stack.imgur.com/owWJr.jpg

You are pointing towards a wrong database reference. Your database has a node named Users but your code doesn't have this.
Instead of
DatabaseReference databaseReference = udetail.getReference(mAuth.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
userreg userprofile = dataSnapshot.getValue(userreg.class);
alertDialog1.setMessage("Name is:" + userprofile.getName());
alertDialog1.setMessage("Your Contact No:" + userprofile.getPnum());
alertDialog1.setMessage("Your Vehicle No is:" + userprofile.getVeclno());
alertDialog1.show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MapSelect.this,databaseError.getCode(),Toast.LENGTH_SHORT).show();
}
});
Use
DatabaseReference databaseReference = udetail.getReference().child("Users");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
userreg userprofile = messageSnapshot.getValue(userreg.class);
Log.v("info",String.valueOf(userprofile));
}
//Do whatever you like to do with your data here
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MapSelect.this,databaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
});

Related

get a value with different key Firebase Realtime DB

so i want to create a refferal code system,using firebase realtime database,but i found a difficult,how i can get all the value in key refferal?
i already have the code,but its just give me the value from my uid,
Show below:-
Query query = mDatabase.child(uid).orderByChild("refferal_status");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot movieSnapshot : dataSnapshot.getChildren()) {
UserModel movie = dataSnapshot.getValue(UserModel.class);
if (movie.getRefferal_status().equals("akmKA")) {
Toast.makeText(SpinActivity.this, movie.getRefferal_status(), Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Class Model
String email;
String point;
String checkin;
String limitation;
String refferal_status;
public UserModel(){
}
public UserModel(String email,String point,String checkin,String limitation,String refferal_status){
this.email = email;
this.point = point;
this.checkin = checkin;
this.limitation = limitation;
this.refferal_status = refferal_status;
}
//setter getter here
}
so this is my database
Image
Try this !
Note:- equals expect String object .
if ("akmKA".equals(movie.getRefferal_status())) {
Toast.makeText(SpinActivity.this, movie.getRefferal_status(), Toast.LENGTH_SHORT).show();
}
Final code
Query query = mDatabase.child(uid).orderByChild("refferal_status");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot movieSnapshot : dataSnapshot.getChildren()) {
UserModel movie = dataSnapshot.getValue(UserModel.class);
if ("akmKA".equals(movie.getRefferal_status())) {
Toast.makeText(SpinActivity.this, movie.getRefferal_status(), Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

How to retrieve all child data in Firebase and show in xml layout

I am creating an App with Firebase in which user can add data and show the data directly from Firebase Database. Data is inserted successfully and it looks like this in database after each value inserted.
This is the picture of my database:
I want to retrieve all the data in Student (eg studentName & studentPhoneNumber) and displayed in a .xml layout file. Since i use legacy SDK in my code, i dont know how to use DatabaseReference.
ShowButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
firebase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Student student = postSnapshot.getValue(Student.class);
String ShowDataString =ShowDataTextView.getText()+ "Name : "+student.getStudentName()+"\nPhone Number : "+student.getStudentPhoneNumber()+"\n\n";
ShowDataTextView.setText(ShowDataString);
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("Data Access Failed" + firebaseError.getMessage());
}
});
}
});
My Student.class look like this:
public class Student {
private String name;
private String phoneNumber;
public Student() {
}
public String getStudentName() {
return name;
}
public void setStudentName(String name) {
this.name = name;
}
public String getStudentPhoneNumber() {
return phoneNumber;
}
public void setStudentPhoneNumber(String phonenumber) {
this.phoneNumber = phonenumber;
}
}
Take student arrayList and add students data to list.
FirebaseAuth mFirebaseAuthInstance = FirebaseAuth.getInstance();
FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance();
FirebaseUser mFirebaseCurrentUser = mFirebaseAuthInstance.getCurrentUser();
DatabaseReference mDatabaseRef = mFirebaseDatabase.getReference("User").child(mFirebaseCurrentUser.getUid()).child("Student");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
ArrayList<Student> studentList=new ArrayList<Student>();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Student student = postSnapshot.getValue(Student.class);
studentList.add(student);
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("Data Access Failed" + firebaseError.getMessage());
}
});
Use RecylerView to load data to xml,refer the below link to use RecylerView
More reference
Try this
public class FethingDatas extends AppCompatActivity {
RecyclerView ry_fetchdata;
Firebase firebase;
ArrayList<Pojo_Recylerview> arrayList = new ArrayList<>();
Pojo_Recylerview pojo_recylerview_obj;
Display_Adapter display_adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fething_datas);
ry_fetchdata = findViewById(R.id.ry_fetchdata);
firebase = new Firebase("https://fir-project-87eac.firebaseio.com/").child("Userdata");
layoutManager = new LinearLayoutManager(this);
ry_fetchdata.setLayoutManager(layoutManager);
ry_fetchdata.setItemAnimator(new DefaultItemAnimator());
display_adapter = new Display_Adapter(this,arrayList);
ry_fetchdata.setAdapter(display_adapter);
firebase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
arrayList.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
{
String subject= (String) dataSnapshot1.child("subject").getValue();
String username = (String) dataSnapshot1.child("username").getValue();
pojo_recylerview_obj = new Pojo_Recylerview();
Log.d("subject","username"+subject +" "+username);
pojo_recylerview_obj.setSubject(subject);
pojo_recylerview_obj.setUsername(username);
arrayList.add(pojo_recylerview_obj);
}
display_adapter.notifyDataSetChanged();
}else
{
Toast.makeText(FethingDatas.this,"No More Data Found",Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}

cannot access outer class variable for datasnapshot.haschild() method

This is my datbase tree
the below is my code to check if my database has a child with a specified number stored in num variable.
i am unable to access num variable in addListenerForSingleValueEvent
here is my code.
Thanks in advance.
mDatabase = FirebaseDatabase.getInstance().getReference();
public String nam,num;
for( contacts e : mylist)
{
num = e.getPhoneNumber();
nam = e.getName();
check(nam,num);
}
public void check(final String nam, final String num)
{
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(num)) //this line is giving error
Toast.makeText(getActivity(),num,Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(),"some error",Toast.LENGTH_SHORT).show();
}
});
}
To solve this, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference numberRef = rootRef.child(num);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
Log.d("TAG", "Number exists!");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
numberRef.addListenerForSingleValueEvent(eventListener);

Firebase DataSnapshot Null values

My app allows you to add a Player to a node, once they have been added I am using the below method to search by phoneNum to see if that player exists in the 'users' node.
Where I am having trouble is that I need to be able to access the values from the datasnapshot for the user within the found matching node. e.g.return the value of the user id. When I attempt this it keeps showing as a null value and when I try to log to the console it complains that it is a null value which doesn't make sense as the if statement should take care of this.
The method is able to find if a player exists in the the users node and displays a Toast message to indicate this.
Code:
public void checkPlayerNum(final String phoneNum) {
DatabaseReference mDatabaseReference =
FirebaseDatabase.getInstance().getReference().child("users");
if (!TextUtils.isEmpty(phoneNum)) {
final Query phoneNumReference = mDatabaseReference.orderByChild("phoneNum").equalTo(phoneNum);
ValueEventListener phoneNumValueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
// String key = String.valueOf(dataSnapshot.getValue());
User mUser = dataSnapshot.getValue(User.class);
String uId = mUser.getUid();
// Log.d("TAG",uId);
Toast.makeText(getApplicationContext(), "* found **"+ uId , Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "****NOT FOUND****", Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
phoneNumReference.addListenerForSingleValueEvent(phoneNumValueEventListener);
} else {
Log.e("Error","phoneNum is null");
}
}
final Query query = mFirebaseDatabase.orderByChild("phoneNum").equalTo("userPhoneNumber");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
//here means the value exist
//do whatever you want to do
} else {
//here means the value not exist
//do whatever you want to do
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Use this to check whether the data exist or not.
you need to do this at two steps , first check if the user exists. then you get user info by your userId.
DatabaseReference mDatabaseReference;
public void checkPlayerNum(final String phoneNum,final String userId) {
mDatabaseReference =
FirebaseDatabase.getInstance().getReference().child("users");
if (!TextUtils.isEmpty(phoneNum)) {
final Query phoneNumReference = mDatabaseReference.orderByChild("phoneNum").equalTo(phoneNum);
ValueEventListener phoneNumValueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
// user exists
getUserInf(userId);
} else {
Toast.makeText(getApplicationContext(), "****NOT FOUND****", Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
phoneNumReference.addListenerForSingleValueEvent(phoneNumValueEventListener);
} else {
Log.e("Error","phoneNum is null");
}
}
get user info by userId. mDatabaseReference is the same as before . define it as member field .
void getUserInf(String userId) {
userValueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// get user data
User mUser = dataSnapshot.getValue(User.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
mDatabaseReference.child(userId).addListenerForSingleValueEvent(userValueEventListener);
}
your user Pojo should be like below.
public class User {
String email,name,phoneNum,uid;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
Thanks everybody I managed to achieve it by using childEventListener
public void checkingNumber(final String phoneNum){
DatabaseReference mDatabaseReference =
FirebaseDatabase.getInstance().getReference().child("users");
final Query query = mDatabaseReference;
query.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
// String key = String.valueOf(dataSnapshot.getValue());
User mUser = dataSnapshot.getValue(User.class);
// String uId = mUser.getUid();
// usersDatabase.child(mUser.getUid());
// Log.d("TAG",uId);
if(mUser.getPhoneNum().equals(phoneNum)) {
String uId= mUser.getUid();
String email = mUser.getEmail();
String name = mUser.getName();
Toast.makeText(getApplicationContext(), "* found **" + " " + uId + " " + " " + email + " " + name, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "****NOT FOUND****", Toast.LENGTH_LONG).show();
}
}
#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) {
}
});
}

Android Firebase retrieve data from child node

I'm trying to get the data from Firebase child node. But it throws Can't convert object of type java.lang.String to type uk.co.stableweb.geethika.model.DailyVerse exception.
This is the structure of my Firebase database.
Here is the code. I'm using Android Firebase new documentation.
mRef = FirebaseDatabase.getInstance().getReference().child("daily_verse");
mRef.keepSynced(true);
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d("Child Event,Verse", dataSnapshot.getKey());
dailyVerse = dataSnapshot.getValue(DailyVerse.class);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d("Child Event,Verse", "onChildChanged:" + dataSnapshot.getKey());
dailyVerse = dataSnapshot.getValue(DailyVerse.class);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("Firebase", "onCancelled", databaseError.toException());
Toast.makeText(context, "Failed to load verse",
Toast.LENGTH_SHORT).show();
}
};
mRef.addChildEventListener(childEventListener);
And Model class.
public class DailyVerse {
private String verse_title;
private String verse_content;
public String getVerseTitle() {
return verse_title;
}
public void setVerseTitle(String verse_title) {
this.verse_title = verse_title;
}
public String getVerseContent() {
return verse_content;
}
public void setVerseContent(String verse_content) {
this.verse_content = verse_content;
}
}
And the LogCat.
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type uk.co.stableweb.geethika.model.DailyVerse
at com.google.android.gms.internal.zzalq.zzd(Unknown Source)
at com.google.android.gms.internal.zzalq.zzb(Unknown Source)
at com.google.android.gms.internal.zzalq.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at uk.co.stableweb.geethika.VerseActivityFragment$1.onChildAdded(VerseActivityFragment.java:63)
at com.google.android.gms.internal.zzahh.zza(Unknown Source)
at com.google.android.gms.internal.zzajh.zzctc(Unknown Source)
at com.google.android.gms.internal.zzajk$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
But if I change the database reference as follows. And get the dataSnapshot value, it grabs the data.
mRef = FirebaseDatabase.getInstance().getReference("daily_verse");
testVal = (String) dataSnapshot.getValue();
Log.d("TEST", testVal);
V/FA: Activity resumed, time: 278033062
D/Child Event,Verse added: verse_content
D/TEST: විශ්වාසකමේ මාර්ගය තෝරා ගතිමි. ඔබගේ විනිශ්චයන් මා ඉදිරියෙහි තබා ගතිමි.
D/Child Event,Verse added: verse_title
D/TEST: ගීතාවලිය 119:30
I think it is possible to get those values using HashMap. But there was a comment saying it is no longer recommended.
Pay careful attention to the names of your properties in the JSON and your fields/getters in the Java code. If they don't match, the Firebase client won't be able to match the values between them.
Your JSON has verse_title and verse_content, so your Java class must be either:
public class DailyVerse {
public String verse_title;
public String verse_content;
}
or:
public class DailyVerse {
private String verse_title;
private String verse_content;
public String getVerse_title() {
return verse_title;
}
public void setVerse_title(String verse_title) {
this.verse_title = verse_title;
}
public String getVerse_content() {
return verse_content;
}
public void setVerse_content(String verse_content) {
this.verse_content = verse_content;
}
}
Since Firebase Android SDK 9.2, you can also annotate your Java code to map to the correct JSON properties. So a third way to get the correct mapping is:
public class DailyVerse {
private String verse_title;
private String verse_content;
#PropertyName("verse_title")
public String getVerseTitle() {
return verse_title;
}
public void setVerseTitle(String verse_title) {
this.verse_title = verse_title;
}
#PropertyName("verse_content")
public String getVerseContent() {
return verse_content;
}
public void setVerseContent(String verse_content) {
this.verse_content = verse_content;
}
}
I haven't tried that latest variant though, so let me know if there are typos on the answer.
By this method
mRef.addChildEventListener(childEventListener);
you subscribe for
.child("daily_verse");
children. daily_verse has two children: verse_content and verse_title. The type of these children is String. So, you get two String datasnapshot in the onChildAdd listener.
Update:
Try something like this:
mRef = FirebaseDatabase.getInstance().getReference();
mRef.keepSynced(true);
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d("Child Event,Verse", dataSnapshot.getKey());
if ("daily_verse".equal(dataSnapshot().getKey){
dailyVerse = dataSnapshot.getValue(DailyVerse.class);
Log.d("TEST","Object: "+dailyVerse);
if (dailyVerse!=null){
Log.d("TEST","values: "+dailyVerse.verse_title+", "+dailyVerse.verse_content);
}
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d("Child Event,Verse", "onChildChanged:" + dataSnapshot.getKey());
if ("daily_verse".equal(dataSnapshot().getKey){
dailyVerse = dataSnapshot.getValue(DailyVerse.class);
Log.d("TEST","Changed object: "+dailyVerse);
if (dailyVerse!=null){
Log.d("TEST","Changed values: "+dailyVerse.verse_title+", "+dailyVerse.verse_content);
}
}
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("Firebase", "onCancelled", databaseError.toException());
Toast.makeText(context, "Failed to load verse",
Toast.LENGTH_SHORT).show();
}
};
mRef.addChildEventListener(childEventListener);
You should subscribe to events from parent object and check the key at events listener.
DataRef = FirebaseDatabase.getInstance().getReference().child("Users");
DataRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String acctname = (String)dataSnapshot.child("Name").getValue();
String acctmail = (String)dataSnapshot.child("Email").getValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
When using a firebase model class, You have to include an empty constructor for firebase to work with.
Try adding it as
public class DailyVerse {
public DailyVerse(){
}
}
ArrayList<EventsResponse> usersEventsList = new ArrayList<>();
dummyIdsArrayList = new ArrayList<>();
Iterator<DataSnapshot> eventsIterator = dataSnapshot.getChildren().iterator();
while (eventsIterator.hasNext()) {
DataSnapshot eventsSnapShot = eventsIterator.next();
EventsResponse eventsResponse = new EventsResponse();
eventsResponse.setEventName(eventsSnapShot.getKey());
Iterator<DataSnapshot> eventChildIterator = eventsSnapShot.getChildren().iterator();
while (eventChildIterator.hasNext()) {
DataSnapshot eventchildSnapshot = eventChildIterator.next();
if (eventchildSnapshot.getKey().equals("is_recursive")) {
eventsResponse.setRecursive((Boolean) eventchildSnapshot.getValue());
} else if (eventchildSnapshot.getKey().equals("is_notif")) {
eventsResponse.setNotif((Boolean) eventchildSnapshot.getValue());
} else if (eventchildSnapshot.getKey().equals("is_editable")) {
eventsResponse.setEditable((Boolean) eventchildSnapshot.getValue());
} else if (eventchildSnapshot.getKey().equals("is_visible")) {
eventsResponse.setVisible((Boolean) eventchildSnapshot.getValue());
} else if (eventchildSnapshot.getKey().equals("name")) {
eventsResponse.setName(eventchildSnapshot.getValue().toString());
} else if (eventchildSnapshot.getKey().equals("date")) {
Iterator<DataSnapshot> eventChildDateIterator = eventchildSnapshot.getChildren().iterator();
while (eventChildDateIterator.hasNext()) {
DataSnapshot eventchildDateSnapshot = eventChildDateIterator.next();
if (eventchildDateSnapshot.getKey().equals("day")) {
eventsResponse.setDay(eventchildDateSnapshot.getValue().toString());
} else if (eventchildDateSnapshot.getKey().equals("month")) {
eventsResponse.setMonth(eventchildDateSnapshot.getValue().toString());
} else if (eventchildDateSnapshot.getKey().equals("year")) {
eventsResponse.setYear(eventchildDateSnapshot.getValue().toString());
}
}
} else {
eventsResponse.setNotif(true);
eventsResponse.setVisible(false);
}
}
if you are not able to fetch data,then you can fetch the data in that format.

Categories

Resources