gpt4 book ai didi

java - 在java类中调用适配器类中的计算函数?

转载 作者:行者123 更新时间:2023-12-01 22:10:29 25 4
gpt4 key购买 nike

我有购物车 Activity ,用户可以在其中添加、更新和删除他们的购物车。每个函数都有它自己的 java Activity ,我也有 CartAdapter Activity 。我想执行一个计算,可以显示 ViewCart Activity 中客户商品的总价。我在 CartAdapter 上进行了计算,但在 ViewCart Activity 中显示 TotalPrice 时遇到问题。

我收到错误消息

java.lang.NumberFormatException: For input string: "99.00" at java.lang.Integer.parseInt(Integer.java:615) at java.lang.Integer.valueOf(Integer.java:801) at com.example.g.Customer.CartAdapter.onBindViewHolder(CartAdapter.java:68) at com.example.g.Customer.CartAdapter.onBindViewHolder(CartAdapter.java:28)

这是我的CartAdapter


public class CartAdapter extends RecyclerView.Adapter<CartAdapter.MyViewHolder> {
private Context context;


List<String> key;


ArrayList<Cart> CartList;

int totalPrice = 0;
int totalPrice1 = 0;


public CartAdapter(ArrayList<Cart> CartListList) {
this.CartList = CartListList;

}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cust_view_cart, viewGroup, false);

context = viewGroup.getContext();
return new CartAdapter.MyViewHolder(view);
}


@NonNull
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {

Picasso.with(context).load(CartList.get(i).getPro_image()).into(myViewHolder.image);
myViewHolder.name.setText(CartList.get(i).getPro_name());
myViewHolder.category.setText(CartList.get(i).getPro_category());
myViewHolder.price.setText(CartList.get(i).getPro_price());
myViewHolder.size.setText(CartList.get(i).getSize());
myViewHolder.quantity.setText(CartList.get(i).getQuantity());


totalPrice1 = (int)Double.parseDouble((CartList.get(i).getPro_price())) * Integer.valueOf(CartList.get(i).getQuantity());
totalPrice = totalPrice + totalPrice1;




myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String cart_id = CartList.get(i).getCart_id();
String keyB = CartList.get(i).getBrand_id();
String pid = CartList.get(i).getPro_id();
String cust_id = CartList.get(i).getCust_id();
String name = CartList.get(i).getPro_name();
String category = CartList.get(i).getPro_category();
String price = CartList.get(i).getPro_price();
String image = CartList.get(i).getPro_image();
String size = CartList.get(i).getSize();
String quantity = CartList.get(i).getQuantity();


Intent intent = new Intent(context, UpdateCart.class);
intent.putExtra("cust_id", cust_id);
intent.putExtra("Cart_id", cart_id);
intent.putExtra("keyB", keyB);
intent.putExtra("pid", pid);
intent.putExtra("pro_name", name);
intent.putExtra("pro_category", category);
intent.putExtra("pro_price", price);
intent.putExtra("pro_image", image);
intent.putExtra("size", size);
intent.putExtra("quantity", quantity);
context.startActivity(intent);
}
});


myViewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String pro_name = CartList.get(i).getPro_name();
String cart_id = CartList.get(i).getCart_id();
String cust_id = CartList.get(i).getCust_id();
DatabaseReference dbCart = FirebaseDatabase.getInstance().getReference("Cart").child(cust_id);
dbCart.child(cart_id).removeValue();

//set url of image to storageref
StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl(CartList.get(i).getPro_image());
// Delete the file
storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// File deleted successfully
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception exception) {
// Uh-oh, an error occurred!
}
});

//Toolbar
// Remove the item on remove/button click
CartList.remove(i);
/*
Parameters
position : Position of the item that has now been removed
*/
notifyItemRemoved(i);

/*
Parameters
positionStart : Position of the first item that has changed
itemCount : Number of items that have changed
*/
notifyItemRangeChanged(i, CartList.size());

// Show the removed item label
Toast.makeText(context, pro_name + " has been removed from your cart", Toast.LENGTH_SHORT).show();

}
});


}

@Override
public int getItemCount() {
return CartList.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {
TextView name, category, price, size, quantity;
ImageView image, btnDelete;

public MyViewHolder(View itemView) {
super(itemView);

name = itemView.findViewById(R.id.name);
category = itemView.findViewById(R.id.category);
image = itemView.findViewById(R.id.image);
price = itemView.findViewById(R.id.price);
size = itemView.findViewById(R.id.size);
quantity = itemView.findViewById(R.id.quantity);
btnDelete = itemView.findViewById(R.id.delete);


}
}
}


查看购物车


public class ViewCart extends AppCompatActivity {

FirebaseAuth firebaseAuth;
DatabaseReference databaseReference;
FirebaseUser currentUser;
ArrayList<Cart> CartList;
RecyclerView recyclerView;

private int totalPrice = 0;
TextView total;


private String userID, cust_id;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_cart);

total = findViewById(R.id.total);

total.setText( String.valueOf(totalPrice));

firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();

if (currentUser != null) {
userID = currentUser.getUid();
cust_id = firebaseAuth.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference("Cart").child(cust_id);
recyclerView = findViewById(R.id.rv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

CartList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {

CartList.add(ds.getValue(Cart.class));
}

CartAdapter cartAdapter = new CartAdapter(CartList);
recyclerView.setAdapter(cartAdapter);

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else
openDialog();

}

private void openDialog() {

LoginDialog loginlDialog = new LoginDialog();
loginlDialog.show(getSupportFragmentManager(), "login dialog");

}

最佳答案

用这个


public class ViewCart extends AppCompatActivity {

FirebaseAuth firebaseAuth;
DatabaseReference databaseReference;
FirebaseUser currentUser;
ArrayList<Cart> CartList;
RecyclerView recyclerView;

private int totalPrice = 0;
TextView total;


private String userID, cust_id;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_cart);

total = findViewById(R.id.total);
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();

if (currentUser != null) {
userID = currentUser.getUid();
cust_id = firebaseAuth.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference("Cart").child(cust_id);
recyclerView = findViewById(R.id.rv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

CartList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {

CartList.add(ds.getValue(Cart.class));
Cart cart = ds.getValue(Cart.class);
totalPrice = totalPrice + (int)Double.parseDouble(cart.getPrice()) * Integer.valueOf(cart.getQuantity());

}

CartAdapter cartAdapter = new CartAdapter(CartList);
recyclerView.setAdapter(cartAdapter);
total.setText( String.valueOf(totalPrice));

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else
openDialog();

}

private void openDialog() {

LoginDialog loginlDialog = new LoginDialog();
loginlDialog.show(getSupportFragmentManager(), "login dialog");

}

关于java - 在java类中调用适配器类中的计算函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58662509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com