【问题标题】:How would I make my scrollable buttons display what the user types in search bar?如何让我的可滚动按钮显示用户在搜索栏中键入的内容?
【发布时间】:2017-12-06 03:14:15
【问题描述】:

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

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

我将如何做到这一点?我猜android:text="something"'s on all <Button's 在activity_maps.xml 文件中的行应该有一个动态方法。但是我该怎么做呢?

这里是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>

【问题讨论】:

    标签: java android android-layout debugging


    【解决方案1】:

    如果您可以访问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); 
    }
    

    将数据存储在哈希表列表中似乎也是一种非常混乱的方式。考虑一个类。如果你需要更多,那么你应该让你的问题更具体。

    【讨论】:

    • 我只想让用户在搜索栏中输入的任何内容都显示在按钮上。我想我明白你在说什么。你的意思是通过编写这个for循环并给父LinearLayout一个ID,按钮将在用户点击SEARCH时动态创建,这取决于地图找到了多少“披萨”(如我的示例中)?例如,如果它找到三个“披萨”,则将创建三个显示“披萨”的按钮。这是你的意思吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2018-06-08
    相关资源
    最近更新 更多