Repeated items with different amounts in a Listview

Asked

Viewed 217 times

0

I have a Listview (lsvProduct) with some items that comes from a request and an Edittext of amount that the user can change (by default, I bring the value of 0). By adding this item, I send it to another Listview (lsvCarrinho) with a very similar structure and with an Edittext that tbm can be changed, like the images below ():

Produto Carrinho

Problem: If I add an item in lsvCarrinho (2x or more) but with different quantities, instead of adding up the amount of items added in a single row, I would like it to leave in separate rows. My Listview already does that. In my case, if the same item is added more than 1x, it repeats the last quantity placed (the item is repeated but with the amount value equal to the last item that was added and it had been added with a different amount value). What do I do to differentiate the quantity even if the item is being repeated in the listview? It does not repeat the amount if I add a different item between them.

Ex.: I added a Clock - with quantity equal to 2, then added the same Clock with quantity equal to 3. In my Listview there are 2 lines with quantity equal to 3. Then add the same Clock with quantity equal to 5, In my Listview there are 3 Clock lines with 5 in quantity (Clock = 5 / Clock = 5 / Clock = 5 but it should stay Clock = 2 / Clock = 3 / Clock = 5). If I put a Cap and then a Clock again looks like this (Clock = 5 / Clock = 5 / Clock = 5 / Cap = 1 / Clock = 1)

Code of the Trolley’s Adapter:

public class LancarVendaCarrinhoListViewAdapter extends BaseAdapter {

private Context mContext;
private List<PesquisarProdutoObjetoRetorno> mDataSource;

public LancarVendaCarrinhoListViewAdapter(Context context, List<PesquisarProdutoObjetoRetorno> items)
{
    mContext = context;
    mDataSource = items;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    try
    {
        LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View sView = mInflater.inflate(R.layout.activity_lancarvenda_carrinho_list_view_item, parent, false);

        PesquisarProdutoObjetoRetorno sItem = (PesquisarProdutoObjetoRetorno) getItem(position);

        TextView descricao = (TextView)sView.findViewById(R.id.lancarvenda_carrinho_item_txtdescricao);
        descricao.setText(sItem.getDescricao());

        TextView preco = (TextView)sView.findViewById(R.id.lancarvenda_carrinho_item_txvpreco);
        preco.setText(Texto.FormatarValor(sItem.getPreco()));

        NumberPicker quantidade = (NumberPicker)sView.findViewById(R.id.lancarvenda_carrinho_item_etquantidade);
        quantidade.setValue((int)sItem.getQuantidade());

        return sView;
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return null;
    }
}

public List<PesquisarProdutoObjetoRetorno> getItemList()
{
    return mDataSource;
}

public void setItemList(List<PesquisarProdutoObjetoRetorno> itemList)
{
    this.mDataSource = itemList;
}

}

Code that adds items to the list:

Button adicionar = (Button)sView.findViewById(R.id.lancarvenda_produto_item_btnadicionar);
        adicionar.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                sAdaptador = (LancarVendaCarrinhoListViewAdapter)lsvCarrinho.getAdapter();
                sCarrinhoAuxiliar = sAdaptador.getItemList();

                LancarVendaProdutosFragment.sProdutos.getDadosProduto().setQuantidade(Double.valueOf(quantidade.displayTextView.getText().toString()));

                sCarrinhoAuxiliar.add(LancarVendaProdutosFragment.sProdutos.getDadosProduto());

                LancarVendaProdutosTabBarActivity sActivity = ((LancarVendaProdutosTabBarActivity)mContext);

                LancarVendaCarrinhoFragment sFragment =
                        (LancarVendaCarrinhoFragment)sActivity.getSupportFragmentManager().getFragments().get(1);
                sFragment.CarregarDados();

                Toast toast = Toast.makeText(v.getContext(),"Produto adicionado ao carrinho", Toast.LENGTH_LONG);
                View toastView = toast.getView();
                toastView.setBackgroundResource(R.drawable.toast_message_style);
                toast.show();
                notifyDataSetChanged();
            }
        });
  • Wow. I stopped understanding there in the part that says: I have a Listview...

  • Boy, no kidding. It’s confusing.

  • I edited it, see if you can understand now... for me that I have the problem in hand did not get confused!

  • you say an error in mDataSource loading?

  • This problem is programming logic, as I understand it. Every time you insert an item in the cart’s arrayList, you have to check that you no longer have any product added with the same id, for example....

  • it has no different id, no data is different, except the quantity and sometimes the quantity tbm can be equal!

  • Exactly... When adding more items, you check if there is an item with equal ID... If the item you are entering has the same ID, then it means that you will have to add the amount of items that are entering to the amount of items that already exist, and not create a new "line" in the cart’s item arrayList.

  • You can use a Map to make it easy, where the key would be the product ID and the object would be the product itself. You can also use an Arraylist, but you will have to scan it in its entirety every time you add an item to the cart.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.