0
我正在创建一个小应用程序,记录用户在冰箱/冰柜中的使用情况。 有一个列表视图的形式的产品的列表,用户可以添加新产品。当添加新的时候,我们可以选择一个失效日期,应用程序应该能够计算剩余的天数。我有一个adapter
和几个activities
,但不知道每天的天数是否会改变?有什么其他方式可以做到吗?这里是我的代码:计数剩余天数
public class Product implements Serializable {
/**
* The name of the food product
*/
private String name;
/**
* The quantity of product
*/
private int quantity;
private Metrics measure;
/**
* The expiry date of the product
*/
private Date expiry;
/**
* The price of the product
*/
private double price;
/**
* The shelf life of the product
*/
private int shelfLife;
/**
*
*/
private FoodCategory foodType;
/**
*
* @param aName
* @param aQuantity
* @param anExpiry
* @param aPrice
*/
public Product(String aName, int aQuantity, Metrics aMeasure, Date anExpiry, double aPrice, FoodCategory aType)
{
this.name = aName;
this.quantity = aQuantity;
this.measure = aMeasure;
this.price = aPrice;
this.expiry = anExpiry;
this.foodType = aType;
Date date = new Date();
this.shelfLife = (int)(anExpiry.getTime()-date.getTime())/(1000 * 60 * 60 * 24);
}
/**
* Returns the name of the product
*
* @return name
*/
public String getName()
{
return name;
}
/**
*
* @param aName
*/
public void setName(String aName)
{
this.name = aName;
}
/**
* Returns the quantity of the product
* @return quantity
*/
public int getQuantity()
{
return quantity;
}
public Metrics getMetric()
{
return measure;
}
/**
*
* @param aQuantity
*/
public void setQuantity(int aQuantity)
{
this.quantity = aQuantity;
}
/**
* Returns the price of the product
* @return price
*/
public double getPrice()
{
return price;
}
/**
*
* @param aPrice
*/
public void setPrice(double aPrice)
{
this.price = aPrice;
}
/**
* Returns the number of days of the shelf life of product
* @return shelfLife
*/
public int getShelfLife()
{
return shelfLife;
}
/**
*
* @return String representation of Product object
*/
public FoodCategory getFoodType()
{
return foodType;
}
public void setFoodType(FoodCategory aType)
{
this.foodType = aType;
}
@Override
public String toString()
{
return name + " " + quantity + " " + price + " " + expiry + " " + shelfLife;
}
}
public class ProductAdapter extends BaseAdapter {
Context context;
ArrayList<Product> products;
public ProductAdapter(Context context, ArrayList<Product> products)
{
this.context = context;
this.products = products;
}
@Override
public int getCount() {
return products.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listview_product,null);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView quantity = (TextView) convertView.findViewById(R.id.quantity);
TextView metric = (TextView) convertView.findViewById(R.id.metric);
TextView expiry = (TextView) convertView.findViewById(R.id.expiry);
Product newProduct = products.get(position);
name.setText(newProduct.getName());
int q = newProduct.getQuantity();
String s = String.valueOf(q);
quantity.setText(s);
metric.setText(newProduct.getMetric().toString());
int d = newProduct.getShelfLife();
String ss = String.valueOf(d);
expiry.setText(ss + " day/s left");
System.getProperty("line.separator");
return convertView;
}
}
我想最好是将'anExpiry'保存在适配器中并计算实际的shelfLife – egoldx
谢谢,这正是我当时的想法,但是适配器每天都会改变它吗? – Joanna
每次创建视图时都会调用getView方法。最简单的事情可能是在'onResume()' – egoldx