Java序列化,android

Java序列化,android,java,android,Java,Android,我需要将数据保存到对象中 这是我必须存储数据的对象类: public static class FilterEntity implements Serializable { public int ageFrom; public int ageTo; public String sex; public String status; public void setAgeFrom() { this.ageFrom = ageFrom;

我需要将数据保存到对象中

这是我必须存储数据的对象类:

public static class FilterEntity implements Serializable {
    public int ageFrom;
    public int ageTo;
    public String sex;
    public String status;


    public void setAgeFrom()
    {
        this.ageFrom = ageFrom;
    }

    public void setAgeTo()
    {
        this.ageTo = ageTo;
    }

    public void setSex()
    {
        this.sex = sex;
    }

    public void setStatus()
    {
        this.status = status;
    }

    public Integer getAgeFrom()
    {

        return ageFrom;
    }

    public Integer getAgeTo()
    {
        return ageTo;
    }

    public String getSex()
    {
        return sex;
    }

    public String getStatus()
    {
        return status;
    }



}
它是序列化的正确实现吗

在主活动中,我将数据保存到FilterEntity对象

private FilterEntity filter = new FilterEntity();    
filter.status = valueOf(spStatusForSearch.toString());
filter.sex = valueOf(rgSex);
filter.ageTo = sbAgeHigh.getProgress();
filter.ageFrom = sbAgeLow.getProgress();
我可以这样做吗

如何从第三类访问数据?

不要使用静态

public class FilterEntity implements Serializable {
public int ageFrom;
public int ageTo;
public String sex;
public String status;


public void setAgeFrom()
{
    this.ageFrom = ageFrom;
}

public void setAgeTo()
{
    this.ageTo = ageTo;
}

public void setSex()
{
    this.sex = sex;
}

public void setStatus()
{
    this.status = status;
}

public Integer getAgeFrom()
{

    return ageFrom;
}

public Integer getAgeTo()
{
    return ageTo;
}

public String getSex()
{
    return sex;
}

public String getStatus()
{
    return status;
}
}
为模型类设置值的步骤

FilterEntity filter = new FilterEntity();
filter.setStatus(spStatusForSearch.toString());
filter.setSex(rgSex);
filter.setAgeTo(sbAgeHigh.getProgress());
filter.setAgeFrom(sbAgeLow.getProgress());
并获得价值

String status = filter.getStatus();
String sex = filter.getSex();
String ageTo = filter.getAgeTo();
String ageFrom = filter.getAgeFrom();
公共类过滤性
{
公共信息来源;
公共INTAGETO;
公共性;
公共字符串状态;
公共筛选属性(int-ageFrom、int-ageTo、字符串性别、字符串状态)
{
this.ageFrom=ageFrom;
this.ageTo=ageTo;
这个。性=性;
这个状态=状态;
}
public int getAgeFrom()
{
从中返回;
}
public int getAgeTo()
{
返回到;
}
公共字符串getSex()
{
回归性;
}
公共字符串getStatus()
{
返回状态;
}
}
//在保存数据的主活动文件中
静态ArrayList数据=新的ArrayList();
添加(新筛选属性(sbAgeLow.getProgress()、sbAgeHigh.getProgress()、valueOf(rgSex)、valueOf(spStatusForSearch.toString()));
//您可以像这样添加多个
/*现在,通过intent将此arraylist传递给您的第三个activity,或者将此arraylist设置为静态,以便能够通过类的名称访问此arraylist,例如:MainActivity.data*/
//现在,在需要对象数据的第三个活动中,获取arraylist
ArrayList data=MainActivity.data;
迭代器iterotor=data.Iterator();
while(iterotor.hasNext())
{
FilterEntity FilterEntity=iterotor.next();
//您添加的多个值
字符串状态=filterEntity.getStatus();
字符串sex=filterEntity.getSex();
int ageTo=filterEntity.getAgeTo();
int-ageFrom=filterEntity.getAgeFrom();
}

您的实现是正确的。阅读这篇文章
public  class FilterEntity 
{
    public int ageFrom;
    public int ageTo;
    public String sex;
    public String status;
    public FilterEntity(int ageFrom, int ageTo, String sex,String status) 
    {

        this.ageFrom = ageFrom;
        this.ageTo = ageTo;
        this.sex = sex;
        this.status = status;
    }
    public int getAgeFrom()
    {

        return ageFrom;
    }
    public int getAgeTo()
    {
        return ageTo;
    }
    public String getSex()
    {
        return sex;
    }
    public String getStatus()
    {
        return status;
    }

}

// in your main activity file where your are saving the data

static ArrayList<FilterEntity> data=new ArrayList<FilterEntity>();

data.add(new FilterEntity(sbAgeLow.getProgress(),sbAgeHigh.getProgress(),valueOf(rgSex),valueOf(spStatusForSearch.toString())));

// you can add multiple like this


/*Now pass this arraylist to your third activity through intent or make this arraylist static so will be able to access this arraylist anywhere through the name of the class example: MainActivity.data*/


//Now in the third activity where you want the data of the object , get the arraylist


ArrayList data=MainActivity.data;
Iterator iterotor=data.iterator();
while (iterotor.hasNext())
{
    FilterEntity filterEntity=iterotor.next();
    // the multiple values you add

    String status = filterEntity.getStatus();
    String sex = filterEntity.getSex();
    int ageTo = filterEntity.getAgeTo();
    int ageFrom = filterEntity.getAgeFrom();

}