Ich erstelle eine kleine App, die aufzeichnet, was Benutzer in einem Kühlschrank/Gefrierschrank hat. Es gibt eine Liste von Produkten in Form eines ListView, zu dem Benutzer ein neues Produkt hinzufügen können. Wenn wir neue hinzufügen, können wir ein Ablaufdatum auswählen, mit dem App in der Lage sein soll, zu zählen, wie viele Tage noch übrig sind. Ich habe eine adapter
und ein paar activities
, aber nicht sicher, ob die Anzahl der Tage wird sich jeden Tag ändern? Kann ich es anders machen? Hier ist mein Code:Lebende Tage leben
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;
}
}
Ich denke, es wäre besser zu speichern "anExpiry" und berechnen tatsächlichen shelfLife in Adapter – egoldx
Vielen Dank, das ist, was ich eigentlich dachte, aber Adapter wird es jeden Tag ändern? – Joanna
Die Methode 'getView' wird jedes Mal aufgerufen, wenn Sie die Ansicht erstellen. Am einfachsten ist es wohl, 'adapter.notifyDataSetChanged' in' onResume() '' – egoldx