如何在片段中获得总计
how to get grand total in fragment
请帮助我,我正面临这些问题,并且需要花费更多时间来解决这些问题以获得片段总计,但他们多次调用所以请显示视频并建议我找出并解决 problem.its用于检查我的知识的简单杂货店应用程序请帮助并可能分享另一种如此快速的方法来快速获得我的解决方案...感谢社区支持......
i want similer to these in fragmnet
我的购物车片段代码
public class CartFragment extends Fragment implements UpdateInf{
RecyclerView cartList;
// public static List<ProductModel> cartModels;
private List<ProductModel> cartItemList;
public static TextView tvTotalWithCheckout;
CartHelper cartHelper;
public static double grandTotalPlus = 0.0d;
int cartCount;
ProductModel productModel;
UpdateInf updateInf;
public CartFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cart, container, false);
cartList = view.findViewById(R.id.cartList);
tvTotalWithCheckout = view.findViewById(R.id.tvTotalWithCheckout);
cartHelper = new CartHelper(getActivity());
productModel = new ProductModel();
updateInf = this;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
cartList.setLayoutManager(linearLayoutManager);
//LayoutAnimationController layoutAnimationController = AnimationUtils.loadLayoutAnimation(getActivity(), R.anim.layout_anim_right_to_left);
// cartList.getAdapter().notifyDataSetChanged();
// cartList.setLayoutAnimation(layoutAnimationController);
//cartList.scheduleLayoutAnimation();
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
cartList.addItemDecoration(dividerItemDecoration);
cartItemList = cartHelper.getAllProducts();
MyCartAdapter myCartAdapter = new MyCartAdapter(getActivity(), cartItemList, updateInf);
cartList.setAdapter(myCartAdapter);
// tvTotalWithCheckout.setText( "\u20B9" + grandTotal(cartItemList));
Log.e("grandCheckTotal", "setMyCartItem: "+grandTotalPlus);
tvTotalWithCheckout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), CheckoutActivity.class);
startActivity(intent);
}
});
return view;
}
@Override
public void setQuantity(int quantity) {
grandTotal(cartItemList);
Log.e("grandTotal", "setQuantity: "+ grandTotalPlus);
}
private double grandTotal(List<ProductModel> cartItemList) {
for (ProductModel model : cartItemList) {
// Log.e("cartData", "setMyCartItem: " + model.getProductId() + ":" + model.getProductName());
cartCount = model.getProductQty();
grandTotalPlus += Double.parseDouble(model.getProductPrice()) * cartCount;
}
return grandTotalPlus;
}
}
**my adapter code**
public class MyCartAdapter extends RecyclerView.Adapter<MyCartAdapter.Holder> {
private Context context;
private List<ProductModel> productModels;
CartHelper cartHelper;
ProductModel productModel;
UpdateInf updateInf;
public MyCartAdapter(Context context, List<ProductModel> productModels, UpdateInf updateInf) {
this.context = context;
this.productModels = productModels;
cartHelper = new CartHelper(context);
this.updateInf = updateInf;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_cart_items, parent, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull final Holder holder, final int position) {
productModel = productModels.get(position);
Glide.with(context).load(productModels.get(position).getProductImage()).into(holder.cartImgProduct);
holder.tvProductName.setText(productModels.get(position).getProductName());
holder.tvProductPrice.setText("\u20B9" + productModels.get(position).getProductPrice());
holder.tvProductType.setText(productModels.get(position).getProductType());
holder.tvQty.setText(String.valueOf(productModels.get(position).getProductQty()));
holder.imgIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//grandTotalPlus = 0.0d;
int cartIncrementItem = productModels.get(position).getProductQty();
cartIncrementItem += 1;
productModels.get(position).setProductQty(cartIncrementItem);
// double cash = Double.parseDouble(productModels.get(position).getProductPrice()) * productModels.get(position).getProductQty();
holder.tvQty.setText(String.valueOf(productModels.get(position).getProductQty()));
// productModels.get(position).setTotalCash(cash);
cartHelper.updateCart(new ProductModel(productModels.get(position).getProductId(), productModels.get(position).getProductQty()));
updateInf.setQuantity(cartIncrementItem);
}
});
holder.imgDecrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int cartDecrementItem = productModels.get(position).getProductQty();
if (cartDecrementItem == 1) {
} else {
cartDecrementItem -= 1;
productModels.get(position).setProductQty(cartDecrementItem);
holder.tvQty.setText(String.valueOf(productModels.get(position).getProductQty()));
updateInf.setQuantity(cartDecrementItem);
}
cartHelper.updateCart(new ProductModel(productModels.get(position).getProductId(), productModels.get(position).getProductQty()));
}
});
}
@Override
public int getItemCount() {
return productModels.size();
}
class Holder extends RecyclerView.ViewHolder {
ImageView cartImgProduct, imgRemoveCart, imgIncrement, imgDecrement;
TextView tvProductName, tvProductPrice, tvProductType, tvQty;
public Holder(@NonNull View itemView) {
super(itemView);
cartImgProduct = itemView.findViewById(R.id.cartImgProduct);
tvProductName = itemView.findViewById(R.id.tvProductName);
tvProductPrice = itemView.findViewById(R.id.tvProductPrice);
tvProductType = itemView.findViewById(R.id.tvProductType);
imgIncrement = itemView.findViewById(R.id.imgIncrement);
imgDecrement = itemView.findViewById(R.id.imgDecrement);
tvQty = itemView.findViewById(R.id.tvQty);
}
}
}
我的购物车助手代码
public class CartHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "grocery_store";
private static final String TABLE_CART = "cart";
private static final String KEY_PRODUCT_ID = "product_id";
private static final String KEY_PRODUCT_NAME = "product_name";
private static final String KEY_PRODUCT_PRICE = "product_price";
private static final String KEY_PRODUCT_TYPE = "product_type";
private static final String KEY_PRODUCT_IMAGE = "product_image";
private static final String KEY_PRODUCT_QTY = "product_qty";
private static final String KEY_TOTAL_CASH = "total_cash";
public CartHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CART_TABLE =
"CREATE TABLE " + TABLE_CART + "(" + KEY_PRODUCT_ID + " TEXT PRIMARY KEY,"
+ KEY_PRODUCT_NAME + " TEXT,"
+ KEY_PRODUCT_PRICE + " TEXT,"
+ KEY_PRODUCT_TYPE + " TEXT,"
+ KEY_PRODUCT_IMAGE + " TEXT,"
+ KEY_PRODUCT_QTY + " INTEGER" + ")";
db.execSQL(CREATE_CART_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CART);
onCreate(db);
}
public void addToCart(ProductModel productModels) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_PRODUCT_ID, productModels.getProductId());
contentValues.put(KEY_PRODUCT_NAME, productModels.getProductName());
contentValues.put(KEY_PRODUCT_PRICE, productModels.getProductPrice());
contentValues.put(KEY_PRODUCT_TYPE, productModels.getProductType());
contentValues.put(KEY_PRODUCT_IMAGE, productModels.getProductImage());
contentValues.put(KEY_PRODUCT_QTY,productModels.getProductQty());
sqLiteDatabase.insert(TABLE_CART, null, contentValues);
sqLiteDatabase.close();
}
public List<ProductModel> getAllProducts() {
List<ProductModel> productModelList = new ArrayList<ProductModel>();
String query = "SELECT * FROM " + TABLE_CART;
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
ProductModel productModel = new ProductModel();
productModel.setProductId(cursor.getString(0));
productModel.setProductName(cursor.getString(1));
productModel.setProductPrice(cursor.getString(2));
productModel.setProductType(cursor.getString(3));
productModel.setProductImage(cursor.getString(4));
productModel.setProductQty(cursor.getInt(5));
productModelList.add(productModel);
} while (cursor.moveToNext());
}
return productModelList;
}
public int updateCart(ProductModel productModel) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PRODUCT_QTY, productModel.getProductQty());
//values.put(KEY_TOTAL_CASH,productModel.getTotalCash());
//values.put(KEY_PRODUCT_PRICE,productModel.getProductPrice());
// updating row
return db.update(TABLE_CART, values, KEY_PRODUCT_ID + " = ? ",
new String[]{String.valueOf(productModel.getProductId())});
}
// Deleting single contact
public void deleteCart(ProductModel productModel) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CART, KEY_PRODUCT_ID + " = ?",
new String[]{String.valueOf(productModel.getProductId())});
db.close();
}
}
private double grandTotal(List<ProductModel> cartItemList) {
grandTotalPlus = 0.0d // Reinitialze to zero
for (ProductModel model : cartItemList) {
// Log.e("cartData", "setMyCartItem: " + model.getProductId() + ":" + model.getProductName());
cartCount = model.getProductQty();
grandTotalPlus += Double.parseDouble(model.getProductPrice()) * cartCount;
}
return grandTotalPlus;
}
grandtotalplus
是一个静态变量,当您离开页面时它的值永远不会重置,因此当您第一次访问购物车页面时它的值是 50。在那之后,当您返回并添加更多项目然后返回购物车页面时,它仍然具有值 50,您开始添加它。这就是你得到 135 的原因。
请帮助我,我正面临这些问题,并且需要花费更多时间来解决这些问题以获得片段总计,但他们多次调用所以请显示视频并建议我找出并解决 problem.its用于检查我的知识的简单杂货店应用程序请帮助并可能分享另一种如此快速的方法来快速获得我的解决方案...感谢社区支持......
i want similer to these in fragmnet
我的购物车片段代码
public class CartFragment extends Fragment implements UpdateInf{
RecyclerView cartList;
// public static List<ProductModel> cartModels;
private List<ProductModel> cartItemList;
public static TextView tvTotalWithCheckout;
CartHelper cartHelper;
public static double grandTotalPlus = 0.0d;
int cartCount;
ProductModel productModel;
UpdateInf updateInf;
public CartFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cart, container, false);
cartList = view.findViewById(R.id.cartList);
tvTotalWithCheckout = view.findViewById(R.id.tvTotalWithCheckout);
cartHelper = new CartHelper(getActivity());
productModel = new ProductModel();
updateInf = this;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
cartList.setLayoutManager(linearLayoutManager);
//LayoutAnimationController layoutAnimationController = AnimationUtils.loadLayoutAnimation(getActivity(), R.anim.layout_anim_right_to_left);
// cartList.getAdapter().notifyDataSetChanged();
// cartList.setLayoutAnimation(layoutAnimationController);
//cartList.scheduleLayoutAnimation();
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
cartList.addItemDecoration(dividerItemDecoration);
cartItemList = cartHelper.getAllProducts();
MyCartAdapter myCartAdapter = new MyCartAdapter(getActivity(), cartItemList, updateInf);
cartList.setAdapter(myCartAdapter);
// tvTotalWithCheckout.setText( "\u20B9" + grandTotal(cartItemList));
Log.e("grandCheckTotal", "setMyCartItem: "+grandTotalPlus);
tvTotalWithCheckout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), CheckoutActivity.class);
startActivity(intent);
}
});
return view;
}
@Override
public void setQuantity(int quantity) {
grandTotal(cartItemList);
Log.e("grandTotal", "setQuantity: "+ grandTotalPlus);
}
private double grandTotal(List<ProductModel> cartItemList) {
for (ProductModel model : cartItemList) {
// Log.e("cartData", "setMyCartItem: " + model.getProductId() + ":" + model.getProductName());
cartCount = model.getProductQty();
grandTotalPlus += Double.parseDouble(model.getProductPrice()) * cartCount;
}
return grandTotalPlus;
}
}
**my adapter code**
public class MyCartAdapter extends RecyclerView.Adapter<MyCartAdapter.Holder> {
private Context context;
private List<ProductModel> productModels;
CartHelper cartHelper;
ProductModel productModel;
UpdateInf updateInf;
public MyCartAdapter(Context context, List<ProductModel> productModels, UpdateInf updateInf) {
this.context = context;
this.productModels = productModels;
cartHelper = new CartHelper(context);
this.updateInf = updateInf;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_cart_items, parent, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull final Holder holder, final int position) {
productModel = productModels.get(position);
Glide.with(context).load(productModels.get(position).getProductImage()).into(holder.cartImgProduct);
holder.tvProductName.setText(productModels.get(position).getProductName());
holder.tvProductPrice.setText("\u20B9" + productModels.get(position).getProductPrice());
holder.tvProductType.setText(productModels.get(position).getProductType());
holder.tvQty.setText(String.valueOf(productModels.get(position).getProductQty()));
holder.imgIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//grandTotalPlus = 0.0d;
int cartIncrementItem = productModels.get(position).getProductQty();
cartIncrementItem += 1;
productModels.get(position).setProductQty(cartIncrementItem);
// double cash = Double.parseDouble(productModels.get(position).getProductPrice()) * productModels.get(position).getProductQty();
holder.tvQty.setText(String.valueOf(productModels.get(position).getProductQty()));
// productModels.get(position).setTotalCash(cash);
cartHelper.updateCart(new ProductModel(productModels.get(position).getProductId(), productModels.get(position).getProductQty()));
updateInf.setQuantity(cartIncrementItem);
}
});
holder.imgDecrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int cartDecrementItem = productModels.get(position).getProductQty();
if (cartDecrementItem == 1) {
} else {
cartDecrementItem -= 1;
productModels.get(position).setProductQty(cartDecrementItem);
holder.tvQty.setText(String.valueOf(productModels.get(position).getProductQty()));
updateInf.setQuantity(cartDecrementItem);
}
cartHelper.updateCart(new ProductModel(productModels.get(position).getProductId(), productModels.get(position).getProductQty()));
}
});
}
@Override
public int getItemCount() {
return productModels.size();
}
class Holder extends RecyclerView.ViewHolder {
ImageView cartImgProduct, imgRemoveCart, imgIncrement, imgDecrement;
TextView tvProductName, tvProductPrice, tvProductType, tvQty;
public Holder(@NonNull View itemView) {
super(itemView);
cartImgProduct = itemView.findViewById(R.id.cartImgProduct);
tvProductName = itemView.findViewById(R.id.tvProductName);
tvProductPrice = itemView.findViewById(R.id.tvProductPrice);
tvProductType = itemView.findViewById(R.id.tvProductType);
imgIncrement = itemView.findViewById(R.id.imgIncrement);
imgDecrement = itemView.findViewById(R.id.imgDecrement);
tvQty = itemView.findViewById(R.id.tvQty);
}
}
}
我的购物车助手代码
public class CartHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "grocery_store";
private static final String TABLE_CART = "cart";
private static final String KEY_PRODUCT_ID = "product_id";
private static final String KEY_PRODUCT_NAME = "product_name";
private static final String KEY_PRODUCT_PRICE = "product_price";
private static final String KEY_PRODUCT_TYPE = "product_type";
private static final String KEY_PRODUCT_IMAGE = "product_image";
private static final String KEY_PRODUCT_QTY = "product_qty";
private static final String KEY_TOTAL_CASH = "total_cash";
public CartHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CART_TABLE =
"CREATE TABLE " + TABLE_CART + "(" + KEY_PRODUCT_ID + " TEXT PRIMARY KEY,"
+ KEY_PRODUCT_NAME + " TEXT,"
+ KEY_PRODUCT_PRICE + " TEXT,"
+ KEY_PRODUCT_TYPE + " TEXT,"
+ KEY_PRODUCT_IMAGE + " TEXT,"
+ KEY_PRODUCT_QTY + " INTEGER" + ")";
db.execSQL(CREATE_CART_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CART);
onCreate(db);
}
public void addToCart(ProductModel productModels) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_PRODUCT_ID, productModels.getProductId());
contentValues.put(KEY_PRODUCT_NAME, productModels.getProductName());
contentValues.put(KEY_PRODUCT_PRICE, productModels.getProductPrice());
contentValues.put(KEY_PRODUCT_TYPE, productModels.getProductType());
contentValues.put(KEY_PRODUCT_IMAGE, productModels.getProductImage());
contentValues.put(KEY_PRODUCT_QTY,productModels.getProductQty());
sqLiteDatabase.insert(TABLE_CART, null, contentValues);
sqLiteDatabase.close();
}
public List<ProductModel> getAllProducts() {
List<ProductModel> productModelList = new ArrayList<ProductModel>();
String query = "SELECT * FROM " + TABLE_CART;
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
ProductModel productModel = new ProductModel();
productModel.setProductId(cursor.getString(0));
productModel.setProductName(cursor.getString(1));
productModel.setProductPrice(cursor.getString(2));
productModel.setProductType(cursor.getString(3));
productModel.setProductImage(cursor.getString(4));
productModel.setProductQty(cursor.getInt(5));
productModelList.add(productModel);
} while (cursor.moveToNext());
}
return productModelList;
}
public int updateCart(ProductModel productModel) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PRODUCT_QTY, productModel.getProductQty());
//values.put(KEY_TOTAL_CASH,productModel.getTotalCash());
//values.put(KEY_PRODUCT_PRICE,productModel.getProductPrice());
// updating row
return db.update(TABLE_CART, values, KEY_PRODUCT_ID + " = ? ",
new String[]{String.valueOf(productModel.getProductId())});
}
// Deleting single contact
public void deleteCart(ProductModel productModel) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CART, KEY_PRODUCT_ID + " = ?",
new String[]{String.valueOf(productModel.getProductId())});
db.close();
}
}
private double grandTotal(List<ProductModel> cartItemList) {
grandTotalPlus = 0.0d // Reinitialze to zero
for (ProductModel model : cartItemList) {
// Log.e("cartData", "setMyCartItem: " + model.getProductId() + ":" + model.getProductName());
cartCount = model.getProductQty();
grandTotalPlus += Double.parseDouble(model.getProductPrice()) * cartCount;
}
return grandTotalPlus;
}
grandtotalplus
是一个静态变量,当您离开页面时它的值永远不会重置,因此当您第一次访问购物车页面时它的值是 50。在那之后,当您返回并添加更多项目然后返回购物车页面时,它仍然具有值 50,您开始添加它。这就是你得到 135 的原因。