对话框仅显示数组列表中的最后一项

时间:2018-04-30 08:21:39

标签: java android arraylist

我正在尝试显示数组列表中的数据但是在标记点击之后 它只显示材料对话框中数组列表中的最后一个元素  DriverLocationDataManager获取数据库中驱动程序的地理位置的所有数据快照

添加所有驱动程序数据后,我使用addMarker函数获取地理位置并在地图上设置标记。

//Init data manager

    drivers = new ArrayList<>(0);



    dataManager = new DriverLocationDataManager(this) {
        @Override
        public void onDataLoaded(List<Driver> data) {
            if (data.isEmpty()) {
                Snackbar.make(container, "Sorry!. UG Shuttle service is currently unavailable",
                        Snackbar.LENGTH_INDEFINITE).show();
            } else {

                drivers.addAll(data);


                List<Marker> markers = addMarkers(data);
                for (int i = 0; i < markers.size(); i++){
                    markers.get(i);
                    Driver driver = drivers.get(i);
                    map.setOnMarkerClickListener(marker -> {
                        // Get custom view
                        View v = getLayoutInflater().inflate(R.layout.driver_popup, null, false);

                        //Assign props
                        TextView username = v.findViewById(R.id.driver_username);
                        CircularImageView profile = v.findViewById(R.id.driver_profile);
                        ImageView status = v.findViewById(R.id.driver_status);
                        TextView shuttle = v.findViewById(R.id.driver_bus_number);
                        ViewGroup viewGroup = v.findViewById(R.id.group);

                        //Init props
                        Glide.with(getApplicationContext())
                                .load(driver.getProfile())
                                .apply(RequestOptions.circleCropTransform())
                                .apply(RequestOptions.placeholderOf(R.drawable.avatar_placeholder))
                                .apply(RequestOptions.errorOf(R.drawable.avatar_placeholder))
                                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                                .transition(withCrossFade())
                                .into(profile);

                        username.setText(driver.getDriver());   //Driver's username
                        shuttle.setText(driver.getCarNumber()); //Driver's car number

                        //Attach to dialog
                        Builder materialDialog = new Builder(HomeActivity.this)
                                .customView(v, true)
                                .negativeText("Dismiss")
                                .onPositive((dialog, which) -> {
                                    dialog.dismiss();
                                    enableTracking(marker);
                                })
                                .onNegative((dialog, which) -> dialog.dismiss());

                        if (driver.isStatus()) {
                            status.setImageResource(android.R.color.holo_green_light);  //Online

                            //Enable tracking when driver is online
                            materialDialog.positiveText("Track")
                                    .onPositive((dialog, which) -> {
                                        dialog.dismiss();
                                        enableTracking(marker);
                                    });
                        } else {
                            //Tracking is disabled
                            status.setImageResource(android.R.color.holo_red_light);    //Offline
                        }

                        materialDialog.build().show();
                        return true;
                    });



                }
            }
        }
    };

1 个答案:

答案 0 :(得分:0)

我邀请您阅读官方开发者指南here。它解释了如何正确使用Dialogs,如何在其中显示列表,甚至如何在需要显示列表时实现自定义视图。