2016-09-28 11 views
0

私は数十のチュートリアルを試してみました。問題のアプリには、リストを保持するカスタムクラスがあり、その中にリストを持つアイテムがあります。理想的には、閉じたり一時停止したりするときに、他のアクティビティから保存できるようにしたいと考えています。私が実行している問題は、別のアクティビティを閉じるとき(戻るボタンを押すと)、保存メッセージのログが表示されることです。しかし、私がそれを開いたときに情報を読み込むのではなく、それを保存しておくと、空のリストが表示されます。ここに私のコードは次のとおりです。Androidスタジオ - ファイルにカスタムクラス(ArrayListを含む)を保存

これは、カスタムクラスを参照して、保存およびロードする方法を呼び出すことができる主な活動です:

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

他の活動はそれほどのような保存機能を呼び出します。

@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?

0123:

はここアイブ氏が試した最も最近の投稿の小さなリストです

これがなぜ機能していないのか、私がしなければならないことを誰かが分かってくれれば、私は大いに感謝する。

おかげで、その中に多くの

+2

'それはcrashes' - >スタックトレースを? – njzk2

+0

私たちがお手伝いできるスタックトレースを投稿してください。 –

+0

私の悪い。私がfos = openFileOutput( "f.rem"、this.MODE_PRIVATE)を持っていたときにクラッシュしました。今は何もしていないようです。両方のログメッセージがログに表示されますが、アプリを閉じてもう一度開くと何も読み込まれません。ミックスアップして申し訳ありません。 – Superdsg

答えて

0

私は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); 
    } 
} 
関連する問題