Here is my delivery application, here I am saving the delivery boy's check-in and check-out location into Firestore with a timestamp, when the delivery boy delivers the product and when he check-out from the house. here is my code, this is my showing data into recyclerView class where I am showing the check-in and check-out data in one CardView. This is my java class
package com.example.location.Activites;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.example.location.AdapterClass.AdapterClass;
import com.example.location.AdapterClass.CheckOutAdapterClass;
import com.example.location.ModelClasses.DataModelClass;
import com.example.location.R;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class LastVisitedLocationActivity extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
String userID = mUser.getUid();
private CollectionReference collectionReference = db.collection("UserLocation").document(userID).collection("CheckIn");
private CollectionReference collectionReference1= db.collection("UserLocation").document(userID).collection("CheckOut");
private RecyclerView recyclerView ;
private AdapterClass adapterClass;
private CheckOutAdapterClass checkOutAdapterClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last_visited_location);
recyclerView = findViewById(R.id.RecyclerView);
setupRecyclerView();
setupRecyclerView1();
}
private void setupRecyclerView() {
Query query1 = collectionReference.orderBy("timestamp",Query.Direction.DESCENDING);
FirestoreRecyclerOptions<DataModelClass> options = new FirestoreRecyclerOptions.Builder<DataModelClass>()
.setQuery(query1,DataModelClass.class).build();
adapterClass = new AdapterClass(options);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapterClass);
}
private void setupRecyclerView1() {
Query query2 = collectionReference1.orderBy("timestamp");
FirestoreRecyclerOptions<DataModelClass> options = new FirestoreRecyclerOptions.Builder<DataModelClass>()
.setQuery(query2,DataModelClass.class).build();
checkOutAdapterClass = new CheckOutAdapterClass(options);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(checkOutAdapterClass);
}
@Override
protected void onStart() {
super.onStart();
adapterClass.startListening();
checkOutAdapterClass.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapterClass.stopListening();
checkOutAdapterClass.stopListening();
}
}
this is my xml class
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_linerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"
app:strokeColor="@color/white"
app:strokeWidth="2dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="6dp">
<TextView
android:id="@+id/recycler_CheckInlatitudeLongitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="@font/amarante"
android:paddingStart="15dp"
android:paddingTop="10dp"
android:paddingEnd="15dp"
android:paddingBottom="10dp"
android:text="Latitude and Longitude"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/recyclerView_CheckIndateAndTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="@font/amarante"
android:paddingStart="15dp"
android:paddingTop="10dp"
android:paddingEnd="15dp"
android:paddingBottom="10dp"
android:text="Date and time"
android:textColor="@color/black"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/recycler_CheckInlatitudeLongitude"
app:layout_constraintTop_toBottomOf="@+id/recycler_CheckInlatitudeLongitude" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="8dp"
android:text="Check-In"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/recycler_CheckInlatitudeLongitude"
app:layout_constraintTop_toTopOf="parent"
tools:text="Check-In" />
<TextView
android:id="@+id/recycler_CheckOutlatitudeLongitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="@font/amarante"
android:paddingStart="15dp"
android:paddingTop="10dp"
android:paddingEnd="15dp"
android:paddingBottom="10dp"
android:text="Latitude and Longitude"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/recyclerView_CheckOutdateAndTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="@font/amarante"
android:paddingStart="15dp"
android:paddingTop="10dp"
android:paddingEnd="15dp"
android:paddingBottom="10dp"
android:text="Date and time"
android:textColor="@color/black"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/recycler_CheckInlatitudeLongitude"
app:layout_constraintTop_toBottomOf="@+id/recycler_CheckOutlatitudeLongitude" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Check-out"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.006"
app:layout_constraintStart_toStartOf="@+id/recycler_CheckOutlatitudeLongitude"
app:layout_constraintTop_toBottomOf="@+id/recyclerView_CheckIndateAndTime"
tools:text="Check-Out" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
So the data into firestore is like:- userLocation(collection) --> userAuthID(document) -->CheckIn(collection)-->here i am saving location of check-in CheckOut(Collection)--> here i am saving location of check-out.