且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何缓存/保存自定义类对象的Andr​​oid?

更新时间:2022-11-20 13:25:34

例如。

 公共类MyClass的实现Serializable
{
    私有静态最后长的serialVersionUID = 1L;

    公共字符串称号;
    公共字符串的startTime;
    公共字符串endTime的;
    公共字符串天;

    公共布尔classEnabled;


    公共MyClass的(标题字符串,字符串的startTime,布尔启用){
        this.title =称号;
        this.startTime = startTime时;
        this.classEnabled =启用;
    }

    公共布尔saveObject(MyClass的OBJ){
        最终文件suspend_f =新的文件(SerializationTest.cacheDir,测试);

        FileOutputStream中FOS = NULL;
        ObjectOutputStream的OOS = NULL;
        布尔保持= TRUE;

        尝试 {
            FOS =新的FileOutputStream(suspend_f);
            OOS =新的ObjectOutputStream(FOS);
            oos.writeObject(OBJ);
        }赶上(例外五){
            保持= FALSE;
        } 最后 {
            尝试 {
                如果(!OOS = NULL)oos.close();
                如果(!FOS = NULL)fos.close();
                如果(保持==假)suspend_f.delete();
        }赶上(例外五){/ *做什么* /}
        }

        返回保持;
    }

    公共MyClass的的getObject(上下文C){
        最终文件suspend_f =新的文件(SerializationTest.cacheDir,测试);

        MyClass的simpleClass = NULL;
        的FileInputStream FIS = NULL;
        ObjectInputStream的是= NULL;

        尝试 {
            FIS =新的FileInputStream(suspend_f);
            是=新的ObjectInputStream(FIS);
            simpleClass =(MyClass的)is.readObject();
        }赶上(例外五){
            串VAL = e.getMessage();
        } 最后 {
            尝试 {
                如果(!FIS = NULL)fis.close();
                如果(是!= NULL)is.close();
            }赶上(例外五){}
        }

        返回simpleClass;
    }
 

和从活动的要求

if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))     cacheDir =新的文件(android.os.Environment.getExternalStorageDirectory(),MyCustomObject); 其他     cacheDir = getCacheDir(); 如果(!cacheDir.exists())     cacheDir.mkdirs(); MyClass的M =新MyClass的(乌默尔,阿西,真正的); 布尔结果= m.saveObject(M); 如果(结果)     Toast.makeText(这一点,拯救​​对象,Toast.LENGTH_LONG).show(); 其他     Toast.makeText(这一点,错误保存对象,Toast.LENGTH_LONG).show(); MyClass的M =新MyClass的(); MyClass的C = m.getObject(本); 如果(C!= NULL)     Toast.makeText(这一点,检索的对象,Toast.LENGTH_LONG).show(); 其他     Toast.makeText(这一点,错误检索对象,Toast.LENGTH_LONG).show();

不要忘记使用WRITE_EXTERNAL_STORAG​​E权限清单文件。

I want to save ArrayList<CustomClass>-object to somewhere in Android storage to retrieve quickly and display data in it.

Is this possible or not? If yes, then which technique will be suitable, SQLite or external storage?

example.

public class MyClass implements Serializable 
{
    private static final long serialVersionUID = 1L;

    public String title;
    public String startTime;
    public String endTime;
    public String day;

    public boolean classEnabled;


    public MyClass(String title, String startTime, boolean enable) {
        this.title = title;
        this.startTime = startTime;
        this.classEnabled = enable;
    }

    public boolean saveObject(MyClass obj) {   
        final File suspend_f=new File(SerializationTest.cacheDir, "test");

        FileOutputStream   fos  = null;
        ObjectOutputStream oos  = null;
        boolean            keep = true;

        try {
            fos = new FileOutputStream(suspend_f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
        } catch (Exception e) {
            keep = false;
        } finally {
            try {
                if (oos != null)   oos.close();
                if (fos != null)   fos.close();
                if (keep == false) suspend_f.delete();
        } catch (Exception e) { /* do nothing */ }
        }

        return keep;
    }

    public MyClass getObject(Context c) {
        final File suspend_f=new File(SerializationTest.cacheDir, "test");

        MyClass simpleClass= null;
        FileInputStream fis = null;
        ObjectInputStream is = null;

        try {
            fis = new FileInputStream(suspend_f);
            is = new ObjectInputStream(fis);
            simpleClass = (MyClass) is.readObject();
        } catch(Exception e) {
            String val= e.getMessage();
        } finally {
            try {
                if (fis != null)   fis.close();
                if (is != null)   is.close();
            } catch (Exception e) { }
        }

        return simpleClass;  
    }

and calling from activity

if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"MyCustomObject");
else
    cacheDir= getCacheDir();
if(!cacheDir.exists())
    cacheDir.mkdirs();

MyClass m = new MyClass("umer", "asif", true);
boolean result = m.saveObject(m);

if(result)
    Toast.makeText(this, "Saved object", Toast.LENGTH_LONG).show();
else
    Toast.makeText(this, "Error saving object", Toast.LENGTH_LONG).show();   

MyClass m = new MyClass();
MyClass c = m.getObject(this);

if(c!= null)
    Toast.makeText(this, "Retrieved object", Toast.LENGTH_LONG).show();
else
    Toast.makeText(this, "Error retrieving object", Toast.LENGTH_LONG).show();

dont forget to use write_external_storage permissions in manifest file.