gpt4 book ai didi

java - 我在 listView 中有一个倒计时器,但工作不正常

转载 作者:行者123 更新时间:2023-12-02 06:04:49 25 4
gpt4 key购买 nike

我有一个 listView,其中每个项目都应显示倒计时,事件日期来自 Firebase。我的倒计时器可以正常工作,但前提是列表中只有一项。

在我的 fragment 中,我有一个 listview

在我的 adapter_events 中,我有一个文本字段,我希望在其中为每个项目显示计数器 (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;
}

最佳答案

每次需要显示新 View 时,您都会创建一个倒计时器。请注意, ListView 只是回收 View ,例如。如果屏幕上只有 10 个 View ,即使您有 100 个项目,也只会创建 10 个 View ,并且您的项目数据将仅绑定(bind)到该 View 。

另请注意,您在 CountdownTimer 内紧握 TempoRestante,这会导致泄漏。

附注请正确命名您的变量并使用驼峰式大小写。

关于java - 我在 listView 中有一个倒计时器,但工作不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55948752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com