具有固定的最大产品数量的清单产品
问题描述:
我的应用程序有一个功能,可以在屏幕中列出固定数量的产品(比如5)。具有固定的最大产品数量的清单产品
int max=5;
//when the next button is clicked
if(start<=items)
{
start=start+max;
}
//when the previous button is clicked
start=start-max;
for(int i=start;i<=start+max;i++)
{
//list products process
}
可以有3个或17,或者可以是26 products.But该算法被设计成使得它要经过5.Which的多说为17产物负载20级的产品,其可以应用程序崩溃。我的问题是“有没有办法避免额外的产品加载”?
答
只要改变上限位置:
for(int i=start;i<=start+max;i++)
{
//list products process
}
从start + max
到min(start + max , item_count - 1)
,其中项目计数是项目的总数。 -1是唯一必要的,如果您使用基于0的数组索引的语言。