Android:ArrayAdapter正在更改对象属性

时间:2016-08-03 20:12:23

标签: android listview android-arrayadapter

我在我的应用中使用自定义ListView。问题是,对象" Pedido"可以具有属性" timestampAtendimento"作为null属性,在这种情况下,我的项目布局上的TextView和项目背景颜色不应更改。但是,即使属性为null,类" ListaPedidosRowAdapter"更改属性的值,使其不为null(使用列表中另一个对象的值设置该值)。

我多次调试程序并发现对象列表是正确的,问题发生在" ListaPedidosRowAdapter"类。但我不知道它为什么会发生。

有人可以帮助我吗?

这是我的" ListaPedidosRowAdapter"类:

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 个答案:

答案 0 :(得分:1)

这就是诀窍:

In [10]: df.head(10)
Out[10]: 
                            0         1         2         3
2011-01-01 00:00:00  0.182481  0.523784  0.718124  0.063792
2011-01-01 01:00:00  0.321362  0.404686  0.481889  0.524521
2011-01-01 02:00:00  0.514426  0.735809  0.433758  0.392824
2011-01-01 03:00:00  0.616802  0.149099  0.217199  0.155990
2011-01-01 04:00:00  0.525465  0.439633  0.641974  0.270364
2011-01-01 05:00:00  0.749662  0.151958  0.200913  0.219916
2011-01-01 06:00:00  0.665164  0.396595  0.980862  0.560119
2011-01-01 07:00:00  0.797803  0.377273  0.273724  0.220965
2011-01-01 08:00:00  0.651989  0.553929  0.769008  0.545288
2011-01-01 09:00:00  0.692169  0.261194  0.400704  0.118335

In [11]: df.tail()
Out[11]: 
                            0         1         2         3
2011-01-03 19:00:00  0.247211  0.539330  0.734206  0.781125
2011-01-03 20:00:00  0.278550  0.534943  0.804949  0.137291
2011-01-03 21:00:00  0.602246  0.108791  0.987120  0.455887
2011-01-03 22:00:00  0.003097  0.436435  0.987877  0.046066
2011-01-03 23:00:00  0.604916  0.670532  0.513927  0.610775


In [12]: df.mean()
Out[12]: 
0    0.495307
1    0.477509
2    0.562590
3    0.447997
dtype: float64

In [13]: new_df = pd.DataFrame(df.mean().to_dict(),index=[df.index.values[-1]])

In [14]: new_df
Out[14]: 
                            0         1        2         3
2011-01-03 23:00:00  0.495307  0.477509  0.56259  0.447997

In [15]: new_df.rename(columns=lambda c: "mean_"+str(c))
Out[15]: 
                       mean_0    mean_1   mean_2    mean_3
2011-01-03 23:00:00  0.495307  0.477509  0.56259  0.447997

您必须记住重复使用行视图。对于将您的观点与数据绑定的每个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个来清理它们。