getAllLandmarks() returns nothing because table is empty as shown in database inspector. I am trying to set up roomDB in Android Studio and creates googlemap marker by extracting the lang and longitude from the database.
Problem ScreenShot:
What it should look like
MapFragment.java
package com.example.whereswaldo.ui.notifications;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import com.example.whereswaldo.Landmarks;
import com.example.whereswaldo.LandmarksDAO;
import com.example.whereswaldo.R;
import com.example.whereswaldo.database;
import com.example.whereswaldo.databinding.FragmentNotificationsBinding;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import java.util.List;
public class MapFragment extends Fragment {
private FragmentNotificationsBinding binding;
GoogleMap mGoogleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_notifications, container, false);
SupportMapFragment supportMapFragment=(SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.google_map);
// Retrieve landmarks data from the database
LandmarksDAO landmarksDAO = database.getDatabase(getContext()).landmarksDAO();
LiveData<List<Landmarks>> landmarksLiveData = landmarksDAO.getAllLandmarks();
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
landmarksLiveData.observe(getViewLifecycleOwner(), landmarks -> {
// Loop through the landmarks and create markers
for (Landmarks landmark : landmarks) {
LatLng position = new LatLng(landmark.getLatitude(), landmark.getLongitude());
String title = landmark.getName();
googleMap.addMarker(new MarkerOptions().position(position).title(title));
CameraPosition cameraPosition = CameraPosition.builder()
.target(position)
.zoom(16)
.bearing(0)
.tilt(45)
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
}
});
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
database.java
package com.example.whereswaldo;
import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Database(entities = {Landmarks.class}, version = 1, exportSchema = false)
public abstract class database extends RoomDatabase {
public abstract LandmarksDAO landmarksDAO();
private static volatile database INSTANCE;
private static final int NUMBER_OF_THREADS = 4;
public static final ExecutorService databaseWriteExecutor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
public static database getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (database.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), database.class, "landmarks").createFromAsset("landmarks.db").build();
}
}
}
return INSTANCE;
}
}
LandmarkDAO.java
package com.example.whereswaldo;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
@Dao
public interface LandmarksDAO {
@Insert(onConflict = OnConflictStrategy.ABORT)
public void insert(Landmarks landmarks);
@Query("SELECT * FROM landmarks")
LiveData<List<Landmarks>> getAllLandmarks();
@Query("SELECT * FROM landmarks WHERE Name = :Name")
LiveData<Landmarks> findLandmark(String Name);
}
I have tried putting Log comments to see where is the problem. It seems i am able to get into the landmarksLiveData.observe(getViewLifecycleOwner(), landmarks -> { but not the for loops afterwards.
I also tried adding this in the getDatabase() from database.java SupportSQLiteDatabase sdb = INSTANCE.getOpenHelper().getWritableDatabase();
Hence, the getAllLandmarks() returns nothing.
I would like to know where is the problem lying? Thank you!


Your getAllLandmarks() function is returning nothing because there is nothing in the database and to add it you must create objects of all the landmarks
Add this code before