notifyDataSetChanged()没有更新我的ListView

时间:2017-01-19 21:29:18

标签: android listview baseadapter notifydatasetchanged

我正在尝试将项目添加到ListView,但我找到的所有解决方案都没有帮助。

public class ListaCustomizadaCarrinho extends BaseAdapter{

    private static ListaCustomizadaCarrinho singleton;
    private LayoutInflater inflater;
    private Context context;
    private ArrayList<Pedido> pedidos;
    private Carrinho carrinho;

    public ListaCustomizadaCarrinho(Context context, ArrayList<Pedido> pedidos, Carrinho carrinho) {
        super();
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.pedidos = pedidos;
        this.carrinho = carrinho;
        if(singleton == null)
            singleton = this;
    }

    static class ViewHolder {
        ImageView imagemH;
        TextView textoDescricao;
        TextView numItens;
        TextView valorItem;
    }

    @Override
    public int getCount() {
        return pedidos.size();
    }

    @Override
    public Object getItem(int position) {
        return pedidos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.activity_lista_carrinho, parent, false);
            holder.imagemH = (ImageView) convertView.findViewById(R.id.icone);
            holder.textoDescricao = (TextView) convertView.findViewById(R.id.descricao_item);
            holder.numItens = (TextView) convertView.findViewById(R.id.quantidade_itens);
            holder.valorItem = (TextView) convertView.findViewById(R.id.valor_item);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.imagemH.setImageResource(pedidos.get(position).getItem().getImageItemSrc());
        holder.textoDescricao.setText(pedidos.get(position).getItem().getName());
        holder.numItens.setText(""+pedidos.get(position).getAmmount());
        holder.valorItem.setText(""+pedidos.get(position).getItem().getPrice());

        final Button botaoRemover = (Button) convertView.findViewById(R.id.btnRemove);
        botaoRemover.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // PRESSED
                        Toast.makeText(context,""+position, Toast.LENGTH_LONG).show();
                        botaoRemover.setBackgroundColor(Color.BLACK);
                        return true; // if you want to handle the touch event
                    case MotionEvent.ACTION_UP:
                        // RELEASED
                        botaoRemover.setBackgroundColor(Color.RED);
                        carrinho.removerPedido(position);
                        pedidos.remove(position);
                        notifyDataSetChanged();
                        return true; // if you want to handle the touch event
                }
                return false;
            }
        });

        return convertView;
    }

    public static ListaCustomizadaCarrinho getSingleton(){
        return singleton;
    }

    public void refresh(ArrayList<Pedido> pedidos){
        this.pedidos.clear();
        this.pedidos.addAll(pedidos);
        notifyDataSetChanged();
        Toast.makeText(context,"Working?",Toast.LENGTH_LONG).show();
    }
}

我尝试刷新对象类中的列表; Carrinho。在方法addPedido(Pedido pedido)

public class Carrinho implements Parcelable{

    private Client client;
    private int idCarrinho;
    private User user;
    private QrCode qrCode;
    private ArrayList<Pedido> listaDePedidos;
    private double discount =0;
    private double subprice =0;
    private double totalPrice =0 ;
    private StatePayment state = StatePayment.WaitingPayment;
    TextView textTotal;
    View convertView;

    public Carrinho(Client client, int idCarrinho, User user,ArrayList<Pedido> listaDePedidos){
        this.client = client;
        this.idCarrinho = idCarrinho;
        this.user = user;
        this.listaDePedidos = listaDePedidos;
    }

    public Carrinho() {
    }

    protected Carrinho(Parcel in) {
        idCarrinho = in.readInt();
        qrCode = (QrCode) in.readValue(QrCode.class.getClassLoader());
        listaDePedidos = new ArrayList<Pedido>();
        in.readList(listaDePedidos,Pedido.class.getClassLoader());
        discount = in.readDouble();
        subprice = in.readDouble();
        totalPrice = in.readDouble();
    }

    public void populateList(Context context, ViewGroup parent){
        View convertView;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.activity_tela_user,parent,true);
        this.convertView = convertView;
        textTotal = (TextView) convertView.findViewById(R.id.textValorTotal);
        double price;
        totalPrice = 0;

        for (Pedido e: listaDePedidos){
            price = e.getItem().getPrice();
            totalPrice += price;
        }

        String totalS = String.format("%.2f",totalPrice);
        totalS = totalS.replace(".",",");
        textTotal.setText(totalS);

        ListView listView = (ListView) convertView.findViewById(R.id.listaCarrinho);
        ListaCustomizadaCarrinho listaCarrinho = new ListaCustomizadaCarrinho(context, listaDePedidos, this);
        listView.setAdapter(listaCarrinho);
}

    public ArrayList<Pedido> getListaDePedidos() {
        return listaDePedidos;
    }

    public void setListaDePedidos(ArrayList<Pedido> listaDePedidos) { this.listaDePedidos = listaDePedidos; }

    public void addPedido(Pedido pedido){
        //textTotal = (TextView) convertView.findViewById(R.id.textValorTotal);
        totalPrice += pedido.getItem().getPrice();
        String totalS = String.format("%.2f",totalPrice);
        totalS = totalS.replace(".",",");
        //textTotal.setText(totalS);
        listaDePedidos.add(pedido);
        ListaCustomizadaCarrinho.getSingleton().refresh(listaDePedidos);
    }

    public void removerPedido(int index){
        totalPrice -= listaDePedidos.get(index).getItem().getPrice();
        String totalS = String.format("%.2f",totalPrice);
        totalS = totalS.replace(".",",");
        textTotal.setText(totalS);
        listaDePedidos.remove(index);
    }
}

我知道这不是调用该方法的最佳位置,但我现在只是尝试测试我的代码。任何帮助将不胜感激!

0 个答案:

没有答案