0

I have a listView where each item should display a countdown, the event dates come from Firebase. My countdown timer is working, but only if the list has only one item.

In my fragment I have a listview.

In my adapter_events I have a text field where I want the counter to appear for each item (textViewTime)

EventsAdapter.java:

public class EventsAdapter extends BaseAdapter {

    private Context mContext;
    private TextView NomeDoEvento;
    private TextView TempoRestante;
    private TextView DataDoEvento;
    //private TextView DataLocal;

    public EventsAdapter(Context context) {
        this.mContext = context;
    }

    public int getCount() {
        return (SharedResources.getInstance().getEventos().size());
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.adapter_events, null);

        NomeDoEvento = v.findViewById(R.id.textViewNomeDoEvento);
        TempoRestante = v.findViewById(R.id.textViewTempo);
        DataDoEvento = v.findViewById(R.id.textViewDataLocal);

        // Hora Atual
        long currentTimeMillis = Calendar.getInstance().getTimeInMillis();

        //Dados do evento
        Evento evento = SharedResources.getInstance().getEventos().get(position);
        NomeDoEvento.setText(evento.getNomeDoEvento());
        //Data do evento
        String[] data = evento.getDataWorld().split("/");
        GregorianCalendar HorarioEvento = new GregorianCalendar(Integer.parseInt(data[0]) ,Integer.parseInt(data[1])-1,
                Integer.parseInt(data[2]),Integer.parseInt(data[3]),Integer.parseInt(data[4]),0);
        long expiryTime = HorarioEvento.getTimeInMillis() - currentTimeMillis;

        DataDoEvento.setText(HorarioEvento.getTime().toString());

        TempoRestante.setText("");
        new CountDownTimer(expiryTime, 1000) {
            public void onTick(long millisUntilFinished) {
                long seconds = millisUntilFinished / 1000; // reminder: 1 sec = 1000 millis
                long minutes = seconds / 60;
                long hours = minutes / 60;
                long days = hours / 24;

                String dayFormat = "d ";
                String hoursFormat = "h ";
                String minutesFormat = "m ";
                String secondFormat = "s";
                String time = days + dayFormat + hours % 24 + hoursFormat + minutes%60 + minutesFormat + seconds % 60 +secondFormat;
                TempoRestante.setText(time);
            }

            // Finished: counted down to 0
            public void onFinish() {
                TempoRestante.setText("Evento iniciado!");
            }
        }.start();

        return v;
    }
}

Fragment.java:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_events, container, false);

        ListEventos = v.findViewById(R.id.ListView_Eventos);
        Progress = v.findViewById(R.id.progressBar);
        Progress.setVisibility(View.INVISIBLE);

        Progress.setVisibility(View.VISIBLE);
        DatabaseCommunication.RecuperaEventos(v.getContext());
        return v;
    }
3iL
  • 2,146
  • 2
  • 23
  • 47

1 Answers1

0

You are creating a Countdown Timer everytime you need a new View to Display. Note that, list view simply recycle the views so for example. If only 10 view will fit in the screen, even if you have a hundred items, only 10 views will be created and your item data will just be binded to that view.

Also note that you are holding on to you TempoRestante inside the CountdownTimer that will cause a leak.

P.S. Please name your variables properly and use camel case.

Archie G. Quiñones
  • 11,638
  • 18
  • 65
  • 107