gpt4 book ai didi

java - 如何在 Android Studio 中添加/删除项目时计算总价

转载 作者:行者123 更新时间:2023-11-29 23:11:57 24 4
gpt4 key购买 nike

我是 Android 的新手。我正在尝试创建一个电子商务应用程序。目前,我正在创建添加到购物车的 activity。因此,我设法将商品添加到购物车并显示在 CartActivity 中。但问题是,我不知道如何获取价格并计算所有项目的总价。当我删除项目时(我使用 onSwiped),价格没有下降。

CartActivity

  public class CartActivity extends AppCompatActivity implements 
RecyclerItemTouchHelper.RecyclerItemTouchHelperListener {
private float totalcost=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.d(TAG, "onDataChange: found a post id: babyyywhuu " + book_id);

new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}

// Called when a user swipes left or right on a ViewHolder
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {

int position = viewHolder.getAdapterPosition(); // this is how you can get the position
Book book= mAdapter.getItem(position); // You will have your own class ofcourse.

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
reference.child("carts")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child(book.getBook_id())
.removeValue();

}
}).attachToRecyclerView(recyclerView);
init();

}


private void init(){
Log.d(TAG, "init: initializing.");
cartList = new ArrayList<>();
setupPostsList();

//reference for listening when items are added or removed from the watch list
mDatabaseRef = FirebaseDatabase.getInstance().getReference()
.child(getString(R.string.node_carts))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());

//set the listener to the reference
mDatabaseRef.addValueEventListener(mLisenter);

}

private void getWatchListIds(){
Log.d(TAG, "getWatchListIds: getting users watch list.");
if(cartList != null){
cartList.clear();
}
if(mPostsIds != null){
mPostsIds.clear();
}

mPostsIds = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

Query query = reference.child(getString(R.string.node_carts))
.orderByKey()
.equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid());

query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getChildren().iterator().hasNext()){
DataSnapshot singleSnapshot = dataSnapshot.getChildren().iterator().next();
for(DataSnapshot snapshot: singleSnapshot.getChildren()){
String id = snapshot.child(getString(R.string.field_book_id)).getValue().toString();
Log.d(TAG, "onDataChange: found a post id: " + id);
mPostsIds.add(id);

}
getPosts();
}else{
getPosts();
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

private void getPosts(){
if(mPostsIds.size() > 0){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

for(int i = 0; i < mPostsIds.size(); i++){
Log.d(TAG, "getPosts: getting post information for: " + mPostsIds.get(i));

Query query = reference.child(getString(R.string.node_books))
.orderByKey()
.equalTo(mPostsIds.get(i));

query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot singleSnapshot = dataSnapshot.getChildren().iterator().next();
Book book = singleSnapshot.getValue(Book.class);
Log.d(TAG, "onDataChange: found a post: " + book.getTitle());
cartList.add(book);
mAdapter = new CartListAdapter (CartActivity.this, cartList);
recyclerView.setAdapter(mAdapter);
here----> totalcost += Float.parseFloat(book.getPrice());
String totPrice = Float.toString(totalcost);
TextView totPrice = findViewById(R.id.totalPrice);
totPrice.setText(totPrice);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}
}
}
@Override
public void onResume() {
super.onResume();
cartList = new ArrayList<>();


}

/**
* callback when recycler view is swiped
* item will be removed on swiped
* undo option will be provided in snackbar to restore the item
*/
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
if (viewHolder instanceof CartListAdapter.MyViewHolder) {
// get the removed item name to display it in snack bar
String name = cartList.get(viewHolder.getAdapterPosition()).getTitle();

// backup of removed item for undo purpose
final Book deletedItem = cartList.get(viewHolder.getAdapterPosition());
final int deletedIndex = viewHolder.getAdapterPosition();

// remove the item from recycler view
mAdapter.removeItem(viewHolder.getAdapterPosition());

// showing snack bar with Undo option
Snackbar snackbar = Snackbar
.make(linearlayour, name + " removed from cart!", Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {

// undo is selected, restore the deleted item
mAdapter.restoreItem(deletedItem, deletedIndex);

}
});
snackbar.setActionTextColor(Color.YELLOW);
snackbar.show();
}
}

enter image description here

我尝试在 getPosts() 中计算它,但我知道这是完全错误的,因为价格一直在上涨。我是否必须创建方法来增加/减少方法?你对此有什么解决办法吗?感谢您的宝贵时间。

最佳答案

有 4 种情况您必须更新价格:-

  1. 添加一个新项目(在你的案例中可能没有使用只是因为你在购物车页面上)
  2. 删除项目
  3. 更新项目数量(增加或减少数量)
  4. 应用任何优惠券代码

只需创建一个新函数并重新计算总价,设置为 TextView 并在适配器通知或更新适配器后立即调用。

关于java - 如何在 Android Studio 中添加/删除项目时计算总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55929882/

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