今天早上问了类似的问题后,我做了更多研究,发现(我认为)通过将其保存到SharedPreferences(将其转换为Json字符串后)保存/加载我的ArrayList的方法。
问题是,我不知道在哪里放我的saveListe()和我的loadListe()。
我没有收到任何错误,但是当重新启动应用程序时,列表仍然是默认列表(它有1个游戏)。
这两个方法和我的列表都在ListeJeux类中。
我尝试在mainActivity中使用loadListe(),并在添加游戏后在另一个活动中使用saveListe()(将游戏添加到列表中以进行测试)。
如果我启动其他活动,它会在列表中添加一个游戏(此部分正在运行),但是如果我重新启动应用程序,该列表仍然只包含1个游戏(默认)。有什么问题?
ListeJeux(包含保存和加载方法+默认列表):
package com.example.jouons;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
@SuppressWarnings("deprecation")
public class ListeJeux extends ActionBarActivity {
// By default, the list contains one game, if the listView show more than
// one, then addJeu() works, if it still shows more than one game after a
// restart, then the save/load works (not working for now).
static Jeu jeu1 = new Jeu("Ballon fou", "moyens grands", "intérieur");
static ArrayList<Jeu> jeux = new ArrayList<>(Arrays.asList(jeu1));
// This is only to test.
static void addJeu() {
jeux.add(new Jeu("Ballon fou", "moyens grands", "intérieur"));
}
// Takes "jeux" (this is where the problem happens, I think), and makes it a
// Json String. Then store it in SharedPreference.
static public void saveListe(Context context) {
Gson gson = new Gson();
String listeJson = gson.toJson(jeux);
SharedPreferences prefs = context.getSharedPreferences("listePrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("listeEditor", listeJson);
editor.commit();
}
// Takes the contain of the SharedPreference (Json String) and reverts it to
// an ArrayList. Then returns it.
static public ArrayList<Jeu> loadListe(Context context) {
Gson gson = new Gson();
SharedPreferences prefs = context.getSharedPreferences("listePrefs", MODE_PRIVATE);
String extractedJson = prefs.getString("listeEditor", "");
Type type = new TypeToken<ArrayList<Jeu>>() {
}.getType();
ArrayList<Jeu> jeux = gson.fromJson(extractedJson, type);
return jeux;
}
}
MainActivity(我使用loadList()方法):
package com.example.jouons;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
@SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListeJeux.loadListe(this);
setupTrouvezButton();
setupRechercherButton();
setupListeButton();
}
private void setupListeButton() {
Button listeButton = (Button) findViewById(R.id.btnMainListe);
listeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ListeActivity.class));
}
});
}
private void setupTrouvezButton() {
Button trouvezButton = (Button) findViewById(R.id.btnMainTrouvez);
trouvezButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, TrouvezActivity.class));
}
});
}
private void setupRechercherButton() {
Button rechercherButton = (Button) findViewById(R.id.btnMainRechercher);
rechercherButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, RechercherActivity.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
另一项活动(将游戏添加到列表然后保存):
package com.example.jouons;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
@SuppressWarnings("deprecation")
public class RechercherActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechercher);
ListeJeux.addJeu();
ListeJeux.saveListe(this);
setupRetourButton();
}
private void setupRetourButton() {
Button retourButton = (Button) findViewById(R.id.btnRechercherRetour);
retourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.rechercher, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
如果要将arraylist保存到共享首选项,则必须在保存之前序列化arraylist。我宁愿将序列化对象保存到SD卡,而不是将其保存到共享首选项,如果它不是一个秘密信息。如果是缓存文件,我建议您将其保存到应用程序缓存目录。
序列化对象
public class SomeClassName implements Serializable{
private ArrayList<SomeObject> arrayList;
public void setObject(ArrayList<SomeObject> list){
arrayList = list;
}
public ArrayList<SomeObject> getObject(){
return arrayList;
}
}
答案 1 :(得分:0)
ActionBarActivity虽然已弃用,但它扩展了Activity,因此您应该能够覆盖onCreate,onResume和onPause来调用您的方法来加载和保存。例如:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechercher);
setupRetourButton();
}
@Override
protected void onResume()
{
super.onResume();
ArrayList<Jeu> loaded = ListeJeux.loadListe(this);
for (Jeu j: loaded)
{
// This method to add doesn't exist and this whole loop should
// be refactored into the loadListe method.
ListeJeux.addJue(j)
}
// This size() method doesn't exist either and could also be subsumed into loadList
if (ListeJeux.size() == 0)
{
ListeJeux.addJeu();
ListeJeux.saveListe(this);
}
}
@Override
protected void onPause()
{
// Save the list as the app may stop any time after a pause.
ListeJeux.saveListe(this);
super.onPause();
}
就个人而言,我会稍微重构代码,以便列表上的save和load方法(ListeJeux)实际更新内部列表。正如您在Android Documentation顶部的图表中所看到的,您实际上只需要加载onResume,因为它应该在显示Activity之前始终被调用。