2017-09-01 1 views
0

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; 
} 

}

+1

Ich denke, es wäre besser zu speichern "anExpiry" und berechnen tatsächlichen shelfLife in Adapter – egoldx

+0

Vielen Dank, das ist, was ich eigentlich dachte, aber Adapter wird es jeden Tag ändern? – Joanna

+0

Die Methode 'getView' wird jedes Mal aufgerufen, wenn Sie die Ansicht erstellen. Am einfachsten ist es wohl, 'adapter.notifyDataSetChanged' in' onResume() '' – egoldx

Antwort

0

Dies ist ein allgemeiner Datumszähler mit den Jahren und Monaten. Wenn Sie nur Tage haben möchten, können Sie verwandte Codes (Jahr und Monat) weglassen.

Calendar thatDay = Calendar.getInstance(); 
     thatDay.set(Calendar.DAY_OF_MONTH,25); 
     thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less thatDay.set(Calendar.YEAR, 1985); 

     Calendar today = Calendar.getInstance(); 

     long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis 

Hier ist eine Annäherung ...

long days = diff/(24 * 60 * 60 * 1000); 

Wenn Sie eine App in Firebase entwickeln dann lassen Sie es mich wissen.