为什么我无法从数组中获取正确的日期?
why I cannot get correct date from array?
为什么我无法从数组中获取正确的日期?
我试图从数组中获取正确的日期,我使用 Log.d 方法,日期在数组中,但是当我点击侦听器时,我总是得到最后一个日期。问题出在哪里?
下面是我的代码:
private void loadMaps() {
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Load...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_MAPSHOW,
new Response.Listener<String>() {
@Override
public void onResponse(String showmaplocation) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(showmaplocation);
JSONArray jsonArray = jsonObject.getJSONArray("map");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject mapShow = jsonArray.getJSONObject(i);
mapShowArrayList.add(new MapShow(
mapShow.getInt("MapID"),
mapShow.getInt("UserID"),
mapShow.getString("MapLat"),
mapShow.getString("MapLng"),
mapShow.getString("MapTitle"),
mapShow.getString("MapPhoto"),
mapShow.getInt("MapType")
));
final String info = mapShowArrayList.get(i).getTitle();
double latitude = Double.parseDouble(mapShowArrayList.get(i).getLat());
double longitude = Double.parseDouble(mapShowArrayList.get(i).getLng());
//Log.d("data:" , latitude +"," + longitude);
LatLng nowlocat = new LatLng(latitude, longitude);
//Log.d("Location", String.valueOf(nowlocat));
switch (mapShowArrayList.get(i).getMtype()) {
case 1:
icon = R.drawable.police;
break;
case 2:
icon = R.drawable.ambulance;
break;
case 3:
icon = R.drawable.firetruck;
break;
}
boolean imageCreated = false;
Bitmap bmp = null;
imageCreated = true;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
bmp = Bitmap.createBitmap(200, 120, conf);
Canvas canvas1 = new Canvas(bmp);
Paint color = new Paint();
Paint wordcolor = new Paint();
color.setTypeface(Typeface.SANS_SERIF);
color.setTextAlign(Paint.Align.LEFT);
wordcolor.setTextSize(30);
int width = (int)wordcolor.measureText(mapShowArrayList.get(i).getTitle());
color.setColor(Color.WHITE);
wordcolor.setColor(Color.BLACK);
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
Bitmap imageBitmap = BitmapFactory.decodeResource(getResources(), icon,opt);
Bitmap resized = Bitmap.createScaledBitmap(imageBitmap, 80, 80, true);
canvas1.drawBitmap(resized, 40, 40, color);
canvas1.drawRoundRect(new RectF(30,-40,width+50,50),10,10, color);
canvas1.drawText(mapShowArrayList.get(i).getTitle(), 40, 40, wordcolor);
URLIMAGE = mapShowArrayList.get(i).getiConPicture();
MarkerOptions options = new MarkerOptions()
.draggable(false)
.icon(BitmapDescriptorFactory.fromBitmap(bmp))
.position(nowlocat);
marker = mMap.addMarker(options);
marker.showInfoWindow();
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getContext(),info, Toast.LENGTH_SHORT).show();
return false;
}
});
}
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getActivity(), "Error Reading Detail. " + error.toString() , Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
下一行可以得到正确的日期
final String info = mapShowArrayList.get(i).getTitle()
但在那里,我总是得到最后的约会。
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getContext(),info, Toast.LENGTH_SHORT).show();
return false;
}
});
无法在 mMap.setOnMarkerClickListener
中获取值 i
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject mapShow = jsonArray.getJSONObject(i);
mapShowArrayList.add(new MapShow(
mapShow.getInt("MapID"),
mapShow.getInt("UserID"),
mapShow.getString("MapLat"),
mapShow.getString("MapLng"),
mapShow.getString("MapTitle"),
mapShow.getString("MapPhoto"),
mapShow.getInt("MapType")
));
//double nowLocation = Double.compare(mapShowArrayList.get(i).getMapLatLng());
//ObjectList A[0] =
info = mapShowArrayList.get(i).getTitle();
Log.d("INFO", info);
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getContext(), mapShowArrayList.get(i).getTitle(), Toast.LENGTH_SHORT).show();
return false;
}
});
error: local variables referenced from an inner class must be final or effectively final
Toast.makeText(getContext(), mapShowArrayList.get(i).getTitle(), Toast.LENGTH_SHORT).show();
CustomObject.java
public class CustomObject {
private int MapID;
private int UserID;
private String Lat;
private String Lng;
private String title;
private String iConPicture;
private int Mtype;
public CustomObject(String title) {
this.title = title;
}
public CustomObject() {
}
public int getMapID() {
return MapID;
}
public void setMapID(int mapID) {
MapID = mapID;
}
public int getUserID() {
return UserID;
}
public void setUserID(int userID) {
UserID = userID;
}
public String getLat() {
return Lat;
}
public void setLat(String lat) {
Lat = lat;
}
public String getLng() {
return Lng;
}
public void setLng(String lng) {
Lng = lng;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getiConPicture() {
return iConPicture;
}
public void setiConPicture(String iConPicture) {
this.iConPicture = iConPicture;
}
public int getMtype() {
return Mtype;
}
public void setMtype(int mtype) {
Mtype = mtype;
}
我使用此方法时,会在空对象引用上出现错误 com.stanley.ifunpot.models.CustomObject.getTitle()'
代码
private CustomObject customObject;
MarkerOptions options = new MarkerOptions().draggable(false).title(customObject.getTitle()).icon(BitmapDescriptorFactory.fromBitmap(bmp)) .position(nowlocat);
marker = mMap.addMarker(options);
marker.setTag(new CustomObject(MAP_ID,MapLat,MapLng,MapTitle,MapPhoto,MapType));
marker.showInfoWindow();
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
CustomObject data = (CustomObject) marker.getTag();
Toast.makeText(getContext(),String.valueOf(data.getMtype()), Toast.LENGTH_SHORT).show();
return false;
}
});
MarkerOptions options = new MarkerOptions().draggable(false).title("Marker Title").icon(BitmapDescriptorFactory.fromBitmap(bmp)) .position(nowlocat);
marker = mMap.addMarker(options);
marker.setTag(new CustomObject());
marker.showInfoWindow();
在检索它时,您可以尝试以下操作:-
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
CustomObject data = (CustomObject) amarker.getTag();
Toast.makeText(getContext(),data.getInfo(), Toast.LENGTH_SHORT).show();
return false;
}
});
您可以在 CustomObject
中取出所有需要的东西,例如 info
、latitude
、longitude
等,然后如图所示取回。
private CustomObject customObject = new CustomObject(MAP_ID,MapLat,MapLng,MapTitle,MapPhoto,MapType);
MarkerOptions options = new MarkerOptions().draggable(false).title(customObject.getTitle()).icon(BitmapDescriptorFactory.fromBitmap(bmp)) .position(nowlocat);
marker = mMap.addMarker(options);
marker.setTag(customObject);
marker.showInfoWindow();
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
CustomObject data = (CustomObject) marker.getTag();
Toast.makeText(getContext(),String.valueOf(data.getMtype()), Toast.LENGTH_SHORT).show();
return false;
}
});
在你的代码中这样做!
我试图从数组中获取正确的日期,我使用 Log.d 方法,日期在数组中,但是当我点击侦听器时,我总是得到最后一个日期。问题出在哪里?
下面是我的代码:
private void loadMaps() {
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Load...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_MAPSHOW,
new Response.Listener<String>() {
@Override
public void onResponse(String showmaplocation) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(showmaplocation);
JSONArray jsonArray = jsonObject.getJSONArray("map");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject mapShow = jsonArray.getJSONObject(i);
mapShowArrayList.add(new MapShow(
mapShow.getInt("MapID"),
mapShow.getInt("UserID"),
mapShow.getString("MapLat"),
mapShow.getString("MapLng"),
mapShow.getString("MapTitle"),
mapShow.getString("MapPhoto"),
mapShow.getInt("MapType")
));
final String info = mapShowArrayList.get(i).getTitle();
double latitude = Double.parseDouble(mapShowArrayList.get(i).getLat());
double longitude = Double.parseDouble(mapShowArrayList.get(i).getLng());
//Log.d("data:" , latitude +"," + longitude);
LatLng nowlocat = new LatLng(latitude, longitude);
//Log.d("Location", String.valueOf(nowlocat));
switch (mapShowArrayList.get(i).getMtype()) {
case 1:
icon = R.drawable.police;
break;
case 2:
icon = R.drawable.ambulance;
break;
case 3:
icon = R.drawable.firetruck;
break;
}
boolean imageCreated = false;
Bitmap bmp = null;
imageCreated = true;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
bmp = Bitmap.createBitmap(200, 120, conf);
Canvas canvas1 = new Canvas(bmp);
Paint color = new Paint();
Paint wordcolor = new Paint();
color.setTypeface(Typeface.SANS_SERIF);
color.setTextAlign(Paint.Align.LEFT);
wordcolor.setTextSize(30);
int width = (int)wordcolor.measureText(mapShowArrayList.get(i).getTitle());
color.setColor(Color.WHITE);
wordcolor.setColor(Color.BLACK);
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
Bitmap imageBitmap = BitmapFactory.decodeResource(getResources(), icon,opt);
Bitmap resized = Bitmap.createScaledBitmap(imageBitmap, 80, 80, true);
canvas1.drawBitmap(resized, 40, 40, color);
canvas1.drawRoundRect(new RectF(30,-40,width+50,50),10,10, color);
canvas1.drawText(mapShowArrayList.get(i).getTitle(), 40, 40, wordcolor);
URLIMAGE = mapShowArrayList.get(i).getiConPicture();
MarkerOptions options = new MarkerOptions()
.draggable(false)
.icon(BitmapDescriptorFactory.fromBitmap(bmp))
.position(nowlocat);
marker = mMap.addMarker(options);
marker.showInfoWindow();
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getContext(),info, Toast.LENGTH_SHORT).show();
return false;
}
});
}
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getActivity(), "Error Reading Detail. " + error.toString() , Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
下一行可以得到正确的日期
final String info = mapShowArrayList.get(i).getTitle()
但在那里,我总是得到最后的约会。
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getContext(),info, Toast.LENGTH_SHORT).show();
return false;
}
});
无法在 mMap.setOnMarkerClickListener
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject mapShow = jsonArray.getJSONObject(i);
mapShowArrayList.add(new MapShow(
mapShow.getInt("MapID"),
mapShow.getInt("UserID"),
mapShow.getString("MapLat"),
mapShow.getString("MapLng"),
mapShow.getString("MapTitle"),
mapShow.getString("MapPhoto"),
mapShow.getInt("MapType")
));
//double nowLocation = Double.compare(mapShowArrayList.get(i).getMapLatLng());
//ObjectList A[0] =
info = mapShowArrayList.get(i).getTitle();
Log.d("INFO", info);
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getContext(), mapShowArrayList.get(i).getTitle(), Toast.LENGTH_SHORT).show();
return false;
}
});
error: local variables referenced from an inner class must be final or effectively final
Toast.makeText(getContext(), mapShowArrayList.get(i).getTitle(), Toast.LENGTH_SHORT).show();
CustomObject.java
public class CustomObject {
private int MapID;
private int UserID;
private String Lat;
private String Lng;
private String title;
private String iConPicture;
private int Mtype;
public CustomObject(String title) {
this.title = title;
}
public CustomObject() {
}
public int getMapID() {
return MapID;
}
public void setMapID(int mapID) {
MapID = mapID;
}
public int getUserID() {
return UserID;
}
public void setUserID(int userID) {
UserID = userID;
}
public String getLat() {
return Lat;
}
public void setLat(String lat) {
Lat = lat;
}
public String getLng() {
return Lng;
}
public void setLng(String lng) {
Lng = lng;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getiConPicture() {
return iConPicture;
}
public void setiConPicture(String iConPicture) {
this.iConPicture = iConPicture;
}
public int getMtype() {
return Mtype;
}
public void setMtype(int mtype) {
Mtype = mtype;
}
我使用此方法时,会在空对象引用上出现错误 com.stanley.ifunpot.models.CustomObject.getTitle()'
代码
private CustomObject customObject;
MarkerOptions options = new MarkerOptions().draggable(false).title(customObject.getTitle()).icon(BitmapDescriptorFactory.fromBitmap(bmp)) .position(nowlocat);
marker = mMap.addMarker(options);
marker.setTag(new CustomObject(MAP_ID,MapLat,MapLng,MapTitle,MapPhoto,MapType));
marker.showInfoWindow();
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
CustomObject data = (CustomObject) marker.getTag();
Toast.makeText(getContext(),String.valueOf(data.getMtype()), Toast.LENGTH_SHORT).show();
return false;
}
});
MarkerOptions options = new MarkerOptions().draggable(false).title("Marker Title").icon(BitmapDescriptorFactory.fromBitmap(bmp)) .position(nowlocat);
marker = mMap.addMarker(options);
marker.setTag(new CustomObject());
marker.showInfoWindow();
在检索它时,您可以尝试以下操作:-
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
CustomObject data = (CustomObject) amarker.getTag();
Toast.makeText(getContext(),data.getInfo(), Toast.LENGTH_SHORT).show();
return false;
}
});
您可以在 CustomObject
中取出所有需要的东西,例如 info
、latitude
、longitude
等,然后如图所示取回。
private CustomObject customObject = new CustomObject(MAP_ID,MapLat,MapLng,MapTitle,MapPhoto,MapType);
MarkerOptions options = new MarkerOptions().draggable(false).title(customObject.getTitle()).icon(BitmapDescriptorFactory.fromBitmap(bmp)) .position(nowlocat);
marker = mMap.addMarker(options);
marker.setTag(customObject);
marker.showInfoWindow();
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
CustomObject data = (CustomObject) marker.getTag();
Toast.makeText(getContext(),String.valueOf(data.getMtype()), Toast.LENGTH_SHORT).show();
return false;
}
});
在你的代码中这样做!