2016-08-03 7 views
0

私のアプリケーションでカスタムのListViewを使用しています。問題は、オブジェクト "Pedido"が属性 "timestampAtendimento"をnull属性として持つことができ、この場合、アイテムレイアウト上のTextViewとアイテムの背景色を変更しないことです。ただし、属性がnullの場合でも、クラス "ListaPedidosRowAdapter"は属性の値を変更し、nullではありません(値はリスト上の別のオブジェクトの値で設定されています)。Android:ArrayAdapterがオブジェクト属性を変更しています

私はプログラムを何度もデバッグし、オブジェクトリストが正しいことを知りました。問題は "ListaPedidosRowAdapter"クラス内で発生します。しかし、なぜそれが起こっているのか分かりません。

誰かが私を助けることができますか?私の "ListaPedidosRowAdapter" クラスです

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); 
} 

あなたはビューが再利用され、その行を覚えている:ここでは

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; 
    } 
} 

答えて

1

はトリックです。ビューをデータにバインドするすべてのif条件では、このような条件が適用されない場合、ビューをクリーニングするにはelseが必要です。

+0

うわー!私はとても多くの時間を費やしており、その解決はとてもシンプルです!非常に私の友人、ありがとう、それは多くの助けた。 –

関連する問題