J'ai deux ArrayLists dans deux activités. La première ArrayList contient environ 1000 éléments (enregistrés dans un fichier JSON) et la deuxième ArrayList contient 0 éléments.Enregistrement de données ArrayList après la commutation Activités
Il est possible d'ajouter 1 article à ArrayList2 avec « onclick »
Mais quand je ferme l'application des activités wont sauver.
Comment sauvegarder ces modifications?
1.MainActivity:
public class MainActivity extends AppCompatActivity {
static ArrayList<Recipe> recipeList = new ArrayList();
public class ObjList{
public ArrayList<String> recipeList = new ArrayList<>();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.recipe_list_view);
recipeList = Recipe.getRecipesFromFile("recipes.json", this);
String[] listItems = new String[recipeList.size()];
for (int i = 0; i < recipeList.size(); i++) {
Recipe recipe = recipeList.get(i);
listItems[i] = recipe.title;
}
final RecipeAdapter adapter = new RecipeAdapter(this, recipeList);
mListView.setAdapter(adapter);
final Context context = this;
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Recipe selectedRecipe = recipeList.get(position);
recipeList.remove(selectedRecipe);
recipeList2.add(selectedRecipe);
adapter.notifyDataSetChanged();
}
});
};
public void test_onClick(View view) {
Intent intent = new Intent(MainActivity.this, TestActivity.class);
startActivity(intent);
}
private ListView mListView;
}
2.Activity:
public class TestActivity extends AppCompatActivity {
static ArrayList<Recipe> recipeList2 = new ArrayList<>();
public void onCreate(Bundle savedInstanceState) {
ListView mListView;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mListView = (ListView) findViewById(R.id.testlist2);
final RecipeAdapter adapter = new RecipeAdapter (this, recipeList2);
mListView.setAdapter(adapter);
final Context context = this;
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Recipe selectedRecipe = recipeList2.get(position);
recipeList2.remove(selectedRecipe);
recipeList.add(selectedRecipe);
adapter.notifyDataSetChanged();
}});}
public void test2_onClick(View view) {
Intent intent = new Intent(TestActivity.this, MainActivity.class);
startActivity(intent);
}}
Recipe.class:
public static ArrayList<Recipe> getRecipesFromFile(String filename, Context context){
final ArrayList<Recipe> recipeList = new ArrayList<>();
try {
// Load data
String jsonString = loadJsonFromAsset("recipes.json", context);
JSONObject json = new JSONObject(jsonString);
JSONArray recipes = json.getJSONArray("recipes");
// Get Recipe objects from data
for(int i = 0; i < recipes.length(); i++){
Recipe recipe = new Recipe();
recipe.title = recipes.getJSONObject(i).getString("title");
recipeList.add(recipe);
}
} catch (JSONException e) {
e.printStackTrace();
}
return recipeList; }
private static String loadJsonFromAsset(String filename, Context context) {
String json = null;
try {
InputStream is = context.getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
}
catch (java.io.IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
Je ne vois pas de code qui tente de sauvegarder la liste ... Pourquoi avez-vous pensé que cela permettrait d'économiser? Essayez d'écrire dans un fichier –
Oui, je sais parce que je ne sais pas comment enregistrer ma liste – GvrG
Ouvrir un objet 'Fichier' sur le disque, et y écrire –