为啥我不能从数组中得到正确的日期?

Posted

技术标签:

【中文标题】为啥我不能从数组中得到正确的日期?【英文标题】:why I cannot get correct date from array?为什么我不能从数组中得到正确的日期? 【发布时间】:2021-01-31 12:09:38 【问题描述】:

为什么我不能从数组中得到正确的日期?

我试图从数组中获取正确的日期,我使用 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;
                                    
                                );

【问题讨论】:

infomapShow.getString("MapTitle") 中的值相同吗? 是的,我可以从 mapShow.getString("MapTitle") 中获取值,但是当我将 mapShowArrayList.get(i).getTitle() 放入 mMap.setOnMarkerClickListener 时会出错 【参考方案1】:
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;
                                
                            );

您可以在其中取出 infolatitudelongitudeCustomObject 内的所有需要​​的东西,然后如图所示检索。

【讨论】:

需要创建新的 CustomObject 类吗? @Nadeem Shaikh @Stanley 是的,创建任何自定义对象并填充您需要与标记关联的所有必需内容。 最后一个问题,如何在google map中始终显示每个marker的标题? @Stanley 现在检查代码。初始化时可以设置标记的标题MarkerOptions 我设置了 "MarkerOptions options = new MarkerOptions().draggable(false).title(mapShowArrayList.get(i).getTitle()).icon(BitmapDescriptorFactory.fromBitmap(bmp)) .position (nowlocat);",但它只显示最后一个标记标题【参考方案2】:
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;
                                
                            );

在你的代码中这样做!

【讨论】:

请在评论下查看 请看图 您的要求是什么?我认为它按预期工作 嗨,兄弟;你能检查我的新问题吗,请***.com/questions/64655328/… 你确定我会检查

以上是关于为啥我不能从数组中得到正确的日期?的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Inputmask 不能正确处理日期?

为啥我不能从我的 DataFrame 中的“日期”列中提取月份的列? [复制]

为啥我不能在 Swift 的 reduce 中正确划分整数?

为啥我不能在 terra::rast 中添加日期作为标题?

为啥指针不能传递正确的值(C)?

为啥我不能将此信息从一个反应组件推送到另一个?