大家好,我已经构建了一个实现 Parcelable 的类,但是当我读取该类时,我定义的 arraylist 属性之一变为空。这是代码
package roblestech.laCartelera;
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class ProgramacionPelicula implements Parcelable {
public ProgramacionPelicula() {
}
public ProgramacionPelicula(Pelicula pelicula) {
_pelicula = pelicula;
}
public ProgramacionPelicula(Cine cine) {
_cine = cine;
}
public String toString() {
if (getVista() == ProgramacionPelicula.VISTA_PELICULA) {
return getCine().getCine();
} else {
return getPelicula().getTituloOriginal();
}
}
private int _idProgramacion;
public void setIdProgramacion(int value) {
_idProgramacion = value;
}
public int getIdProgramacion() {
return _idProgramacion;
}
private Pelicula _pelicula;
// public ArrayList<Pelicula> _peliculas = new ArrayList<Pelicula>();
public void setPelicula(Pelicula pelicula) {
_pelicula = pelicula;
}
public Pelicula getPelicula() {
return _pelicula;
}
private Cine _cine;
public void setCine(Cine cine) {
_cine = cine;
}
public Cine getCine() {
return _cine;
}
public ArrayList<Tanda> _tandas = new ArrayList<Tanda>();
public void setTandas(ArrayList<Tanda> value) {
_tandas = value;
}
public void setTandas(Object[] tandas) {
for (Object tanda : tandas) {
if (tanda instanceof Tanda) {
_tandas.add((Tanda) tanda);
}
}
}
public void addTanda(Tanda value) {
_tandas.add(value);
}
public ArrayList<Tanda> getTandas() {
return _tandas;
}
private String _sala = "";
public void setSala(String value) {
_sala = value;
}
public String getSala() {
return _sala;
}
public static final int VISTA_CINE = 0;
public static final int VISTA_PELICULA = 1;
private int _vista = VISTA_CINE;
public int getVista() {
return _vista;
}
public ProgramacionPelicula toPelicula() {
ProgramacionPelicula programacionPelicula = new ProgramacionPelicula();
programacionPelicula._idProgramacion = _idProgramacion;
programacionPelicula._pelicula = _pelicula;
programacionPelicula._cine = _cine;
programacionPelicula._tandas = _tandas;
programacionPelicula._sala = _sala;
programacionPelicula._vista = VISTA_PELICULA;
return programacionPelicula;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(getIdProgramacion());
dest.writeString(getSala());
ArrayList<Pelicula> peliculas = new ArrayList<Pelicula>();
peliculas.add(getPelicula());
Object[] objectsPeliculas = peliculas.toArray();
dest.writeArray(objectsPeliculas);
Object[] objectsTanda = getTandas().toArray();
dest.writeArray(objectsTanda);
}
// this is used to regenerate your object. All Parcelables must have a
// CREATOR that implements these two methods
public static final Parcelable.Creator<ProgramacionPelicula> CREATOR = new Parcelable.Creator<ProgramacionPelicula>() {
public ProgramacionPelicula createFromParcel(Parcel in) {
return new ProgramacionPelicula(in);
}
public ProgramacionPelicula[] newArray(int size) {
return new ProgramacionPelicula[size];
}
};
// example constructor that takes a Parcel and gives you an object populated
// with it's values
private ProgramacionPelicula(Parcel in) {
this();
setIdProgramacion(in.readInt());
setSala(in.readString());
Object[] obj = in.readArray(Pelicula.class.getClassLoader());
setPelicula((Pelicula) obj[0]);
setTandas(in.readArray(Tanda.class.getClassLoader()));
}
}
提前谢谢大家。
最佳答案
另一种传递数组的方法是使用类型化数组,如下所示:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mMyArray.length);
dest.writeTypedArray(mMyArray, flags);
}
然后像这样读回去:
public MyParcelableObject(Parcel in) {
mMyArray = new MyOtherParcelableObject[in.readInt()];
in.readTypedArray(mMyArray, MyOtherParcelableObject.CREATOR);
}
简而言之,在写入其值之前,将数组的长度作为 int 值写入 parcelable,这样您就可以在读取它之前创建一个相同大小的数组。
关于Android Parcelable 数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7166770/
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat