Finition des méthodes dans le fichier AfficherFragment + Création fichier CuissonAdapter pour afficher les trois arguments de la liste

This commit is contained in:
Thizy Alexandre
2022-02-23 12:19:23 +01:00
parent 96af2c51de
commit 5384820fa8
7 changed files with 145 additions and 45 deletions
@@ -14,11 +14,20 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class AfficherFragment extends Fragment {
@@ -26,9 +35,13 @@ public class AfficherFragment extends Fragment {
/**
* Liste des cuissons enregistré dans l'application
*/
public static ArrayList<Cuisson> cuissonAffichees = new ArrayList<>();
public static ArrayList<com.example.outilcuisson.Cuisson> cuissonAffichees = new ArrayList<>();
ListView listeCuisson;
static View listeCuisson1;
static ArrayList listeCuisson;
private ArrayAdapter<String> adaptateur;
private ArrayList<String> cuissonAAfficher;
/**
* Ajouter une cuisson
@@ -46,15 +59,20 @@ public class AfficherFragment extends Fragment {
private static void updateSaveFile() {
// TODO Il faut creer un fichier cuisson.txt si il n'existe pas
// try {
// FileOutputStream fos = new FileOutputStream("dataCuisson.txt", false);
// ObjectOutputStream oos = new ObjectOutputStream(fos);
// oos.writeObject(listeCuisson);
// oos.close();
// fos.close();
// } catch (IOException ioe) {
// ioe.printStackTrace();
// }
File file;
FileOutputStream fos = null;
try {
file = new File("dataCuisson.txt");
fos = new FileOutputStream(file);
if(!file.exists()){
file.createNewFile();
}
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(listeCuisson1);
oos.close(); fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
@@ -63,21 +81,20 @@ public class AfficherFragment extends Fragment {
private static void loadSaveFile() {
// TODO à décommenter lorsque updateSaveFile() sera terminé
// try {
// FileInputStream fis = new FileInputStream("dataCuisson.txt");
// ObjectInputStream ois = new ObjectInputStream(fis);
//
// listeCuisson = (ArrayList) ois.readObject();
//
// ois.close();
// fis.close();
// } catch (FileNotFoundException ignored) {
// } catch (IOException ioe) {
// ioe.printStackTrace();
// } catch (ClassNotFoundException c) {
// System.out.println("Class not found");
// c.printStackTrace();
// }
try {
FileInputStream fis = new FileInputStream("dataCuisson.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
listeCuisson = (ArrayList) ois.readObject();
ois.close();
fis.close();
} catch (FileNotFoundException ignored) {
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Class not found");
c.printStackTrace();
}
}
public AfficherFragment() {
@@ -97,8 +114,8 @@ public class AfficherFragment extends Fragment {
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.afficher_fragment, container, false);
loadSaveFile();
listeCuisson = view.findViewById(R.id.listeCuisson);
registerForContextMenu(listeCuisson);
listeCuisson1 = view.findViewById(R.id.listeCuisson);
registerForContextMenu(listeCuisson1);
return view;
}
@@ -16,7 +16,8 @@ import android.widget.TimePicker;
import androidx.fragment.app.Fragment;
import com.exercice.onglet.outilcuisson2021.OutilCuisson;
import com.example.outilcuisson.OutilCuisson;
import com.example.outilcuisson.Cuisson;
public class AjouterFragment extends Fragment {
@@ -0,0 +1,57 @@
package com.example.outilcuisson;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import java.util.List;
public class CuissonAdapter extends ArrayAdapter<Cuisson> {
private int identifiantVueItem;
public CuissonAdapter(@NonNull Context context, int vueItem, List<Cuisson> lesCuissons) {
super(context, vueItem,lesCuissons);
this.identifiantVueItem = vueItem;
}
public View getView(int position, View uneVue, ViewGroup parent){
Cuisson unPlat = getItem(position);
LinearLayout vueItemListe;
if (uneVue == null) {
/*
* la vue décrivant chaque item de la liste n'est pas encore créée
* Il faut désérialiser le layout correspondant à cette vue.
*/
LayoutInflater outil;
outil = (LayoutInflater)getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vueItemListe = (LinearLayout) outil.inflate(identifiantVueItem,
parent, false);
} else {
vueItemListe = (LinearLayout) uneVue;
}
// on accède aux 2 widgets présents sur la vue
TextView vuePlat = vueItemListe.findViewById(R.id.item_plat);
TextView vueDuree = vueItemListe.findViewById(R.id.item_duree);
TextView vueDegres = vueItemListe.findViewById(R.id.item_degres);
// on place dans les 2 widgets les valeurs de l'item à afficher
vuePlat.setText(unPlat.getPlat());
vueDuree.setText(unPlat.getHeure());
vueDegres.setText(Integer.toString(unPlat.getDegree()));
return vueItemListe;
}
}
@@ -17,7 +17,7 @@
/* A priori de fichier ne doit pas être modifié. Vous restez libre de le modifier si
besoin
*/
package com.exercice.onglet.outilcuisson2021;
package com.example.outilcuisson;
/**
+17 -8
View File
@@ -2,28 +2,37 @@
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
android:stretchColumns="3">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/afficher_titre" />
android:text="@string/afficher_titre"
android:layout_marginBottom="15dp"/>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/afficher_label_plat" />
android:text="@string/afficher_label_plat"
android:layout_column="1"
android:layout_span="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/afficher_label_duree"
android:layout_column="2"
android:layout_span="1"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/afficher_label_duree" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/afficher_label_temperature" />
android:text="@string/afficher_label_temperature"
android:layout_column="3"
android:layout_span="1"/>
</TableRow>
<ListView
+21 -5
View File
@@ -5,13 +5,29 @@
température soient bien alignées)
Un padding est ajouté autour du texte
fichier ligne_liste.xml 12/21 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:id="@+id/item_plat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textStyle="bold"/>
<!--android:textSize="@dimen/taille_texte_liste"
android:paddingTop="@dimen/padding_liste"
android:paddingBottom="@dimen/padding_liste"
android:textColor="@color/couleur_item_liste"-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_duree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textStyle="bold"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_degres"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textStyle="bold"/>
</LinearLayout>