gpt4 book ai didi

java - 通过删除清除所有项目后列表返回一项

转载 作者:行者123 更新时间:2023-12-01 17:32:45 26 4
gpt4 key购买 nike

我的列表在通过删除清除所有项目后返回一个项目,在应用程序全新安装时返回 null,这很好,但在添加项目然后删除所有项目后,从该 Activity 返回并再次出现时会发生这种情况,列表.size() 返回 1 并且有一个项目保留,我不知道它是否从缓存对象实例加载,这是我的适配器类代码

[请查看所附图片,列表为空,但计数器仍然为 1 counter = cartModelList.size()]我有一个购物车项目列表 private List<CartModel> cartModelList;

它正在返回null在应用程序全新安装上,这很好,但是当我将商品添加到购物车然后删除所有商品时,它会返回 1。

我的意思是cartmodelList.size()据我所知,它正在返回缓存对象中的一些项目或类似的东西。

The question is how to remove that List object cached or any alternative?

我尝试了删除按钮,但仍然缓存了


public static double p = 0;
private List<CartModel> cartModelList;
Database db;
Context context;

public CartAdapter(Context context, List<CartModel> cartModelList) {
this.cartModelList = cartModelList;
this.context = context;
db = new Database(context);
}

@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_layout_item, parent, false);
return new Viewholder(view);
}

@Override
public void onBindViewHolder(@NonNull final Viewholder holder, final int position) {


String namee = cartModelList.get(position).getName();
String manufacturere = cartModelList.get(position).getManufacturer();
String availabilitye = cartModelList.get(position).getAvailability();
String e_parte = cartModelList.get(position).getE_part();
String m_parte = cartModelList.get(position).getM_part();
String floatprice = cartModelList.get(position).getUnit_();
String int_quantity = cartModelList.get(position).getQuantity();
String float_line_total = cartModelList.get(position).getLine_total();



holder.setItemDetails(namee, manufacturere, availabilitye, e_parte, m_parte, floatprice, int_quantity, float_line_total);


int checker = SharedPrefManager.getInstance(context).cartcount().getCounter();

if (checker <= 0){

cartModelList.clear();

}

holder.btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (db.deleteProduct(cartModelList.get(position).getID())) {

cartModelList.remove(position);
notifyDataSetChanged();

Toast.makeText(context, "Product deleted from cart", Toast.LENGTH_LONG).show();



} else {

Toast.makeText(context, "Product not deleted from cart", Toast.LENGTH_LONG).show();

}

CartList user111 = new CartList(--COUNTER_BADGE);

// Toast.makeText(context, "else", Toast.LENGTH_SHORT).show();
SharedPrefManager.getInstance(context).cartList(user111);
((Activity)context).invalidateOptionsMenu();
((Activity)context).finish();
Intent intent = new Intent(context, CartActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(intent);

}



});


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

class Viewholder extends RecyclerView.ViewHolder {

private TextView name;
private TextView manufacturer;
private TextView availability;
private TextView e_part;
private TextView m_part;
private TextView price;
private EditText quantity;
private TextView linetotal;
private Button btn_delete;
private Button btn_update;
private Button adapter_livestock;
public SpinKitView progressbar;

public Viewholder(@NonNull View itemView) {
super(itemView);

name = itemView.findViewById(R.id.name);
manufacturer = itemView.findViewById(R.id.manufacturer);
availability = itemView.findViewById(R.id.availability);
e_part = itemView.findViewById(R.id.e_part);
m_part = itemView.findViewById(R.id.m_part);
price = itemView.findViewById(R.id.price);
quantity = itemView.findViewById(R.id.quantity);
linetotal = itemView.findViewById(R.id.linetotal);
btn_delete = itemView.findViewById(R.id.btn_delete);
btn_update = itemView.findViewById(R.id.btn_update);
adapter_livestock = itemView.findViewById(R.id.adapter_livestock);
progressbar = itemView.findViewById(R.id.adapterrprogresslivestockprogress);

}


private void setItemDetails(String namee, String manufacturere, String availabilitye, String e_parte, String m_parte, String floatprice, String int_quantity, String float_line_total) {

name.setText(namee);
manufacturer.setText(manufacturere);
availability.setText(availabilitye);
e_part.setText(e_parte);
m_part.setText(m_parte);
price.setText("£"+floatprice);
quantity.setText(int_quantity);
linetotal.setText("£"+float_line_total);


}


}


[/image/PxDTZ.jpg]

最佳答案

好的...第一个。

if (db.deleteProduct(cartModelList.get(position).getID())) 

不会从 cartModelList 中删除您的商品,您需要手动执行此操作。像这样:

if (db.deleteProduct(cartModelList.get(position).getID())) {
cartModelList.remove(position)

还有第二个。您必须仅在删除方法的末尾调用notifyDataSetChanged()或itemChanged或itemRemoved等。请告诉我它是否有效。

附注您的项目不会被缓存。问题出在您的代码顺序中。

编辑 1. 另外,您需要检查您的 db.deleteProduct 方法。有效吗?你的 if 语句有效吗?

编辑2。试试这个。

holder.btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (db.deleteProduct(cartModelList.get(position).getID())) {
cartModelList.remove(position);
notifyItemRemoved(position);

Toast.makeText(context, "Product deleted from cart", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Product not deleted from cart", Toast.LENGTH_LONG).show();
}

CartList user111 = new CartList(cartModelList.size());

// Toast.makeText(context, "else", Toast.LENGTH_SHORT).show();
SharedPrefManager.getInstance(context).cartList(user111);
((Activity)context).invalidateOptionsMenu();
((Activity)context).finish();
Intent intent = new Intent(context, CartActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(intent);
}
});

关于java - 通过删除清除所有项目后列表返回一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61087047/

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