Inside the onPostExecute()
method inside GetNearbyPlacesData.java
, List<HashMap<String, String>> nearbyPlacesList = null;
is what will store all the searches that the user types in following by hitting SEARCH
.
I would like to display what the user types into the search bar onto the scrollable buttons. For example, if the user types in "pizza", then pizza should appear on those buttons.
How would I make this happen? I'm guessing the line that says android:text="something"
's on all <Button
's inside the activity_maps.xml
file should have a dynamic approach. But how would I go about doing that?
Here's GetNearbyPlacesData.java
:
import android.os.AsyncTask;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
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.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GetNearbyPlacesData extends AsyncTask<Object, String, String> {
String googlePlacesData;
GoogleMap mMap;
String url;
@Override
protected void onPostExecute(String result) {
Log.d("GooglePlacesReadTask", "onPostExecute Entered");
// goes in here whatever you search
List<HashMap<String, String>> nearbyPlacesList = null;
DataParser dataParser = new DataParser();
nearbyPlacesList = dataParser.parse(result);
saveNearbyPlaces(nearbyPlacesList);
ShowNearbyPlaces(nearbyPlacesList);
Log.d("GooglePlacesReadTask", "onPostExecute Exit");
}
private void saveNearbyPlaces(List<HashMap<String, String>> nearbyPlacesList){
DatabaseReference myRootDBref = FirebaseDatabase.getInstance().getReference();
for (int i = 0; i < nearbyPlacesList.size(); i++){
Log.d("onPostExecute","Saving location " + i );
Map<String, String> values = new HashMap<>();
Map<String, String> waittime = new HashMap<>();
//dummy waittime
waittime.put("Waittime" , "10" );
values = nearbyPlacesList.get(i);
//an if statement should encapsulate the creation of new entry store
//if(values.get("vicinity") != myRootDBref.child(values.get("vicinity")).getKey() )
// if(values.get("place_name") != myRootDBref.child(values.get("vicinity")).child(values.get("place_name")).getKey())
myRootDBref.child(values.get("place_name")).child(values.get("vicinity")).child("10").child("waitime").push().setValue(waittime);
}
};
private void ShowNearbyPlaces(List<HashMap<String, String>> nearbyPlacesList) {
DatabaseReference myRootDBref = FirebaseDatabase.getInstance().getReference();
for (int i = 0; i < nearbyPlacesList.size(); i++) {
Log.d("onPostExecute","Entered into showing locations");
final MarkerOptions markerOptions = new MarkerOptions();
final HashMap<String, String> googlePlace = nearbyPlacesList.get(i);
double lat = Double.parseDouble(googlePlace.get("lat"));
double lng = Double.parseDouble(googlePlace.get("lng"));
final String placeName = googlePlace.get("place_name");
final String vicinity = googlePlace.get("vicinity");
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
//The wait time will need to go inside parenthesis
markerOptions.title(placeName + " : " + vicinity
/* myRootDBref.child(googlePlace.get("vicinity")).child(googlePlace.get("place_name")).toString()*/ );
mMap.addMarker(markerOptions);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
}
}
}
Here's activity_maps.xml
file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mancj.example.MapsActivity">
</fragment>
<LinearLayout
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
>
<EditText
android:hint="Search"
android:id="@+id/searchBar"
android:layout_width="289dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="onSearch"
android:text="SEARCH" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="480dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="something"
android:id="@+id/button2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="something"
android:id="@+id/button3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="something"
android:id="@+id/button4"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>