Y
Y
YuriyBum3342020-01-30 20:58:46
Java
YuriyBum334, 2020-01-30 20:58:46

Why does working Java code in Android work incorrectly?

The method describes a loop showing how many events are scheduled for the day. Everything seems to be simple: the code is restarted for a new day, the loop checks for each event in a month that the date matches. If it matches, reloads them into an array for today and returns the length of the new array. But in practice, all events for all days fall on the first day, and the rest are empty. Why is that?

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
{
    Date monthDate = dates.get(position);
    Calendar dateCalendar = Calendar.getInstance();
    dateCalendar.setTime(monthDate);
    int DayNo = dateCalendar.get(Calendar.DAY_OF_MONTH);
    int displayMonth = dateCalendar.get(Calendar.MONTH) + 1;
    int displayYear = dateCalendar.get(Calendar.YEAR);
    int currentMonth = currentDate.get(Calendar.MONTH) + 1;
    int currentYear = currentDate.get(Calendar.YEAR);

    View view = convertView;
    if (view == null)
    {
        view = inflater.inflate(R.layout.single_cell_layout, parent, false);
    }

    if (displayMonth == currentMonth && displayYear == currentYear)
    {
        view.setBackgroundColor(getContext().getResources().getColor(R.color.green));
    }
    else
    {
        view.setBackgroundColor(Color.parseColor("#cccccc"));
    }

    TextView Day_Number = view.findViewById(R.id.calendar_day);
    TextView EventNumber = view.findViewById(R.id.events_id);
    Day_Number.setText(String.valueOf(DayNo));
    Calendar eventCalendar = Calendar.getInstance();
    ArrayList<String> arrayList = new ArrayList<>();

    for (int i = 0; i < events.size(); i++)
    {
        eventCalendar.setTime(ConvertStringToDate(events.get(i).getDATE()));
        if (DayNo == eventCalendar.get(Calendar.DAY_OF_MONTH) && displayMonth == eventCalendar.get(Calendar.MONTH) + 1
                && displayYear == eventCalendar.get(Calendar.YEAR))
        {
            arrayList.add(events.get(i).getEVENT());
            EventNumber.setText(arrayList.size() + " Events");
        }
    }

    return view;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-01-31
@zagayevskiy

. If it matches, reloads them into an array for today and returns the length of the new array

This is what is not in the code. Add it, it will work as it should.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question