如何让我的可滚动按钮显示用户在搜索栏中输入的内容?

时间:2017-12-06 03:14:15

标签: java android android-layout debugging

onPostExecute()内的GetNearbyPlacesData.java方法内,List<HashMap<String, String>> nearbyPlacesList = null;会通过点击SEARCH来存储用户输入的所有搜索内容。

我想在可滚动按钮上显示用户在搜索栏中输入的内容。例如,如果用户键入&#34; pizza&#34;,那么披萨应该出现在这些按钮上。

我该如何做到这一点?我猜测android:text="something"文件中所有<Buttonactivity_maps.xml所说的行应该采用动态方法。但是我该怎么做呢?

enter image description here

此处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));
        }
    }
}

此处有activity_maps.xml个文件:

<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>

1 个答案:

答案 0 :(得分:0)

如果你可以访问mMap,你应该在该类的某个地方有一个句柄,如果没有,你就必须通过它。

一旦掌握了布局,只需找到父LinearLayout(通过给它一个ID)并写一个for循环到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); 
}

将数据存储在一个哈希映射列表中似乎也是一种令人难以置信的混乱方式。考虑一堂课。如果您需要更多,那么您应该使您的问题更具体。