清空所有项目后,列表返回一个项目。

huangapple go评论81阅读模式
英文:

List is returning one after clearing all the items by deleting

问题

我列表清空后通过删除返回一个项目,在应用程序刚安装时返回 null,这是好的,但在添加项目然后全部删除后,当从该活动返回并再次进入时,list.size() 返回 1 并且仍然有一个项目。我不知道是否从缓存对象实例加载,这是我的适配器类代码:

private List<CartModel> cartModelList;

它在应用程序刚安装时返回 null这是好的但当我将项目添加到购物车然后删除所有项目时它返回 1

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

>问题是如何移除那个列表对象缓存或是否有任何替代方法

我尝试过删除按钮但仍然会出现缓存

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) {
    // ... 省略其他部分 ...
}

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

class Viewholder extends RecyclerView.ViewHolder {
    // ... 省略其他部分 ...
}

图片链接:[https://i.stack.imgur.com/PxDTZ.jpg]

英文:

My List is returning an item after clearing all the items by deleting ,On app fresh install its returing null which is good but after adding item and then by deleting all, this happens when go back from that activity and come again, list.size() is returning 1 and an item is remaing ,i don't know if it is loading from cache object instance here is my code of adapter class

[please look to the image attached ,list is empty but still counter 1 counter = cartModelList.size()]i have a list of cart items private List&lt;CartModel&gt; cartModelList;

It's returning null on app fresh install which is good but when i add item to the cart and then remove all the items then its returning 1.

I mean cartmodelList.size() is returning as far I know it's returning some items from cached objects or some thing like that.

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

I tried on delete button but still cached coming


    public static  double p = 0;
    private List&lt;CartModel&gt; cartModelList;
    Database db;
    Context context;

    public CartAdapter(Context context, List&lt;CartModel&gt; 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 &lt;= 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, &quot;Product  deleted from cart&quot;, Toast.LENGTH_LONG).show();



                } else {

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

                }

                CartList user111 = new CartList(--COUNTER_BADGE);

                //  Toast.makeText(context, &quot;else&quot;, 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(&quot;&#163;&quot;+floatprice);
            quantity.setText(int_quantity);
            linetotal.setText(&quot;&#163;&quot;+float_line_total);


        }


    }


[https://i.stack.imgur.com/PxDTZ.jpg]

答案1

得分: 0

好的... 第一个。

if (db.deleteProduct(cartModelList.get(position).getID())) 
不会从cartModelList中删除你的物品你需要手动处理像这样

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

第二个部分。你只能在删除方法的最后调用notifyDataSetChanged()itemChangeditemRemoved等。请告诉我,是否有效。

附注:你的项目没有被缓存。问题出现在你的代码顺序上。

编辑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, "已从购物车中删除产品", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "未能从购物车中删除产品", 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);
    }
});
英文:

Okay... The first.

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

will not delete your item from cartModelList, you need to do it manually. Like this:

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

And the second. You have to call notifyDataSetChanged() or itemChanged or itemRemoved etc. only in the end of your deletion method. Please, tell me, if it worked.

P.S. Your items do not cached. The problem is in your code order.

Edit 1. Also, you need to check your db.deleteProduct method. Is it worked? Is your if statement worked?

Edit 2. Try this.

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, &quot;Product  deleted from cart&quot;, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, &quot;Product not deleted from cart&quot;, Toast.LENGTH_LONG).show();
}
CartList user111 = new CartList(cartModelList.size());
//  Toast.makeText(context, &quot;else&quot;, 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);
}
});

答案2

得分: 0

我的问题通过在删除按钮上添加 cartModelList.clearcartModelList.size() == 1 时得以解决,因此在删除最后一项后,它将清空列表。

英文:

my problem is solved by putting cartModelList.clear on delete button when cartModelList.size() == 1 , so after deleting the last item it will clear the list.

huangapple
  • 本文由 发表于 2020年4月8日 02:36:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61087047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定