2017-08-25 4 views
0

私はそのデータにオブジェクトクラスを使用するカスタムListViewを持っています。ユーザーはリストビューに新しい項目を追加することができ、arraylistはSharedPreferencesに保存されます。ただし、個々のアイテムを別のExpandable ListViewで使用できるように保存する場合、ユーザーが作成する個々のアイテムごとにファイルを作成するにはどうすればよいでしょうか?前もって感謝します。ここでは、オブジェクトのクラスがある:?データベースを使用しないのはなぜカスタムクラスオブジェクトのデータモデルをjavaで作成するたびに別のファイルに保存するにはどうすればよいですか?

public class Item implements Serializable{ 
String homework, date, classes; 

    public Item(String homework, String date, String classes){ 
     this.homework = homework; 
     this.date = date; 
     this.classes = classes; 
    } 

    public String getHomework(){ 
     return homework; 
    } 
    public String getDate(){ 
     return date; 
    } 
    public String getClasses(){ 
     return classes; 
    } 
} 

答えて

0

あり設定のビットがありますが、初期設定までのデータを読み込み/書き込みには非常に簡単です後。 https://developer.android.com/training/basics/data-storage/databases.html

+0

あなたのデータベースは良いアイデアのように聞こえますが、私はあなたがリンクしているものを読むでしょう、他の良いチュートリアルはありますか? –

+0

データベースの仕組みがわからない場合は、それらについて読んだり、これを見てください:https://www.youtube.com/watch?v = KgiCxe-ZW8o データベース、テーブル、およびクエリを理解したら、リンク私はあなたがアンドロイドでそれを構築するのに十分でなければならない前にあなたに与えました。 –

0

シリアル化 - デシリアライズクラスの例

public final class Serialize 
{ 
    private static final String className = Serialize.class.getName(); 

    public static void save(Object saveThis, String serializeFileName, Context context) 
    { 
    FileOutputStream fos = null; 
    ObjectOutputStream oos = null; 

    try 
    { 
     if(saveThis != null) 
     { 
     fos = context.openFileOutput(serializeFileName, Context.MODE_PRIVATE); 
     oos = new ObjectOutputStream(fos); 
     oos.writeObject(saveThis); 
     } 
    } 
    catch(Throwable t) 
    { 
     //log it 
    } 
    finally 
    { 
     if(oos != null) 
     { 
     try{oos.close();}catch(Throwable t){} 
     } 
     if(fos != null) 
     { 
     try{fos.close();}catch(Throwable t){} 
     } 
    } 
    } 

    public static Object read(String serializeFileName, Context context) 
    { 
    FileInputStream fis = null; 
    ObjectInputStream ois = null; 
    Object readThis = null; 

    try 
    { 
     File file = context.getFileStreamPath(serializeFileName); 
     if(file != null && file.exists()) 
     { 
     fis = context.openFileInput(serializeFileName); 
     ois = new ObjectInputStream(fis); 
     readThis = ois.readObject(); 
     } 
    } 
    catch(Throwable t) 
    { 
     //log it 
    } 
    finally 
    { 
     if(ois != null) 
     { 
     try{ois.close();}catch(Throwable t){} 
     } 
     if(fis != null) 
     { 
     try{fis.close();}catch(Throwable t){} 
     } 
    } 

    return readThis; 
    } 

    public static boolean delete(String serializeFileName, Context context) 
    { 
    boolean deleted = false; 
    try 
    { 
     File file = context.getFileStreamPath(serializeFileName); 
     if(file != null && (file.exists())) 
     { 
     deleted = file.delete(); 
     } 
    } 
    catch(Throwable t) 
    { 
     //log it 
    } 

    return deleted; 
    } 

    public static boolean exist(String serializeFileName, Context context) 
    { 
    boolean exist = false; 
    try 
    { 
     File file = context.getFileStreamPath(serializeFileName); 
     if(file != null && (file.exists())) 
     { 
     exist = true; 
     } 
    } 
    catch(Throwable t) 
    { 
     //log it 
    } 

    return exist; 
    } 
} 

それを読むために、ファイルやread方法で直列化可能なオブジェクトを保存するためにsaveメソッドを使用します。

関連する問題