I have a custom list view adapter that uses a countdown timer to populate one field of the listview.
My problem is that the method that is generating the countdown timer needs to access a textview called txtTiempo, viewItem.txtTiempo in this case, but there is an error saying that the variable viewItem is accessed from within inner class, and needs to be declared final, but if I declare it as final, then the there are more errors, then you cannot assign a value to a final variable. How can I solve it?
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItemSubastasHome viewItem = null;
if(convertView == null)
{
viewItem = new ViewItemSubastasHome();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInfiater.inflate(R.layout.layout_items_subastashome, null);
viewItem.txtTiempo = (TextView)convertView.findViewById(R.id.txtTiempo);
viewItem.txtReferencia = (TextView)convertView.findViewById(R.id.txtReferencia);
viewItem.txtReferencia4 = (TextView)convertView.findViewById(R.id.txtReferencia4);
viewItem.txtNumeroPujas = (TextView)convertView.findViewById(R.id.txtNumeroPujas);
viewItem.txtTitulo = (TextView)convertView.findViewById(R.id.txtTitulo);
viewItem.txtDescripcion = (TextView)convertView.findViewById(R.id.txtDescripcion);
viewItem.txtMejorprecio = (TextView)convertView.findViewById(R.id.txtMejorprecio);
viewItem.foto = (ImageView)convertView.findViewById(R.id.foto);
convertView.setTag(viewItem);
}
else
{
viewItem = (ViewItemSubastasHome) convertView.getTag();
}
String foto = valueList.get(position).foto;
String tiempo = valueList.get(position).termina;
String referencia = valueList.get(position).referencia;
String titulo = valueList.get(position).titulo;
String descripcion = valueList.get(position).descripcion;
String termina = valueList.get(position).termina;
String total_pujas = valueList.get(position).total_pujas;
String mejor_precio = valueList.get(position).mejor_precio;
viewItem.txtReferencia.setText(referencia);
viewItem.txtTitulo.setText(titulo);
viewItem.txtDescripcion.setText(descripcion);
if (total_pujas.equals("0")){
viewItem.txtNumeroPujas.setText("Sin pujas");
//viewItem.txtReferencia4.setVisibility(GONE);
}
else {
viewItem.txtNumeroPujas.setText(total_pujas+" pujas");
}
if(mejor_precio.equals("null")){
viewItem.txtMejorprecio.setText("0€");
}
else {
viewItem.txtMejorprecio.setText(mejor_precio+"€");
}
String imgUrl = "https://..."+valueList.get(position).foto;
//COUNTDOWN here
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
formatter.setLenient(false);
String endTime = "20.07.2018, 15:05:36";
Date endDate;
try {
endDate = formatter.parse(endTime);
milliseconds = endDate.getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startTime = System.currentTimeMillis();
diff = milliseconds - startTime;
mCountDownTimer = new CountDownTimer(milliseconds, 5000) {
@Override
public void onTick(long millisUntilFinished) {
startTime=startTime-1;
Long serverUptimeSeconds =
(millisUntilFinished - startTime) / 5000;
String daysLeft = String.format("%d", serverUptimeSeconds / 86400);
// holder.txtempresa.setText(daysLeft);
String hoursLeft = String.format("%d", (serverUptimeSeconds % 86400) / 3600);
// holder.txtempresa.setText(hoursLeft);
String minutesLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) / 60);
String secondsLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) % 60);
viewItem.txtTiempo.setText(daysLeft+"d"+hoursLeft+"h"+minutesLeft+"m");<---ERROR HERE
}
@Override
public void onFinish() {
}
}.start();
Glide.with(context).load(imgUrl).into(viewItem.foto);
return convertView;
}