Commit 0fe6ddb0 authored by Tristan's avatar Tristan
Browse files

Merge remote-tracking branch 'origin/master'

parents 782884a9 09fee651
......@@ -7,34 +7,54 @@ import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.example.wastedfood.models.ServeurBDD;
import com.example.wastedfood.new_ingredient;
import com.example.wastedfood.models.IngredientItem;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
public class MonFrigo extends AppCompatActivity {
public final ServeurBDD BDD = new ServeurBDD(this);;
private Button ajouter_ingredient;
private ListView listView;
private String ingredient, date;
private HashMap<String,String> map;
private IngredientItem ingredientItem = new IngredientItem();
private List<IngredientItem> list;
private int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mon_frigo);
//list ingrédient
listView = findViewById(R.id.ingredient_list_view);
BDD.open();
Bundle extras = getIntent().getExtras();
if(BDD.getServeurs() !=null){
list=BDD.getServeurs();
while(list.get(i) !=null) {
ingredientItem = list.get(i);
map = new HashMap<String,String>();
map.put("ingrédient",ingredientItem.getName());
map.put("date",ingredientItem.getDate());
ingredientItem.valeurs.add(map);
}
}
if(extras!=null)
{
ingredient = extras.getString("ingrédient");
date = extras.getString("date");
map = new HashMap<String,String>();
BDD.insererIng(ingredient,date);
map.put("ingrédient",ingredient);
map.put("date",date);
ingredientItem.valeurs.add(map);
......
......@@ -5,13 +5,13 @@ import java.util.ArrayList;
import java.util.HashMap;
public class IngredientItem {
/*
// attribut
private String name;
private String date;
// Constructeur
public IngredientItem(String name, String date){
public IngredientItem(){
this.name = name;
this.date = date;
}
......@@ -20,8 +20,13 @@ public class IngredientItem {
public String getName(){
return name;
}
public void setName(String A){
this.name= A;
}
public void setDate(String A){
this.date= A;
}
public String getDate() {return date; }
*/
public static ArrayList<HashMap<String,String>> valeurs = new ArrayList<HashMap<String, String>>();
}
package com.example.wastedfood.models;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class ServeurBDD
{
private SQLiteDatabase bdd = null;
private ServeurSQL serveurSQLite = null;
public ServeurBDD(Context context)
{
// cn crée la BDD et sa table
serveurSQLite = new ServeurSQL(context);
}
public void open()
{
// on ouvre la BDD en écriture
if (bdd == null)
bdd = serveurSQLite.getWritableDatabase();
}
public void close()
{
if (bdd != null)
if (bdd.isOpen())
bdd.close();
}
public SQLiteDatabase getBDD()
{
return bdd;
}
public long insererIng(String a,String b)
{
ContentValues values = new ContentValues();
values.put(ServeurSQL.COL_NOM, a);
values.put(ServeurSQL.COL_DATE_PER, b);
return bdd.insert(ServeurSQL.TABLE_SERVEURS, null, values);
}
public int modifierIng(int id, IngredientItem serveur)
{
ContentValues values = new ContentValues();
values.put(ServeurSQL.COL_NOM, serveur.getName());
values.put(ServeurSQL.COL_DATE_PER, serveur.getDate());
return bdd.update(ServeurSQL.TABLE_SERVEURS, values, ServeurSQL.COL_NOM + " = " + id, null);
}
public int supprimerIng(int id)
{
return bdd.delete(ServeurSQL.TABLE_SERVEURS, ServeurSQL.COL_NOM + " = " + id, null);
}
public IngredientItem getServeur(String nom)
{
Cursor c = bdd.query(ServeurSQL.TABLE_SERVEURS, new String[] {ServeurSQL.COL_NOM, ServeurSQL.COL_DATE_PER}, ServeurSQL.COL_NOM + " LIKE \"" + nom +"\"", null, null, null, null);
return cursorToServeur(c, true);
}
/*public IngredientItem getServeurByAdresseIP(String adresseIP)
{
Cursor c = bdd.rawQuery("SELECT * FROM" + ServeurSQL.TABLE_SERVEURS + " WHERE " + ServeurSQL.COL_DATE_PER + " = ?", new String[] { adresseIP });
return cursorToServeur(c, true);
}*/
public List<IngredientItem> getServeurs()
{
List<IngredientItem> serveurs = new ArrayList<IngredientItem>();
Cursor cursor = bdd.query(ServeurSQL.TABLE_SERVEURS, new String[] {ServeurSQL.COL_NOM, ServeurSQL.COL_DATE_PER}, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
IngredientItem serveur = cursorToServeur(cursor, false);
serveurs.add(serveur);
cursor.moveToNext();
}
cursor.close();
return serveurs;
}
// Cette méthode permet de convertir un cursor en un objet de type Serveur
private IngredientItem cursorToServeur(Cursor c, boolean one)
{
if (c.getCount() == 0)
return null;
if(one == true)
c.moveToFirst();
IngredientItem serveur = new IngredientItem();
serveur.setName(c.getString(ServeurSQL.NUM_COL_NOM));
serveur.setDate(c.getString(Integer.parseInt(ServeurSQL.COL_DATE_PER)));
if(one == true)
c.close();
return serveur;
}
}
\ No newline at end of file
package com.example.wastedfood.models;
import android.content.Context;
import android.database.sqlite.*;
import android.support.annotation.Nullable;
public class ServeurSQL extends SQLiteOpenHelper
{
public static final String DATABASE_NAME = "serveurs.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_SERVEURS = "table_serveurs";
public static final String COL_NOM = "NOM";
public static final String COL_DATE_PER = "DATE_PER";
public static final int NUM_COL_ID = 0;
public static final int NUM_COL_NOM = 1;
public static final int NUM_COL_DATE_PER = 2;
public static final int NUM_COL_PORT = 3;
private static final String CREATE_BDD = new StringBuilder().append("CREATE TABLE ").append(TABLE_SERVEURS).append(" (").append(COL_NOM).append(" KEY, ").append(COL_DATE_PER).append(" TEXT NOT NULL,").toString();
public ServeurSQL(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public ServeurSQL(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db)
{
// on crée la table
db.execSQL(CREATE_BDD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// au choix : on supprime la table puis on la recrée
db.execSQL("DROP TABLE " + TABLE_SERVEURS + ";");
onCreate(db);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment