Android Studio- 将自定义 class(使用 ArrayList)保存到文件

Android Studio- Save custom class (with ArrayList) to a file

我已经尝试了 Whosebug 和其他网站上的几十个教程和指南,试图让它正常工作。有问题的应用程序有一个自定义 class,它包含一个列表,其中的项目包含一个列表。理想情况下,我希望能够在其他活动关闭或暂停时保存它们。我 运行 遇到的问题是,当我关闭另一个 activity(通过按后退按钮)时,它会显示保存消息的日志。但是当我重新打开它而不是加载它应该保存的信息时,它显示一个空白列表。这是我的代码:

这是引用自定义class的主要activity,可以调用保存和加载方法:

public class ReminderList extends AppCompatActivity {

    public static ListHolder Lholder;
    public static ArrayList<ReminderType> AllReminders=new ArrayList<>();
    public reminderCAdapter adapter;
    public static ReminderType currentReminderType;
    public static ItemType currentItemType;
    public static TimeType currentTimesType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder_list);
        //generate list
        File file=new File("f.rem");
        if(file.exists()) {
            try {
                LoadData();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }

            AllReminders=Lholder.Rlist;
        }
        adapter=new reminderCAdapter(AllReminders,this);
        ReminderType r1=new ReminderType("Thing1");
        //AllReminders.add(r1);
        ReminderType r2=new ReminderType("Thing2");
        //AllReminders.add(r2);

        //instantiate custom adapter
        //MyCustomAdapter adapter = new MyCustomAdapter(AllReminders, this);

        //handle listview and assign adapter
        ListView lView = (ListView)findViewById(R.id.listView);
        lView.setAdapter(adapter);
    }

    public void onResume(){
        super.onResume();
        adapter.notifyDataSetChanged();
    }

    public static void SaveData() throws IOException {
        Log.e("Saving Data", "trying");
        FileOutputStream fos=new FileOutputStream("f.rem");
        ObjectOutputStream oos= new ObjectOutputStream(fos);
        oos.writeObject(Lholder);
        oos.close();
    }

    public void LoadData() throws IOException, ClassNotFoundException {
        Log.e("Loading Data", "trying");
        FileInputStream fis=new FileInputStream("f.rem");
        ObjectInputStream ois=new ObjectInputStream(fis);
        Lholder=(ListHolder) ois.readObject();
        ois.close();
    }

    public void AddToList(View view){
        Intent intent = new Intent(this,ReminderSettings.class);
        intent.putExtra("Type", "new");
        startActivity(intent);
    }
}

另一个activity像这样调用保存函数:

@Override
    protected void onStop() {
        super.onStop();  // Always call the superclass method first
        try {
            ReminderList.SaveData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这是我尝试过的最新帖子的一小部分列表:

How to create a file on Android Internal Storage?

http://www.101apps.co.za/articles/using-android-s-file-system-for-saving-application-data-part-1-saving-files.html

如果有人能帮助我理解为什么这不起作用或我需要做什么,我将不胜感激。

非常感谢

如果不将数组转换为 json 字符串并将其他数组嵌套在其中,我无法将其保存到文件中:

public class ReminderList extends AppCompatActivity {

    public static ArrayList<ReminderType> AllReminders=new ArrayList<>();
    public reminderCAdapter adapter;
    public static ReminderType currentReminderType;
    public static ItemType currentItemType;
    public static TimeType currentTimesType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.context=this;
        setContentView(R.layout.activity_reminder_list);
        //generate list
            try {
                LoadData();
            } catch (IOException | ClassNotFoundException | JSONException e) {
                e.printStackTrace();
            }
        alarmMan=(AlarmManager) getSystemService(ALARM_SERVICE);

        adapter=new reminderCAdapter(AllReminders,this);

        //handle listview and assign adapter
        ListView lView = (ListView)findViewById(R.id.listView);
        lView.setAdapter(adapter);
    }


    public void onResume(){
        super.onResume();
        adapter.notifyDataSetChanged();
    }

    public static void SaveData(Context context) throws IOException, JSONException {
        Log.e("Saving Data", "trying");

        JSONArray reminderData=new JSONArray();
        JSONObject reminder;
        JSONArray jItemData;
        JSONObject jItem;
        JSONArray jTimeData;
        JSONObject jTime;

        for(int i=0; i<AllReminders.size();i++){
            //create the reminder item
            reminder=new JSONObject();
            jItemData=new JSONArray();
            reminder.put("RemName",AllReminders.get(i).ReminderName);
            //create each of the itemtype items
            for(int j=0; j<AllReminders.get(i).Items.size();j++){
                jItem=new JSONObject();
                jTimeData=new JSONArray();
                jItem.put("iName",AllReminders.get(i).Items.get(j).ItemName);
                jItem.put("iNote",AllReminders.get(i).Items.get(j).AdditionalNote);
                //create each time item
                for(int h=0; h<AllReminders.get(i).Items.get(j).Times.size();h++){
                    jTime=new JSONObject();
                    jTime.put("hr",AllReminders.get(i).Items.get(j).Times.get(h).hour);
                    jTime.put("min",AllReminders.get(i).Items.get(j).Times.get(h).minute);
                    jTimeData.put(jTime);
                }
                //String tList=jTimeData.toString();
                jItem.put("timeList",jTimeData);
                jItemData.put(jItem);
            }
            //String iList=jItemData.toString();
            reminder.put("itemList",jItemData);
            Log.e("reminderItem", reminder.toString());
            reminderData.put(reminder);
        }
        String remList=reminderData.toString();
        FileOutputStream fos=context.openFileOutput("savedData",MODE_PRIVATE);
        fos.write(remList.getBytes());
        fos.close();
    }

    public void LoadData() throws IOException, ClassNotFoundException, JSONException {
        Log.e("Loading Data", "trying");
        FileInputStream fis=openFileInput("savedData");
        BufferedInputStream bis= new BufferedInputStream(fis);
        StringBuffer b=new StringBuffer();
        while(bis.available() !=0){
            char c=(char) bis.read();
            b.append(c);
        }

        bis.close();
        fis.close();

        JSONArray reminderData= new JSONArray(b.toString());
        //Log.e("b string", b.toString());

        for(int i=0; i<reminderData.length();i++){
            JSONObject remObj=reminderData.getJSONObject(i);
            String rName=remObj.getString("RemName");
            //Log.e("Rname",rName);
            ReminderType remi=new ReminderType(rName);
            JSONArray itemArray=remObj.getJSONArray("itemList");
            for(int h=0; h<itemArray.length();h++){
                JSONObject itemObj= itemArray.getJSONObject(h);
                String iName=itemArray.getJSONObject(h).getString("iName");
                //Log.e("iName",iName);
                ItemType iTem= new ItemType(iName);
                JSONArray timeArray=itemObj.getJSONArray("timeList");
                for(int j=0; j<timeArray.length();j++){
                    JSONObject timeObj=timeArray.getJSONObject(j);
                    int hr=timeObj.getInt("hr");
                    int min=timeObj.getInt("min");
                    TimeType tIme=new TimeType(hr,min);
                    tIme.SetDisplayTime();
                    iTem.Times.add(tIme);
                }
                remi.Items.add(iTem);
            }
            AllReminders.add(remi);
        }
    }

    public void AddToList(View view) throws IOException, JSONException {
        Intent intent = new Intent(this,ReminderSettings.class);
        intent.putExtra("Type", "new");
        startActivity(intent);
    }
}