How can I retrieve list data in Firebase Android - android

I want to retrieve data in screenshot nodes in my Firebase Real Time. How can I do that?
This is my database
I have tried this
mDataref.child("Game").orderByChild("ten").equalTo(ten).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
String ScreenURLdata = String.valueOf(dataSnapshot.child("screenshot").getValue());
Game game = new Game();
game.setScreenshotURL(ScreenURLdata);
String ScreenURL = game.getScreenshotURL();
Log.d("scre",ScreenURL);
Model_ScreenShot model_screenShot = new Model_ScreenShot(ScreenURL);
listscreenshot.add(model_screenShot);
adapter = new Adapter_ScreenShot(listscreenshot, getApplicationContext());
viewPager.setPadding(-20, -15, 150, 0);
viewPager.setAdapter(adapter);
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
The logcat showed this : 2019-12-03 11:36:41.443 8144-8144/com.example.gamingshop D/scre: {s1=https://firebasestorage.googleapis.com/v0/b/skullgaming-5dab3.appspot.com/o/Game%2FBorderlands-3-screenshots-78.jpg?alt=media&token=980c6d95-f4a6-4b13-89ba-1ffec3af07d9,
s2=https://firebasestorage.googleapis.com/v0/b/skullgaming-5dab3.appspot.com/o/Game%2Fbl3_06.jpg?alt=media&token=66a8673e-ed2e-4a23-9fff-d6ef10e16a49}
but it does not work when I try to show it to view.

instead of pushing data use custom Id UUID for unique key or use user's id and simply fetch data using ref.child(userid).child("screenshot")
add read write true in storage Firebase rules

Related

list containing data from firebase database not displayed

I'm trying to create a Snapchat clone. I'm stuck at the part where the activity is supposed to display a list of email ids of users who have sent snaps to the current user.In fact the message is not even getting logged into logcat. Here is the code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blank);
list=findViewById(R.id.emails);
id=new ArrayList<>();
ArrayAdapter<String> ada=new ArrayAdapter(this,android.R.layout.simple_list_item_1,id);
list.setAdapter(ada);
try {
FirebaseDatabase.getInstance().getReference().child("users").child(mAuth.getCurrentUser().getUid()).child("snaps").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Log.i("first", "created");
Log.i("pug", dataSnapshot.child("from").getValue().toString());
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
catch (Exception e){
Log.e("error",e.getMessage());
}
}
}
Here is the screenshot of the database
In your database structure, it shows that you are not using the uid, instead you are using a random id generated using the push() method, you need to add data using the uid instead to be able to access it when retrieving.

Firebase: Query inner child without knowing the parent

So I want to know if there is a way to query based on inner child without having to know the parent in Firebase Database? Here is the exact situation:
As shown in the image, I want to query the entire data snapshot where title = "bj", since I have pushed them automatically so Im unaware of the Key (or parent in this case). Im curious if Fb Query has anyway to look into the nested childs directly? TIA
private void fetchPinnedLocationsForSearchbar(String key) {
Query query = MyApplication.database.getReference("/pins/").child("title").orderByKey().equalTo(key);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Log.e("-->X-DATA-FROM-QUERY", dataSnapshot.toString());
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
final BusinessModel pinItModel = new BusinessModel();
if (((Boolean) dataSnapshot1.child("ifBusiness").getValue())) {
if
BusinessModel pinItModelTest = dataSnapshot1.getValue(BusinessModel.class);
Log.e("->PicCount", pinItModelTest.getPicCount() + "");
//
_data.add(pinItModelTest);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
So I found the answer from another post on StackOverFlow that suggested the solution should be something like:
FirebaseDatabase.getInstance().getReference("pins").orderByChild("title").equalTo("bj").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Log.e("-->X--ChildAddedQUERY", dataSnapshot.toString()+"---"+s);
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

How to display chat users list in the chronological order using firebase?

I have a requirement of arranging chat users based on their most recent messages which they have sent to the user using firebase chat. I have tried using order by the query based on timestamp, but it's not working for me. Please find my code below and database structure here
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
final Query databaseReference = FirebaseDatabase.getInstance().getReference().child(FirebaseConstants.USERS).child(mFirebaseUser).child(FirebaseConstants.COLLEGEMATES).orderByChild("timestamp");
final DatabaseReference databaseReference1 = FirebaseDatabase.getInstance().getReference().child(FirebaseConstants.USERS);
FirebaseRecyclerOptions<Student> options = new FirebaseRecyclerOptions.Builder<Student>().setQuery(databaseReference, Student.class).setLifecycleOwner(getActivity()).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Student, FirebaseListViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final FirebaseListViewHolder holder, final int position, #NonNull final Student model) {
try {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(final DataSnapshot dataSnapshot) {
}
I solved this problem by adding the child at every message named "createdAt" added with this snippet:
String patternForDateExtended = "yyyyMMddHHmmss";
DateFormat dff = new SimpleDateFormat(patternForDateExtended);
Date today = Calendar.getInstance().getTime();
String timeString = df.format(today);
String rightNowEverything = dff.format(today)+rightNow.get(Calendar.MILLISECOND);
for example the output will be a crescent number like 20190404132156825
and then ordering the messages with this:
myRef.orderByChild("createdAt").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
// adding to the recycleview everything.
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Hope that will help you, keep coding :)

Allow user to mark attendance Once per day

I am working on Attendance Management System.I want the user to mark attendance once per day.
How can i put a constraint for allowing users to mark once per day..Either i restrict the button after clicking or i can do something in database logic.
DBreference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
String present ="Present";
DBreference.child("Attendance").child(currentDateTimeString).setValue(present);
Toast.makeText(ProfileAct.this, "Marked as Present", Toast.LENGTH_SHORT).show();
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
break;
case R.id.cardLeave:
DBreference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
String present ="On Leave";
DBreference.child("Attendance").child(currentDateTimeString).setValue(present);
Toast.makeText(ProfileAct.this, "Marked As Leave", Toast.LENGTH_SHORT).show();
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});`
Add a listener to your path only for the day not the time:
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
e.g. DBreference.child("Attendance").(fDate)
And add the value "Present" or "Absent" on this path.
Then attach a ValueEventListener on this path. And you get a callback of a value present in this path in the onDataChange, you can disable your button or view if there is a value found.
ValueEventListener Documentation
Your database need to create daily entries according to date and then you will check from your front end side that user is attended or not if yes then you need to store a boolean flag in database and you will have to check before user giving attendance that he/she have already gave or not using that flag.
The best thing is to actually have a node for attendance. Firebase encourages flat data than nested data. Then you can create a child for each day, with a user to check for the day. This way even your data querying is easier. Here is an example
attendance : {
"12/03/2019" : {
"UID" : "time stamp"
},
"11/03/2019" : {
"UID" : "time stamp"
}
}

event listener doesn't trigger when data changes firebase android

As i told you in question when some data changes in firebase realtime databese nothing heppens on datachange() method.
I tried every eventlistener child,value and single but none of them worked.Please help me with that problem i am trying to solve it about 5 hours.
public void StatusTextCheck(){
typing_status.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Toast.makeText(MainActivity.this,"Event listener triggered.",Toast.LENGTH_LONG).show();
TypingStatus type = dataSnapshot.getValue(TypingStatus.class);
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Here is the reference define:
private DatabaseReference user_dbref,
message_dbref,typing_status_dbref,typing_status;
user_dbref = database.getReference("Users");
message_dbref = database.getReference("Message");
typing_status = database.getReference("Typing_Status");
#PradyumanDixit

Categories

Resources