从ArrayList <object>保存并获取数据

时间:2015-07-20 08:12:41

标签: java android

我正在使用活动来显示联系人列表,在此活动中我有一个按钮来添加另一个联系人。我可以添加一个新的联系人并在列表视图中显示它。但是,如果我退出应用程序或按BACK按钮,所有联系人都将消失。

我认为在尝试从内部存储中获取数据时遇到问题。我可以保存数据,我可以在模拟器中看到CONTACT_LIST的文件。但我无法从中读取数据。我将“SavingFuction”放在“OnRause”中的“OnPause”和“ReadingFunction”中。我使用下面的代码片段来保存数据:

对于内部存储:

onPause,onResume

@Override
protected void onPause()
{
    super.onPause();
    SavePreferences(arrContacts);
}

@Override
protected void onResume()
{
    super.onResume();
    getContact(arrContacts);
}

-Saving:

public void SavePreferences(ArrayList<Contacts> ct)
{
    try
    {
        FileOutputStream fOS = openFileOutput("CONTACT_LISTs", MODE_PRIVATE);
        ObjectOutputStream oOS = new ObjectOutputStream(fOS);
        oOS.writeObject(ct);
        oOS.flush();
        oOS.close();
        fOS.close();
    }
    catch(Exception e)
    {
        Log.e("InternalStorage", e.getMessage());
    }
}

-Reading

public void getContact(ArrayList<Contacts> ct)
{
    try
    {
        FileInputStream fIS = openFileInput("CONTACT_LISTs");
        ObjectInputStream oi = new ObjectInputStream(fIS);
        ct = (ArrayList<Contacts>) oi.readObject();
        oi.close();
        fIS.close();
    } catch (FileNotFoundException e) {
        Log.e("InternalStorage", e.getMessage());       
    } catch (IOException e) {
        Log.e("InternalStorage", e.getMessage()); 
    } catch (ClassNotFoundException e) {
        Log.e("InternalStorage", e.getMessage()); 
    }
}

我怎样才能做对?在关闭应用程序并重新打开它之后,一切都消失了。

这是我的Contacts.java

    public class Contacts implements Serializable
    {


    private static final long serialVersionUID = 1L;
        private ArrayList<Contacts>contactList=new ArrayList<Contacts>();
        String name;
        String number;
        String address;

    public Contacts(String name, String num, String add)
    {
        this.name = name;
        this.number = num;
        this.address = add;
    }

    public Contacts() {

    }

    public void addContact(Contacts ct)
    {
        int i=0;
        for(; i < contactList.size(); i++)
        {
            Contacts nvOld=contactList.get(i);
            if(nvOld.getNum().trim().equalsIgnoreCase(ct.getNum().trim()))
            {
                break;
            }
        }
        if(i < contactList.size())
            contactList.set(i, ct);
        else
            contactList.add(ct);
    }

    String getName()
    {
        return name;
    }
    public void setName(String na)
    {
        this.name = na;
    }

    String getNum()
    {
        return number;
    }
    public void setNum(String nu)
    {
        this.number = nu;
    }

    String getAdd()
    {
        return address;
    }
    public void setAdd(String ad)
    {
        this.address = ad;
    }
}

4 个答案:

答案 0 :(得分:0)

使用流不仅可以捕获异常,还可以关闭流。使用以下结构是一种很好的做法:

try {
    //read from stream
} catch (Exception e) {
    //work with Exceptions
} finally {
    //close all streams even when exception was called
}

为什么不在阅读块中关闭fIS?

答案 1 :(得分:0)

1.确保数据对象是实现Serializable;

2.确保数据保存正常(您可以在探索中找到此文件,注意文件大小和时间);

3.确保读取数据正常;

最后:

我检查这个源代码,Contacts不是实现Serializable。 这就是你要找的原因;

您可以为存储数据实现自定义类。

答案 2 :(得分:0)

因为我认为改变了

Rename
You must type a file name.
[OK]

到那个

@Override
protected void onPause()
{
    super.onPause();
    SavePreferences(arrContacts);
}

@Override
protected void onResume()
{
    super.onResume();
    getContact(arrContacts);
}

public void getContact(ArrayList<Contacts> ct)
{ 
    try 
    { 
        FileInputStream fIS = openFileInput("CONTACT_LISTs");
        ObjectInputStream oi = new ObjectInputStream(fIS);
        ct = (ArrayList<Contacts>) oi.readObject();
        oi.close();
        fIS.close();
    } catch (FileNotFoundException e) {
        Log.e("InternalStorage", e.getMessage());       
    } catch (IOException e) {
        Log.e("InternalStorage", e.getMessage()); 
    } catch (ClassNotFoundException e) {
        Log.e("InternalStorage", e.getMessage()); 
    } 
} 

另一次更新

@Override
protected void onPause()
{
    SavePreferences(arrContacts);
    super.onPause();
}

@Override
protected void onResume()
{
    super.onResume();
    arrContacts = getContact();
}

public ArrayList<Contacts> getContact()
{ 
    ArrayList<Contacts> ct = new ArrayList<Contacts>();
    try 
    { 
        FileInputStream fIS = openFileInput("CONTACT_LISTs");
        ObjectInputStream oi = new ObjectInputStream(fIS);
        ct = (ArrayList<Contacts>) oi.readObject();
        oi.close();
        fIS.close();
    } catch (FileNotFoundException e) {
        Log.e("InternalStorage", e.getMessage());       
    } catch (IOException e) {
        Log.e("InternalStorage", e.getMessage()); 
    } catch (ClassNotFoundException e) {
        Log.e("InternalStorage", e.getMessage()); 
    } 
    return ct;
} 

答案 3 :(得分:0)

    public ArrayList<Contacts> getContact()
{
    ArrayList<Contacts> ct = null;
    try
    {
        FileInputStream fIS = openFileInput("CONTACT_LISTs");
        ObjectInputStream oi = new ObjectInputStream(fIS);
        ct = (ArrayList<Contacts>) oi.readObject();
        oi.close();
        fIS.close();
    } catch (FileNotFoundException e) {
        Log.e("InternalStorage", e.getMessage());       
    } catch (IOException e) {
        Log.e("InternalStorage", e.getMessage()); 
    } catch (ClassNotFoundException e) {
        Log.e("InternalStorage", e.getMessage()); 
    }

    return ct;
}   

我测试它,它的工作正常。