Here I have setLayoutManager to be Linear Layout to the Recyclerviewhere we can I am trying to create to show Near By Places in recyclerView in a child Fragment which uses info(Named As hello ) is the place name that it display Viewpager (Parent Fragment is also a fragment of ParentViewPager).My App Got Crashes and Do not pass List of items to be display in recyclerView. Here is ChildFragmentClass Code:
public class NearByFragment extends Fragment implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
RecyclerView mRecycler;
// private GoogleMap mMap;
private GoogleApiClient client;
private LocationRequest locationRequest;
// private Marker currentLocationmMarker;
public static final int REQUEST_LOCATION_CODE = 99;
int PROXIMITY_RADIUS = 8000;
double latitude, longitude;
NearByFragment byFragment;
private FusedLocationProviderClient mFusedLocationProviderClient;
private PlacesClient placesClient;
private List<AutocompletePrediction> predictionList;
private Location mLastKnownLocation;
private LocationCallback locationCallback;
private final float DEFAULT_ZOOM = 15;
GetNearbyPlacesData getNearbyPlacesData;
Object dataTransfer[];
LocationManager locationManager;
NearByModelClass nearByModelClass;
List<NearByModelClass>list=new ArrayList<>();
View view;
public NearByFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_near_by, container, false);
// Inflate the layout for this fragment
byFragment=this;
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getContext());
Places.initialize(getContext(), getString(R.string.google_maps_api));
placesClient = Places.createClient(getContext());
dataTransfer = new Object[2];
getNearbyPlacesData = new GetNearbyPlacesData();
String resturant = "restuarant";
String url = getUrl(latitude, longitude, resturant);
Log.i("url",url);
// dataTransfer[0] = mMap;
dataTransfer[1] = url;
getNearbyPlacesData.execute(dataTransfer);
Toast.makeText(getContext(), "Showing Nearby Restaurants", Toast.LENGTH_SHORT).show();
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 51) {
if(resultCode == RESULT_OK) {
getting device location.
getDeviceLocation();
}
}
}
@SuppressLint("MissingPermission")
private void getDeviceLocation(){
mFusedLocationProviderClient.getLastLocation()
.addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if(task.isSuccessful()){
mLastKnownLocation = task.getResult();
if(mLastKnownLocation != null){
// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
} else {
final LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if(locationResult == null) {
return;
}
mLastKnownLocation = locationResult.getLastLocation();
//mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
mFusedLocationProviderClient.removeLocationUpdates(locationCallback);
}
};
mFusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
}
} else {
Toast.makeText(getContext(), "unable to get last location", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onConnected(@Nullable Bundle bundle) {
locationRequest = new LocationRequest();
locationRequest.setInterval(100);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if(ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED)
{
LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
mLastKnownLocation = location;
Log.d("lat = ",""+latitude);
}
private String getUrl(double latitude , double longitude , String nearbyPlace)
{
StringBuilder googlePlaceUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlaceUrl.append("location="+latitude+","+longitude);
googlePlaceUrl.append("&radius="+PROXIMITY_RADIUS);
googlePlaceUrl.append("&type="+nearbyPlace);
googlePlaceUrl.append("&sensor=true");
googlePlaceUrl.append("&key="+"APIs");
Log.d("MapsActivity", "url = "+googlePlaceUrl.toString());
return googlePlaceUrl.toString();
}
public void preppareList(List<NearByModelClass>list ) {
mRecycler = view.findViewById(R.id.nearby_recycler);
mRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
NearByRecyclerAdapterClass recyclerAdapterClass = new NearByRecyclerAdapterClass(list);
mRecycler.setAdapter(recyclerAdapterClass);
}
Here is my Code of NearPlaces.java class extended by AsynTask I have called function Name PreapparedList of that child fragment to pass list:
class GetNearbyPlacesData extends AsyncTask<Object, String, String> {
private String googlePlacesData;
private GoogleMap mMap;
String url;
NearByModelClass nearByModelClass;
List<NearByModelClass>list=new ArrayList<>();
@Override
protected String doInBackground(Object... objects){
mMap = (GoogleMap)objects[0];
url = (String)objects[1];
DownloadURL downloadURL = new DownloadURL();
try {
googlePlacesData = downloadURL.readUrl(url);
} catch (IOException e) {
e.printStackTrace();
}
return googlePlacesData;
}
@Override
protected void onPostExecute(String s){
List<HashMap<String, String>> nearbyPlaceList;
DataParser parser = new DataParser();
nearbyPlaceList = parser.parse(s);
Log.d("nearbyplacesdata","called parse method");
showNearbyPlaces(nearbyPlaceList);
}
private void showNearbyPlaces(List<HashMap<String, String>> nearbyPlaceList)
{
for(int i = 0; i < nearbyPlaceList.size(); i++)
{
HashMap<String, String> googlePlace = nearbyPlaceList.get(i);
String placeName = googlePlace.get("place_name");
String vicinity = googlePlace.get("vicinity");
double lat = Double.parseDouble( googlePlace.get("lat"));
double lng = Double.parseDouble( googlePlace.get("lng"));
Log.i("hello",placeName);
this is where i want to post my list of data to That Fragment by calling it's method m.preppareList
NearByFragment m =new NearByFragment();
nearByModelClass=new NearByModelClass(placeName,vicinity,lat,lng);
list.add(nearByModelClass);
m.preppareList(list);
}
}
}