重复列表正在 android 添加到 gridview

duplicate list is adding to gridview in android

你好,在下面我正在显示 calendarview.In calendarview 我正在选择日期并将该日期传递给 GetAppointmentDate(selecteddate) 如果 selecteddate 和我的日期匹配 我正在显示 gridview layout.but 它是给多个列表如下 .

任何人都可以帮助您重复列表显示每时每刻。

CalendarViewActivity.java:

public class CalendarViewActivity extends Activity implements DatetimeAdapter.MyItemClickListener{

    private CalendarView mCalendarView;
    private ProgressDialog progressDialog = null;
    private ArrayList<GetData> getDataArrayList;
    private ArrayList<GetSlots> getSlotsArrayList;
    private GridView gridLayout;
    private  GetData getData2;
    private GetSlots getSlots2;
    private String Id,StartTime,SlotBooked,SlotDate,EndTime,SloteTime;
    private DatetimeAdapter datetimeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.calendar_view);
        gridLayout=findViewById(R.id.gridlayout);
        getDataArrayList=new ArrayList<>();
        getSlotsArrayList=new ArrayList<>();

        mCalendarView = (CalendarView) findViewById(R.id.calendarView);
        mCalendarView.setMinDate(System.currentTimeMillis() - 1000);
        datetimeAdapter = new DatetimeAdapter(getApplicationContext(), getSlotsArrayList,this);



        mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            Calendar calendar = Calendar.getInstance();
            int year1 = calendar.get(Calendar.YEAR);
            int month1 = calendar.get(Calendar.MONTH);
            int dayOfMonth1 = calendar.get(Calendar.DAY_OF_MONTH);

            @Override
            public void onSelectedDayChange(CalendarView CalendarView, int year1, int month1, int dayOfMonth1) {

                String date = new SimpleDateFormat("MM", Locale.getDefault()).format(new Date());
                SimpleDateFormat sdf = new SimpleDateFormat(date);

                String selecteddate = (sdf.format(month1 + 1)) + "/" + dayOfMonth1 + "/" + year1;

                GetAppointmentDate(selecteddate);
                Toast.makeText(getApplicationContext(),selecteddate,Toast.LENGTH_LONG).show();
                }
        });


    }
    private void GetAppointmentDate(String outputcurrentdate) {
//        progressDialog = new ProgressDialog(getApplicationContext());
//        progressDialog.setIndeterminate(true);
//        progressDialog.setMessage("Loading...");
//        progressDialog.setCanceledOnTouchOutside(false);
//        progressDialog.setCancelable(false);
//        progressDialog.show();
        String doctor_ids="13";
        String HospitalId="PH:193";
        final APIService service = RetroClass.getRetrofitInstance().create(APIService.class);
        Call<GetAppointmentDateModel> call = service.GetAppointmentDate(doctor_ids,HospitalId);
        Log.wtf("URL Called", call.request().url() + "");
        call.enqueue(new Callback<GetAppointmentDateModel>() {
            @Override
            public void onResponse(Call<GetAppointmentDateModel> call, Response<GetAppointmentDateModel> response) {
                Log.e("response", new Gson().toJson(response.body()));
                if (response.isSuccessful()) {
                    Log.e("response", new Gson().toJson(response.body()));
                    GetAppointmentDateModel getAppointmentDateModel = response.body();
                    ArrayList<GetData> getData = getAppointmentDateModel.getGetAppointmentList();
                    for (GetData getData1 : getData) {
                        String date1 = getData1.getDate();
                        String id=getData1.getId();
                        DateFormat inputFormatter = new SimpleDateFormat("dd MMM yyyy HH:mm-HH:mm");
                        Date date = null;
                        try {
                            date = inputFormatter.parse(date1);
                            DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
                            String output = outputFormatter.format(date);
                            getData2=new GetData(id,output);
                            getDataArrayList.add(getData2);
                            Log.d("dateString",output);
                            ArrayList<GetSlots> getSlots = getData1.getData();
                            for (GetSlots getSlots1 : getSlots) {
                                Id=getSlots1.getId();
                                SlotDate=getSlots1.getDate();
                                StartTime=getSlots1.getStartTime();
                               EndTime=getSlots1.getEndTime();
                                SloteTime=getSlots1.getSlotTime();
                                SlotBooked=getSlots1.getSlotBooked();

                                getSlots2=new GetSlots(Id,SlotDate,StartTime,EndTime,SlotBooked,SloteTime);
                                getSlotsArrayList.add(getSlots2);

                            }

                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                    }
                    String current_date=getData2.getDate();
                    if(current_date.equals(outputcurrentdate)) {
                        gridLayout.setAdapter(datetimeAdapter);
                        datetimeAdapter.notifyDataSetChanged();
                    }
                }

               // progressDialog.dismiss();
            }
            @Override
            public void onFailure(Call<GetAppointmentDateModel> call, Throwable t) {
                Log.d("error", t.getMessage());
              //  progressDialog.dismiss();
            }
        });



    }

    @Override
    public void myItemClick(int position) {

    }
}

您的列表 - getSlotsArrayList 每次都在添加新值,并且不会在任何地方清除。这是传递给适配器的同一个列表。所以它显示多个值。

在此之前调用清除方法

getSlotsArrayList.clear();
GetAppointmentDate(selecteddate);