diff --git a/app/src/main/java/com/a1/nextlocation/data/Location.java b/app/src/main/java/com/a1/nextlocation/data/Location.java index 24422bb..681f816 100644 --- a/app/src/main/java/com/a1/nextlocation/data/Location.java +++ b/app/src/main/java/com/a1/nextlocation/data/Location.java @@ -1,11 +1,14 @@ package com.a1.nextlocation.data; +import android.os.Parcel; +import android.os.Parcelable; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import org.jetbrains.annotations.NotNull; -public class Location { +public class Location implements Parcelable { @NonNull private String name; @@ -31,6 +34,25 @@ public class Location { this(name,getStringFromCoordinates(latCoord,longCoord),description,imageUrl); } + protected Location(Parcel in) { + name = in.readString(); + coordinates = in.readString(); + description = in.readString(); + imageUrl = in.readString(); + } + + public static final Creator CREATOR = new Creator() { + @Override + public Location createFromParcel(Parcel in) { + return new Location(in); + } + + @Override + public Location[] newArray(int size) { + return new Location[size]; + } + }; + @NotNull public String getName() { return name; @@ -85,4 +107,16 @@ public class Location { return lat1 + "," + long1; } + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(name); + dest.writeString(coordinates); + dest.writeString(description); + dest.writeString(imageUrl); + } } diff --git a/app/src/main/java/com/a1/nextlocation/fragments/LocationFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/LocationFragment.java index 133665f..4bc159d 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/LocationFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/LocationFragment.java @@ -3,25 +3,54 @@ package com.a1.nextlocation.fragments; import android.os.Bundle; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.a1.nextlocation.R; +import com.a1.nextlocation.data.Location; +import com.a1.nextlocation.recyclerview.CouponAdapter; +import com.a1.nextlocation.recyclerview.LocationAdapter; +import com.a1.nextlocation.recyclerview.LocationListManager; + +import java.util.ArrayList; +import java.util.List; public class LocationFragment extends Fragment { + private RecyclerView locationRecyclerView; + private LocationAdapter locationAdapter; + private RecyclerView.LayoutManager layoutManager; + private List locationList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_location, container, false); + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + + View view = inflater.inflate(R.layout.fragment_location, container, false); + + locationRecyclerView = view.findViewById(R.id.locationRecyclerView); + locationRecyclerView.setHasFixedSize(true); + layoutManager = new LinearLayoutManager(this.getContext()); + LocationListManager locationListManager = new LocationListManager(this.getContext()); + locationListManager.load(); + locationList = locationListManager.getLocationList(); + locationAdapter = new LocationAdapter(this.getContext(), locationList, clickedPosition -> { + LocationDetailFragment locationDetailFragment = new LocationDetailFragment(); + Bundle locationBundle = new Bundle(); + locationBundle.putParcelable("location", locationList.get(clickedPosition)); + locationDetailFragment.setArguments(locationBundle); + ((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, locationDetailFragment).addToBackStack(null).commit(); + }); + locationRecyclerView.setLayoutManager(layoutManager); + locationRecyclerView.setAdapter(locationAdapter); + return view; } } \ No newline at end of file diff --git a/app/src/main/java/com/a1/nextlocation/recyclerview/LocationAdapter.java b/app/src/main/java/com/a1/nextlocation/recyclerview/LocationAdapter.java index 5ef367f..9148dc7 100644 --- a/app/src/main/java/com/a1/nextlocation/recyclerview/LocationAdapter.java +++ b/app/src/main/java/com/a1/nextlocation/recyclerview/LocationAdapter.java @@ -18,7 +18,7 @@ public class LocationAdapter extends RecyclerView.Adapter locationList; - private CouponAdapter.OnItemClickListener clickListener; + private OnItemClickListener clickListener; public interface OnItemClickListener { void onItemClick(int clickedPosition); @@ -31,6 +31,7 @@ public class LocationAdapter extends RecyclerView.Adapter location, CouponAdapter.OnItemClickListener listener){ + public LocationAdapter(Context context, List location, OnItemClickListener listener){ this.appContext = context; this.locationList = location; this.clickListener = listener; diff --git a/app/src/main/res/layout/fragment_location.xml b/app/src/main/res/layout/fragment_location.xml index 11a47f1..b64896e 100644 --- a/app/src/main/res/layout/fragment_location.xml +++ b/app/src/main/res/layout/fragment_location.xml @@ -31,6 +31,7 @@