Java tutorial
/* * MIT License * * Copyright (c) 2016. Agmo Studio * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.agmostudio.pasarmalamlocator; import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; import java.util.ArrayList; import io.nlopez.smartlocation.OnLocationUpdatedListener; import io.nlopez.smartlocation.SmartLocation; public class NearbyFragment extends Fragment { public NearbyFragment() { // Required empty public constructor } NearbyAdapter adapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_nearby, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // find all the UI from the fragment_nearby layout RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); // create adapter and setup our list adapter = new NearbyAdapter(); recyclerView.setAdapter(adapter); // get the Firebase and listen to any changes to "pasarmalam" table FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference table = database.getReference("pasarmalam"); table.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // convert the Firebase data into PasarMalam ArrayList<PasarMalam> list = new ArrayList<>(); for (DataSnapshot snapshot : dataSnapshot.getChildren()) { list.add(snapshot.getValue(PasarMalam.class)); } adapter.replaceAll(list); // add into our adapter to display UI } @Override public void onCancelled(DatabaseError error) { error.toException().printStackTrace(); } }); } @Override public void onResume() { super.onResume(); startLocationUpdate(); // start to get location when app resumes } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 100 && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { startLocationUpdate(); // we have permission, try to get user location } } private void startLocationUpdate() { if (ContextCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { // we have permission, try to get user location. we only need 1 GPS location SmartLocation.LocationControl smartLocation = SmartLocation.with(getContext()).location(); smartLocation.oneFix().start(new OnLocationUpdatedListener() { @Override public void onLocationUpdated(Location location) { adapter.setCurrentLocation(location); // tell the adapter of our location } }); // if user had a location previous, use it first to quickly display the nearest adapter.setCurrentLocation(smartLocation.getLastLocation()); } else { // we dont have permission or user have revoke it, try to ask user requestPermissions(new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, 100); } } }