作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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();
}
}
我尝试在 getPosts()
中计算它,但我知道这是完全错误的,因为价格一直在上涨。我是否必须创建方法来增加/减少方法?你对此有什么解决办法吗?感谢您的宝贵时间。
最佳答案
有 4 种情况您必须更新价格:-
只需创建一个新函数并重新计算总价,设置为 TextView 并在适配器通知或更新适配器后立即调用。
关于java - 如何在 Android Studio 中添加/删除项目时计算总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55929882/
import java.util.Scanner; import java.util.Date; import java.text.DateFormat; import java.text.Simpl
您将看到示例代码。 以下是我的问题:-我必须选择有值(value)的服务,但是当我取消服务时,总价格增加而不是减少。-我测试后取消了所有服务,但是总价还是有。我怎样才能让它回到零? 谢谢! :) 这是
我是一名优秀的程序员,十分优秀!