mirror of
https://github.com/LucasVbr/OutilCuisson.git
synced 2026-05-13 17:11:53 +00:00
persistance marche tjs pas
This commit is contained in:
@@ -95,8 +95,7 @@ public class AfficherFragment extends Fragment {
|
|||||||
public void onCreateContextMenu(ContextMenu menu,
|
public void onCreateContextMenu(ContextMenu menu,
|
||||||
View v,
|
View v,
|
||||||
ContextMenu.ContextMenuInfo menuInfo) {
|
ContextMenu.ContextMenuInfo menuInfo) {
|
||||||
// pas sûr du super.getContext() (Noé)
|
new MenuInflater(getContext()).inflate(R.menu.menu_contextuel,
|
||||||
new MenuInflater(super.getContext()).inflate(R.menu.menu_contextuel,
|
|
||||||
menu);
|
menu);
|
||||||
super.onCreateContextMenu(menu, v, menuInfo);
|
super.onCreateContextMenu(menu, v, menuInfo);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ public class AjouterFragment extends Fragment {
|
|||||||
R.string.alert_neutral_button,
|
R.string.alert_neutral_button,
|
||||||
null)
|
null)
|
||||||
.show();
|
.show();
|
||||||
System.out.println(e.getMessage());
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import android.util.Log;
|
|||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.ArrayAdapter;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
@@ -28,11 +30,14 @@ import java.io.FileWriter;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity implements AjouterFragment.EcouteurAjout {
|
public class MainActivity extends AppCompatActivity implements AjouterFragment.EcouteurAjout {
|
||||||
|
|
||||||
@@ -69,128 +74,65 @@ public class MainActivity extends AppCompatActivity implements AjouterFragment.E
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onStart() {
|
||||||
super.onResume();
|
super.onStart();
|
||||||
printFile("print avant readFromFile()");
|
readFromFile();
|
||||||
readFromFile(this);
|
System.out.println("onResume : " + AfficherFragment.cuissonAffichees);
|
||||||
|
|
||||||
printFile("print après readFromFile()");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onDestroy() {
|
||||||
printFile("print avant writeToFile()");
|
System.out.println(
|
||||||
writeToFile("test", this);
|
"Avant write : " + AfficherFragment.cuissonAffichees);
|
||||||
printFile("print après writeToFile()");
|
writeToFile();
|
||||||
super.onPause();
|
System.out.println(
|
||||||
|
"Après write : " + AfficherFragment.cuissonAffichees);
|
||||||
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readFromFile(Context context) {
|
private void writeToFile() {
|
||||||
|
|
||||||
String ret = "";
|
|
||||||
|
|
||||||
try {
|
|
||||||
InputStream inputStream = context.openFileInput(NOM_FICHIER);
|
|
||||||
|
|
||||||
if (inputStream != null) {
|
|
||||||
InputStreamReader inputStreamReader = new InputStreamReader(
|
|
||||||
inputStream);
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(
|
|
||||||
inputStreamReader);
|
|
||||||
String receiveString = "";
|
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
|
||||||
|
|
||||||
while ((receiveString = bufferedReader.readLine()) != null) {
|
|
||||||
stringBuilder.append("\n").append(receiveString);
|
|
||||||
}
|
|
||||||
|
|
||||||
inputStream.close();
|
|
||||||
ret = stringBuilder.toString();
|
|
||||||
}
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
Log.e("login activity", "File not found: " + e.toString());
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e("login activity", "Can not read file: " + e.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeToFile(String data, Context context) {
|
|
||||||
try {
|
|
||||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
|
|
||||||
context.openFileOutput(NOM_FICHIER, Context.MODE_PRIVATE));
|
|
||||||
outputStreamWriter.write(data);
|
|
||||||
outputStreamWriter.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e("Exception", "File write failed: " + e.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// private void writeToFile() {
|
|
||||||
// try {
|
|
||||||
// File path = getFilesDir();
|
|
||||||
// File file = new File(path, NOM_FICHIER);
|
|
||||||
//
|
|
||||||
// if (!path.exists()) {
|
|
||||||
// path.mkdirs();
|
|
||||||
// }
|
|
||||||
// FileWriter fw = new FileWriter(file);
|
|
||||||
// BufferedWriter bw = new BufferedWriter(fw);
|
|
||||||
//
|
|
||||||
// printFile("print avant écriture");
|
|
||||||
//
|
|
||||||
// for (String s : AfficherFragment.cuissonAffichees) {
|
|
||||||
// bw.write(s + "\n");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// printFile("print après écriture");
|
|
||||||
//
|
|
||||||
// fw.close();
|
|
||||||
// } catch (IOException e) {
|
|
||||||
// Log.e(TAG, "File write failed: " + e.toString());
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private void readFromFile() {
|
|
||||||
// try {
|
|
||||||
// File path = getFilesDir();
|
|
||||||
// File file = new File(path, NOM_FICHIER);
|
|
||||||
// FileReader fr = new FileReader(file);
|
|
||||||
// BufferedReader br = new BufferedReader(fr);
|
|
||||||
// String receiveString;
|
|
||||||
//
|
|
||||||
// printFile("print avant lecture");
|
|
||||||
//
|
|
||||||
// while ((receiveString = br.readLine()) != null) {
|
|
||||||
// AfficherFragment.cuissonAffichees.add(receiveString);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// printFile("print après lecture");
|
|
||||||
//
|
|
||||||
// fr.close();
|
|
||||||
// } catch (FileNotFoundException e) {
|
|
||||||
// Log.e(TAG, "File not found: " + e.toString());
|
|
||||||
// } catch (IOException e) {
|
|
||||||
// Log.e(TAG, "Can not read file: " + e.toString());
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void printFile(String intitule) {
|
|
||||||
System.out.println(intitule);
|
|
||||||
try {
|
try {
|
||||||
File path = getFilesDir();
|
File path = getFilesDir();
|
||||||
File file = new File(path, NOM_FICHIER);
|
File file = new File(path, NOM_FICHIER);
|
||||||
BufferedReader in = new BufferedReader(new FileReader(file));
|
|
||||||
String line = in.readLine();
|
if (!path.exists()) {
|
||||||
while (line != null) {
|
path.mkdirs();
|
||||||
System.out.println(line);
|
|
||||||
line = in.readLine();
|
|
||||||
}
|
}
|
||||||
in.close();
|
|
||||||
} catch (FileNotFoundException e) {
|
FileOutputStream fos = new FileOutputStream(file);
|
||||||
e.printStackTrace();
|
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
||||||
|
|
||||||
|
oos.writeObject(AfficherFragment.cuissonAffichees);
|
||||||
|
|
||||||
|
oos.close();
|
||||||
|
fos.close();
|
||||||
|
System.out.println("Ecriture réalisée");
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "File write failed: " + e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readFromFile() {
|
||||||
|
try {
|
||||||
|
File path = getFilesDir();
|
||||||
|
File file = new File(path, NOM_FICHIER);
|
||||||
|
|
||||||
|
FileInputStream fis = new FileInputStream(file);
|
||||||
|
ObjectInputStream ois = new ObjectInputStream(fis);
|
||||||
|
|
||||||
|
AfficherFragment.cuissonAffichees
|
||||||
|
= (ArrayList<String>) ois.readObject();
|
||||||
|
|
||||||
|
ois.close();
|
||||||
|
fis.close();
|
||||||
|
System.out.println("Lecture réalisée");
|
||||||
|
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
Log.e(TAG, "File not found: " + e.toString());
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "Can not read file: " + e.toString());
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user