Android: ArrayAdapter 正在更改对象属性
Android: ArrayAdapter is changing the object attribute
我在我的应用程序中使用自定义 ListView。问题是,对象 "Pedido" 可以将属性 "timestampAtendimento" 作为空属性,在这种情况下,我的项目布局上的 TextView 和项目背景颜色不应更改。但是,即使属性为空,class "ListaPedidosRowAdapter" 也会更改属性的值,使其不为空(该值被设置为列表中另一个对象的值)。
我调试了几次程序,发现对象列表是正确的,问题出在"ListaPedidosRowAdapter"class里面。但是我不知道为什么会这样。
有人可以帮我吗?
那是我的 "ListaPedidosRowAdapter" class:
public class ListaPedidosRowAdapter extends ArrayAdapter<Pedido> {
private List<Pedido> pedidos;
private Context context;
public ListaPedidosRowAdapter(List<Pedido> pedidos, Context context) {
super(context, item_lista_pedidos);
this.pedidos = pedidos;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(item_lista_pedidos, null, true);
holder = new ViewHolder();
holder.itemNomePratoTextView = (TextView) convertView.findViewById(R.id.itemNomePratoTextView);
holder.itemQtdPedidoTextView = (TextView) convertView.findViewById(R.id.itemQtdPedidosTextView);
holder.itemGarcomNomeTextView = (TextView) convertView.findViewById(R.id.itemGarcomNomeTextView);
holder.itemTimestampPedidoTextView = (TextView) convertView.findViewById(R.id.itemTimestampPedidoTextView);
holder.itemTimestampAtendimentoTextView = (TextView) convertView.findViewById(R.id.itemTimestampAtendimentoTextView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Pedido pedido = pedidos.get(position);
holder.itemNomePratoTextView.setText(pedido.nomeServico);
holder.itemGarcomNomeTextView.setText(pedido.usuario);
holder.itemTimestampPedidoTextView.setText(ValidateDate.getDateTimeForView(pedido.timestampPedido));
holder.itemQtdPedidoTextView.setText(Integer.toString(pedido.quantidade));
// if the attribute is null do not change the text view
if(pedido.timestampAtendimento != null) {
holder.itemTimestampAtendimentoTextView.setText( ValidateDate.getDateTimeForView( pedido.timestampAtendimento ) );
convertView.setBackgroundResource(R.drawable.item_list_disabled);
}
return convertView;
}
@Override
public int getCount(){ return pedidos.size(); }
static class ViewHolder{
public TextView itemNomePratoTextView;
public TextView itemQtdPedidoTextView;
public TextView itemGarcomNomeTextView;
public TextView itemTimestampPedidoTextView;
public TextView itemTimestampAtendimentoTextView;
}
}
技巧如下:
if(pedido.timestampAtendimento != null) {
holder.itemTimestampAtendimentoTextView.setText( ValidateDate.getDateTimeForView( pedido.timestampAtendimento ) );
convertView.setBackgroundResource(R.drawable.item_list_disabled);
} else {
holder.itemTimestampAtendimentoTextView.setText("");
convertView.setBackgroundDrawable(null);
}
您必须记住行视图是重复使用的。对于将您的视图绑定到数据的每个 if
条件,如果此类条件不适用,您将需要一个 else
来清理它们。
我在我的应用程序中使用自定义 ListView。问题是,对象 "Pedido" 可以将属性 "timestampAtendimento" 作为空属性,在这种情况下,我的项目布局上的 TextView 和项目背景颜色不应更改。但是,即使属性为空,class "ListaPedidosRowAdapter" 也会更改属性的值,使其不为空(该值被设置为列表中另一个对象的值)。
我调试了几次程序,发现对象列表是正确的,问题出在"ListaPedidosRowAdapter"class里面。但是我不知道为什么会这样。
有人可以帮我吗?
那是我的 "ListaPedidosRowAdapter" class:
public class ListaPedidosRowAdapter extends ArrayAdapter<Pedido> {
private List<Pedido> pedidos;
private Context context;
public ListaPedidosRowAdapter(List<Pedido> pedidos, Context context) {
super(context, item_lista_pedidos);
this.pedidos = pedidos;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(item_lista_pedidos, null, true);
holder = new ViewHolder();
holder.itemNomePratoTextView = (TextView) convertView.findViewById(R.id.itemNomePratoTextView);
holder.itemQtdPedidoTextView = (TextView) convertView.findViewById(R.id.itemQtdPedidosTextView);
holder.itemGarcomNomeTextView = (TextView) convertView.findViewById(R.id.itemGarcomNomeTextView);
holder.itemTimestampPedidoTextView = (TextView) convertView.findViewById(R.id.itemTimestampPedidoTextView);
holder.itemTimestampAtendimentoTextView = (TextView) convertView.findViewById(R.id.itemTimestampAtendimentoTextView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Pedido pedido = pedidos.get(position);
holder.itemNomePratoTextView.setText(pedido.nomeServico);
holder.itemGarcomNomeTextView.setText(pedido.usuario);
holder.itemTimestampPedidoTextView.setText(ValidateDate.getDateTimeForView(pedido.timestampPedido));
holder.itemQtdPedidoTextView.setText(Integer.toString(pedido.quantidade));
// if the attribute is null do not change the text view
if(pedido.timestampAtendimento != null) {
holder.itemTimestampAtendimentoTextView.setText( ValidateDate.getDateTimeForView( pedido.timestampAtendimento ) );
convertView.setBackgroundResource(R.drawable.item_list_disabled);
}
return convertView;
}
@Override
public int getCount(){ return pedidos.size(); }
static class ViewHolder{
public TextView itemNomePratoTextView;
public TextView itemQtdPedidoTextView;
public TextView itemGarcomNomeTextView;
public TextView itemTimestampPedidoTextView;
public TextView itemTimestampAtendimentoTextView;
}
}
技巧如下:
if(pedido.timestampAtendimento != null) {
holder.itemTimestampAtendimentoTextView.setText( ValidateDate.getDateTimeForView( pedido.timestampAtendimento ) );
convertView.setBackgroundResource(R.drawable.item_list_disabled);
} else {
holder.itemTimestampAtendimentoTextView.setText("");
convertView.setBackgroundDrawable(null);
}
您必须记住行视图是重复使用的。对于将您的视图绑定到数据的每个 if
条件,如果此类条件不适用,您将需要一个 else
来清理它们。