0

Hello i am still newbie in Firebase, how can i call the inner field latitude and ongitude field? Does me do the correct way structure the database?

enter image description here

I've tried read the docs by Google but it still not worked.

//Write a message to the database
public class LocationActivity extends AppCompatActivity {

    private static final String TAG = "LocationActivity";
    private RecyclerView.LayoutManager layoutManager;
    private Query query;
    private RecyclerView recyclerView;
    public FirebaseRecyclerAdapter<LocationContent,LocationViewHolder> firebaseRecyclerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        // Write a message to the database
        query = FirebaseDatabase.getInstance()
                .getReference()
                .child("Locations")
                .limitToLast(50);

        query.keepSynced(true);

        // set up the RecyclerView
        recyclerView = findViewById(R.id.myrecyclerview);
        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        recyclerView.setHasFixedSize(true);

        // use a linear layout manager
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        FirebaseRecyclerOptions<LocationContent> options =
                new FirebaseRecyclerOptions.Builder<LocationContent>()
                        .setQuery(query, LocationContent.class)
                        .build();

        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    LocationContent lc = ds.child("South Bound").getValue(LocationContent.class);
                    Log.d(TAG,"Error onDataChange");
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
            }
        };
        query.addListenerForSingleValueEvent(valueEventListener);

        firebaseRecyclerAdapter= new FirebaseRecyclerAdapter<LocationContent, LocationViewHolder>(options) {

            @Override
            protected void onBindViewHolder(@NonNull LocationViewHolder holder, int position, @NonNull LocationContent model) {
                String doubleLatitude = Double.toString(model.getLatitude());
                String doubleLongitude = Double.toString(model.getLongitude());

                holder.post_name.setText(model.getName());
                holder.post_latitude.setText(doubleLatitude);
                holder.post_longitude.setText(doubleLongitude);
            }

            @NonNull
            @Override
            public LocationViewHolder onCreateViewHolder(@NonNull ViewGroup viewgroup, int i) {
                View view = LayoutInflater.from(viewgroup.getContext()).inflate(R.layout.location_row,viewgroup,false);

                return new LocationViewHolder(view);
            }
        };

        recyclerView.setAdapter(firebaseRecyclerAdapter);
    }

    @Override
    protected void onStart(){
        super.onStart();
        firebaseRecyclerAdapter.startListening();

    }

    @Override
    protected void onStop() {
        super.onStop();
        firebaseRecyclerAdapter.stopListening();
    }

and this is my model

public class LocationContent {
    private String Name;
    private Double Latitude;
    private Double Longitude;

    public LocationContent(String name, Double latitude, Double longitude) {
        Name = name;
        Latitude = latitude;
        Longitude = longitude;
    }

    public LocationContent(){

    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public Double getLatitude() {
        return Latitude;
    }

    public void setLatitude(Double latitude) {
        Latitude = latitude;
    }

    public Double getLongitude() {
        return Longitude;
    }

    public void setLongitude(Double longitude) {
        Longitude = longitude;
    }

}

1 Answers1

2
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationsRef = rootRef.child("Locations");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            double lat = ds.child("South Bound").child("Latitude").getValue(Double.class);
            double lng = ds.child("South Bound").child("Longitude").getValue(Double.class);
            Log.d(TAG, lat, ", " + lng);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
locationsRef.addListenerForSingleValueEvent(valueEventListener);

Or using your LocationContent class:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationsRef = rootRef.child("Locations");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            LocationContent lc = ds.child("South Bound").getValue(LocationContent.class);
            Log.d(TAG, lc.getLatitude(), ", " + lc.getLongitude());
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
locationsRef.addListenerForSingleValueEvent(valueEventListener);

If you intend to use the second solution, please also be aware about the name of your fields in the database vs. the name of the fields in your LocationContent class. So please also see my answer from the following post:

Edit:

public LocationContent(String name, double latitude, double longitude) {
    Name = name;
    Latitude = latitude;
    Longitude = longitude;
}

public LocationContent(){

}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public double getLatitude() {
    return Latitude;
}

public void setLatitude(double latitude) {
    Latitude = latitude;
}

public double getLongitude() {
    return Longitude;
}

public void setLongitude(double longitude) {
    Longitude = longitude;
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • sorry but `double doubleLatitude = model.getLatitude();` is still return null but log in `onDataChange` is return the value. hm it's weird actually. – shafiq ruslan Mar 24 '19 at 16:06