I'm working on a E-commerce App to place some online orders. In my Recyclerview there are products, which are added at the previous activity. Here i'm populating the added products in the Recyclerview with the details such as item name, qty, price etc.. There are buttons to increase/decrease the qty of each products and another button to delete the item too.
At the bottom of the Recyclerview Layout there is another textview to Show the grand total. As I'm handling the button slicks in the RecyclerViewAdapter class, while user updating the qty or deleting an item, I have to update the Grand total. I tried some solutions already mentioned here, but the app crashes at random button clicks. Please provide a solution or a better way to do this.
Here is my Adapter class:
public class InvoiceRecyclerViewAdapter extends RecyclerView.Adapter<InvoiceRecyclerViewAdapter.ViewHolder> {
private static final String TAG = "RecylerViewAdapter";
List<Products> addedProductsList;
Context mContext;
public InvoiceRecyclerViewAdapter(Context mContext,List<Products> addedProductsList)
{
this.mContext=mContext;
this.addedProductsList=addedProductsList;
}
public InvoiceRecyclerViewAdapter(Context mContext)
{
this.mContext=mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_invoice,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.item_qty.setText("1");
holder.itemname.setText(addedProductsList.get(position).getName());
holder.itemprice.setText("Rs "+addedProductsList.get(position).getPrice());
holder.itemdiscount.setText("Rs "+calculate_dis(position));
holder.itemtotal.setText("Rs "+calculate_total(position));
holder.button_inc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int qty = addedProductsList.get(position).getQty();
qty++;
addedProductsList.get(position).setQty(qty);
holder.item_qty.setText(""+qty);
holder.itemdiscount.setText("Rs "+calculate_dis(position));
holder.itemtotal.setText("Rs "+calculate_total(position));
UpdateTotal();
}
});
holder.button_dec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int qty = addedProductsList.get(position).getQty();
qty--;
if(qty>0){
addedProductsList.get(position).setQty(qty);
holder.item_qty.setText(""+qty);
holder.itemdiscount.setText("Rs"+calculate_dis(position));
holder.itemtotal.setText("Rs"+calculate_total(position));
UpdateTotal();
}
}
});
holder.button_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addedProductsList.get(position).setAddedTocart(false);
//notifyDataSetChanged();
addedProductsList.remove(position);
notifyItemRemoved(position);
UpdateTotal();
}
});
}
@Override
public int getItemCount() {
return addedProductsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView itemname,itemtotal,itemprice,itemdiscount,item_qty;
Button button_cancel,button_inc,button_dec;
ConstraintLayout parent_layout;
public ViewHolder(View itemView) {
super(itemView);
itemname = itemView.findViewById(R.id.textView_item_name);
itemprice = itemView.findViewById(R.id.textView_item_price);
itemdiscount = itemView.findViewById(R.id.textView_item_discount);
itemtotal = itemView.findViewById(R.id.textView_item_total);
button_cancel = itemView.findViewById(R.id.button_cancel);
button_inc=itemView.findViewById(R.id.button_inc);
button_dec=itemView.findViewById(R.id.button_dec);
item_qty = itemView.findViewById(R.id.textView_qty);
parent_layout = itemView.findViewById(R.id.invoice_parent_layout);
}
}
private float calculate_dis(int pos){
float dis = 0;
if(addedProductsList.get(pos).isPrice_g_enabled()){
int qty=addedProductsList.get(pos).getQty();
dis = Float.parseFloat(addedProductsList.get(pos).getPrice())-Float.parseFloat(addedProductsList.get(pos).getPrice_g());
dis = qty*dis;
}
return dis;
}
private float calculate_total(int pos){
int qty=addedProductsList.get(pos).getQty();
float price,total;
if(addedProductsList.get(pos).isPrice_g_enabled()){
price = Float.parseFloat(addedProductsList.get(pos).getPrice_g());
}
else{
price= Float.parseFloat(addedProductsList.get(pos).getPrice());
}
total = price*qty;
return total;
}
private void UpdateTotal(){
TextView txtView =((EditQuantity)mContext).findViewById(R.id.textView_total);
float total=0,price=0;
int qty=0;
for(int i=0;i<addedProductsList.size();i++){
if(addedProductsList.get(i).isAddedTocart()) {
qty = addedProductsList.get(i).getQty();
if(addedProductsList.get(i).isPrice_g_enabled()){
price = Float.parseFloat(addedProductsList.get(i).getPrice_g());
}
else{
price= Float.parseFloat(addedProductsList.get(i).getPrice());
}
}
total=total+(qty*price);
}
txtView.setText("Rs "+total);
}
}
Here is the Main Activity part:
public class EditQuantity extends AppCompatActivity {
ArrayList<Products> products_list;
ArrayList<Products> cart_productslist= new ArrayList<>();
Products added_product;
FloatingActionButton fab_next;
TextView total_textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_quantity);
products_list = getIntent().getParcelableArrayListExtra("products");
Products products;
for(int i=0;i<products_list.size();i++){
products = products_list.get(i);
if(products.isAddedTocart()){
added_product = new Products(products.getName(),products.getPrice(),products.getPrice_g(),products.getImage(),1,true,products.isPrice_g_enabled());
cart_productslist.add(added_product);
}
}
Toolbar toolbar = findViewById(R.id.toolbar_invoice);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Cart");
total_textview = findViewById(R.id.textView_total);
total_textview.setText("Rs "+CalTotal());
fab_next = findViewById(R.id.fab_next);
fab_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
InitRecylerView();
}
@Override
public void onBackPressed() {
}
private void InitRecylerView() {
RecyclerView recyclerView = findViewById(R.id.invoice_recyclerview);
InvoiceRecyclerViewAdapter adapter = new InvoiceRecyclerViewAdapter(
this ,cart_productslist);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public float CalTotal(){
float total=0;
float i_total,price;
for(int i=0;i<cart_productslist.size();i++){
if(cart_productslist.get(i).isAddedTocart()) {
int qty = cart_productslist.get(i).getQty();
if(cart_productslist.get(i).isPrice_g_enabled()){
price = Float.parseFloat(cart_productslist.get(i).getPrice_g());
}
else{
price= Float.parseFloat(cart_productslist.get(i).getPrice());
}
i_total = price * qty;
total = total + i_total;
}
}
return total;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…