自定义信息窗口显示来自json android studio的重复数据

时间:2018-08-12 04:54:57

标签: android android-studio android-maps-v2 infowindow

我真的是Android编程的新手。

我可以问一个问题,为什么我的数据都一样。我从json获取数据并将其放在自定义信息窗口中,为什么它们都是重复的,但标记却不是。 对不起,我无法发送图片,因为我是第一次来这里提问

这是我的适配器类

适配器类

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private Context context;
private TextView tvnama, tvalamat, tvkursi, tvmeja, tvstop, tvlain, tvket;
public static String stonama, stoalamat, kursisto, mejasto, stopkontaksto, lainlainsto, keterangansto, id;
public MarkerInfoWindowAdapter(Context context){
    this.context = context.getApplicationContext();
}

@Override
public View getInfoWindow(Marker marker){
    return null;
}
@Override
public View getInfoContents(Marker marker){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.map_marker_info_window, null);
    tvnama = (TextView) v.findViewById(R.id.tv_namasto);
    tvalamat = (TextView) v.findViewById(R.id.tv_alamatsto);
    tvkursi = (TextView) v.findViewById(R.id.tv_kursisto);
    tvmeja = (TextView) v.findViewById(R.id.tv_mejasto);
    tvstop = (TextView) v.findViewById(R.id.tv_stopkontaksto);
    tvlain = (TextView) v.findViewById(R.id.tv_lainlainsto);
    tvket = (TextView) v.findViewById(R.id.tv_keterangansto);
    tvnama.setText("Nama STO: "+stonama);
    tvalamat.setText("Alamat STO: "+stoalamat);
    tvkursi.setText("Fasilitas Kursi: "+kursisto);
    tvmeja.setText("Fasilitas Meja: "+mejasto);
    tvstop.setText("Fasilitas Stop Kontak: "+stopkontaksto);
    tvlain.setText("Fasilitas Lain Lain: "+lainlainsto);
    tvket.setText("Keterangan: "+keterangansto);
    return v;
}
}

MapsActivity``

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private String[] nama_sto, alamat_sto, kursi, meja, stop, lain, keterangan, id;
int numData;
LatLng latLng[];
Boolean markerD[];
private Double[] latitude, longitude;

private void getLokasi() {
    String url = "http://huracan97.site/api/GetSTO.php";
    JsonArrayRequest request = new JsonArrayRequest
            (Request.Method.GET, url, new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    numData = response.length();
                    Log.d("DEBUG_", "Parse JSON");
                    latLng = new LatLng[numData];
                    markerD = new Boolean[numData];
                    id = new String[numData];
                    nama_sto = new String[numData];
                    alamat_sto = new String[numData];
                    kursi = new String[numData];
                    meja = new String[numData];
                    stop = new String[numData];
                    lain = new String[numData];
                    keterangan = new String[numData];
                    latitude = new Double[numData];
                    longitude = new Double[numData];

                    for (int i = 0; i < numData; i++) {
                        try {
                            JSONObject data = response.getJSONObject(i);
                            id[i] = data.getString("id_sto");
                            latLng[i] = new LatLng(data.getDouble("latitude"),
                                    data.getDouble("longitude"));
                            nama_sto[i] = data.getString("nama_sto");
                            alamat_sto[i] = data.getString("alamat_sto");
                            kursi[i] = data.getString("fasilitas_kursi");
                            meja[i] = data.getString("fasilitas_meja");
                            stop[i] = data.getString("fasilitas_stopkontak");
                            lain[i] = data.getString("fasilitas_lainlain");
                            keterangan[i] = data.getString("keterangan");
                            latitude[i] = data.getDouble("latitude");
                            longitude[i] = data.getDouble("longitude");
                            MarkerInfoWindowAdapter.stonama = nama_sto[i];
                            MarkerInfoWindowAdapter.stoalamat = alamat_sto[i];
                            MarkerInfoWindowAdapter.kursisto = kursi[i];
                            MarkerInfoWindowAdapter.mejasto = meja[i];
                            MarkerInfoWindowAdapter.stopkontaksto = stop[i];
                            MarkerInfoWindowAdapter.lainlainsto = lain[i];
                            MarkerInfoWindowAdapter.keterangansto = keterangan[i];
                            markerD[i] = false;
                            mMap.addMarker(new MarkerOptions()
                                    .position(latLng[i])

                            );

                        } catch (JSONException je) {}

                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng[i], 15.5f));
                    }



                }

0 个答案:

没有答案