I have a Listview ( lsvProduct ) with some items that come from a request and an EditText amount that the user can change (by default, I bring the value of 0). When I add this item, I send it to another ListView ( Carriage ) with a similar structure and with an EditText that can be changed, just like the images below:
Problem:IfIaddaniteminthelsvCarriage(2xormore)butwithdifferentamounts,insteadofaddingthenumberofitemsaddedtoasingleline,IIwishhewouldleaveinseparatelines.MyListViewalreadydoesthis.Inmycase,ifthesameitemisaddedmorethan1x,itrepeatsthelastquantityplaced(itgetstheitemrepeatedbutwiththeamountvalueequaltothelastitemthatwasaddedbeingthatithadbeenaddedwithadifferentquantityvalue).WhatdoIdotodifferentiatethequantityeveniftheitemisbeingrepeatednextinthelistview?ItdoesnotrepeatthequantityifIaddadifferentitembetweenthem.
Ex:IaddedaClock-withquantityequalto2,thenIaddedthesameClockwithquantityequalto3.InmyListViewthereare2lineswithquantityequalto3.TheresameClockwiththeamountequalto5,inmyListViewthereare3Clocklineswith5intheamount(Clock=5/Clock=5/Clock=5butshouldbeClock=2/Clock=3/Clock=5).IfIputaCapandthenaClockagainitlookslikethis(Clock=5/Clock=5/Clock=5/Cap=1/Clock=1)
Cartadaptercode:
publicclassLancarVendaCarrinhoListViewAdapterextendsBaseAdapter{privateContextmContext;privateList<PesquisarProdutoObjetoRetorno>mDataSource;publicLancarVendaCarrinhoListViewAdapter(Contextcontext,List<PesquisarProdutoObjetoRetorno>items){mContext=context;mDataSource=items;}@OverridepublicintgetCount(){returnmDataSource.size();}@OverridepublicObjectgetItem(intposition){returnmDataSource.get(position);}@OverridepubliclonggetItemId(intposition){returnposition;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){try{LayoutInflatermInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);ViewsView=mInflater.inflate(R.layout.activity_lancarvenda_carrinho_list_view_item,parent,false);PesquisarProdutoObjetoRetornosItem=(PesquisarProdutoObjetoRetorno)getItem(position);TextViewdescricao=(TextView)sView.findViewById(R.id.lancarvenda_carrinho_item_txtdescricao);descricao.setText(sItem.getDescricao());TextViewpreco=(TextView)sView.findViewById(R.id.lancarvenda_carrinho_item_txvpreco);preco.setText(Texto.FormatarValor(sItem.getPreco()));NumberPickerquantidade=(NumberPicker)sView.findViewById(R.id.lancarvenda_carrinho_item_etquantidade);quantidade.setValue((int)sItem.getQuantidade());returnsView;}catch(Exceptione){e.printStackTrace();returnnull;}}publicList<PesquisarProdutoObjetoRetorno>getItemList(){returnmDataSource;}publicvoidsetItemList(List<PesquisarProdutoObjetoRetorno>itemList){this.mDataSource=itemList;}
}
Codethataddstheitemstothelist:
Buttonadicionar=(Button)sView.findViewById(R.id.lancarvenda_produto_item_btnadicionar);adicionar.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){sAdaptador=(LancarVendaCarrinhoListViewAdapter)lsvCarrinho.getAdapter();sCarrinhoAuxiliar=sAdaptador.getItemList();LancarVendaProdutosFragment.sProdutos.getDadosProduto().setQuantidade(Double.valueOf(quantidade.displayTextView.getText().toString()));sCarrinhoAuxiliar.add(LancarVendaProdutosFragment.sProdutos.getDadosProduto());LancarVendaProdutosTabBarActivitysActivity=((LancarVendaProdutosTabBarActivity)mContext);LancarVendaCarrinhoFragmentsFragment=(LancarVendaCarrinhoFragment)sActivity.getSupportFragmentManager().getFragments().get(1);sFragment.CarregarDados();Toasttoast=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();
}
});