我有一个自定义列表视图适配器,该适配器使用倒数计时器填充列表视图的一个字段。
我的问题是,在这种情况下,生成倒数计时器的方法需要访问一个名为txtTiempo,viewItem.txtTiempo的文本视图,但是有一个错误消息指出从内部类内部访问变量viewItem,并且需要被声明为final,但是如果我将其声明为final,则存在更多错误,则您无法将值分配给final变量。我该怎么解决?
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;
}
答案 0 :(得分:2)
Java仅支持将最终变量作为内部类,您可以阅读有关here的更多信息。
现在要回答您的问题,最简单的方法是使其成为成员变量。
private ViewItemSubastasHome viewItem = null;
public View getView(int position, View convertView, ViewGroup parent)
{
// code....
}
这应该可以解决问题。