-1

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?

enter image description here

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>
obsolutemal
  • 55
  • 1
  • 8

1 Answers1

0

You should have a handle on the view somewhere in that class if you can access mMap, if not you would have to pass it through.

Once you have a handle on the layout, just find the parent LinearLayout (by giving it an ID) and write a for loop to produce a button:

layout = (LinearLayout) findViewById(R.id.searchHistory);

// see comment below
for (int i = 0; i < nearbyPlacesList.size(); i++) {
  Button history = new Button(context);
  history.setText("I am a variable");
  layout.addView(history); 
}

It also seems like an incredibly messy way to store data in a list of hashmaps. Consider a class. If you need more than this, then you should make your question more specific.

Honest Objections
  • 773
  • 1
  • 6
  • 13
  • I just want whatever the user types into the search bar to display on the buttons. I think I see what you're saying. Do you mean that by writing this for loop and giving the parent `LinearLayout` an ID, the buttons will be created dynamically upon the user hitting `SEARCH` depending on how many "pizzas" (as in my example) the map finds? For example, if it finds three "pizzas", then three buttons will be created which displays "pizza". Is this what you mean? – obsolutemal Dec 06 '17 at 12:33