【问题标题】:Android: Saving an ArrayList to SharedPreferences, but loading it is not workingAndroid:将 ArrayList 保存到 SharedPreferences,但加载它不起作用
【发布时间】:2015-12-18 03:27:00
【问题描述】:

今天早上问了一个类似的问题后,我做了更多的研究,发现(我认为)一种通过将 ArrayList 保存到 SharedPreferences(将其转换为 Json 字符串后)来保存/加载它的方法。

问题是,我不知道将 saveListe() 和 loadListe() 放在哪里。

我没有收到任何错误,但重新启动应用程序时,列表仍然是默认列表(其中有 1 场比赛)。

这 2 个方法和我的列表都在 ListeJeux 类中。

我尝试在 mainActivity 中使用 loadListe(),并在添加游戏后在另一个 Activity(将游戏添加到列表中以进行测试)中使用了 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);
    }
}

【问题讨论】:

    标签: java android arraylist save sharedpreferences


    【解决方案1】:

    如果要将数组列表保存到共享首选项,则必须在保存之前对数组列表进行序列化。如果它不是机密信息,我宁愿将序列化对象保存到 sdcard,而不是将其保存到共享优先级。如果它是一个缓存文件,我建议你将它保存到应用程序缓存目录。

    序列化一个对象

    public class SomeClassName implements Serializable{
        private ArrayList<SomeObject> arrayList;
    
        public void setObject(ArrayList<SomeObject> list){
            arrayList = list;
        }
    
        public ArrayList<SomeObject> getObject(){
            return arrayList;
        }
    }
    

    【讨论】:

      【解决方案2】:

      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();
      }
      

      就个人而言,我会稍微重构代码,以便列表 (ListeJeux) 上的保存和加载方法实际上更新内部列表。从Android Documentation 顶部的图表中可以看出,您实际上只需要加载 onResume,因为它应该始终在 Activity 显示之前调用。

      【讨论】:

      • 谢谢!但现在我正试图弄清楚将这些方法放在哪里,我也不确定你对 onResume() 方法中的那些 cmets 是什么意思:为什么要使用不存在的方法?
      • 把方法放在名为MainActivity的ActionBarActivity中。我假设您将编写不存在的方法,因为 ListeJeux 包装了一个数组列表,所以这是为了访问该内部 ArrayList
      猜你喜欢
      • 1970-01-01
      • 2011-10-26
      • 2015-12-10
      • 2016-03-24
      • 1970-01-01
      • 2020-02-16
      • 2013-01-25
      相关资源
      最近更新 更多