我陷入困境。我有一个自定义列表适配器的自定义列表。每个列表项都有一个按钮,如果我点击这个按钮,弹出的对话框应该包含谷歌地图(稍后用标记)。我尝试将片段分配给SupportMapFragment,但Android Studio告诉我,“getSupportFragmentManager()无法解析方法”。我的问题是如何把这个地图(片段)的对话框?如何在对话框中显示google map api v2,当我点击一个按钮
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="hu.bognaroliver.bajaintezemnyek.CustomListAdapter">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_alignParentTop="true" />
<fragment
android:id="@+id/map"
android:layout_alignParentBottom="true"
android:layout_below="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
这是地图。
package hu.bognaroliver.bajaintezemnyek;
import java.util.ArrayList;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.CameraUpdateFactory;
public class CustomListAdapter extends ArrayAdapter<Institute> implements OnMapReadyCallback
{
private LayoutInflater inflater;
private ImageView instituteLogo;
private TextView instituteName;
private TextView instituteAddress;
private TextView instituteCloseTime;
private ImageView instituteType;
private TextView instituteStatus;
private ImageView mapView;
private Dialog mDialog;
private final Context context = getContext();
public CustomListAdapter(@NonNull Context context, ArrayList<Institute> item)
{
super(context, 0, item);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
{
Institute institute = getItem(position);
if(convertView == null)
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.institute_item, parent, false);
instituteLogo = (ImageView) convertView.findViewById(R.id.instituteLogo);
instituteName = (TextView) convertView.findViewById(R.id.instituteName);
instituteAddress = (TextView) convertView.findViewById(R.id.instituteAddress);
instituteCloseTime = (TextView) convertView.findViewById(R.id.instituteCloseTime);
instituteType = (ImageView) convertView.findViewById(R.id.instituteType);
instituteStatus = (TextView) convertView.findViewById(R.id.instituteStatus);
mapView = (ImageView) convertView.findViewById(R.id.mapView);
instituteLogo.setImageResource(R.drawable.cba_logo);
instituteName.setText(institute.getName());
instituteAddress.setText(institute.getAddress());
instituteCloseTime.setText("30 perc múlva");
instituteType.setImageResource(R.drawable.ic_shopping_cart_black_48dp);
instituteStatus.setText(institute.getStatus().toString());
mapView.setImageResource(R.drawable.map_icon);
SupportMapFragment supportMapFragment = (SupoortMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
//MapFragment mapFragment = ((MapFragment) findFragmentById(R.id.map));
//mapFragment.getMapAsync(this);
mDialog = new Dialog(context);
mDialog.setContentView(R.layout.custom_dialog_map);
mDialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) mDialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
mapView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
mDialog.show();
}
});
return convertView;
}
@Override
public void onMapReady(GoogleMap map)
{
//DO WHATEVER YOU WANT WITH GOOGLEMAP
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setTrafficEnabled(false);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(46.1811211,18.9542073) , 13.0f));
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
}
谢谢您的帮助:)
它几乎可以工作,但是当我点击列表项中的按钮时,屏幕只会变灰,就像对话框想要加载的一样。但是在这种情况下,屏幕上没有任何东西只出现灰色的“图层”。 –
第二个人可能会帮助你 –
哦,很好。第二个是为我工作。谢谢 :) –