我使用名为GameSettings
的自定义parcelable对象在Android应用程序(使用MonoDroid开发)中的Activites之间传递了许多设置。这些设置存储在此GameSettings
课程的属性中,到目前为止,它们都是简单的整数,我可以使用Parcel.WriteInt()
和{{1 }}
我刚刚向名为Parcel.ReadInt()
GameSettings
的{{1}}添加了一个新属性,其类型为CelebrityNames
,我试图以同样的方式传递此属性,但是当调用List<string>
时,属性会被填充一个空列表(尽管使用ReadStringList()
之前将非空列表写入包之前)。该地块正在从WriteStringList()
传递到NameEntryActivity
。
GameRoundActivity
GameSettings.cs
注意:我使用支持变量using System;
using System.Collections.Generic;
using Android.OS;
using Java.Interop;
using Object = Java.Lang.Object;
namespace Celebrities
{
public class GameSettings : Object, IParcelable
{
private static readonly GenericParcelableCreator<GameSettings> _creator
= new GenericParcelableCreator<GameSettings>((parcel) => new GameSettings(parcel));
[ExportField("CREATOR")]
public static GenericParcelableCreator<GameSettings> InitializeCreator()
{
return _creator;
}
public int NumberOfPlayers { get; set; }
public int NumberOfTeams { get; set; }
public int CelebritiesPerPlayer { get; set; }
public int SecondsPerRound { get; set; }
private List<string> _celebrityNames;
public List<string> CelebrityNames {
get
{
_celebrityNames.Shuffle ();
return _celebrityNames;
}
set
{
_celebrityNames = value;
}
}
public GameSettings (int players, int teams, int celebrities, int secondsPerRound)
{
NumberOfPlayers = players;
NumberOfTeams = teams;
CelebritiesPerPlayer = celebrities;
SecondsPerRound = secondsPerRound;
}
private GameSettings(Parcel parcel) : this(parcel.ReadInt (), parcel.ReadInt (), parcel.ReadInt (), parcel.ReadInt ())
{
if (_celebrityNames == null)
{
_celebrityNames = new List<string>();
}
parcel.ReadStringList (_celebrityNames);
}
public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
{
dest.WriteInt (NumberOfPlayers);
dest.WriteInt (NumberOfTeams);
dest.WriteInt (CelebritiesPerPlayer);
dest.WriteInt (SecondsPerRound);
dest.WriteStringList (_celebrityNames);
}
public int DescribeContents()
{
return 0;
}
}
}
进行分区,因为我有一个自定义的getter来清理列表,此时此刻并不是必需的。无论使用属性还是变量,问题都是一样的。
_celebrityNames
GenericParcelableCreator.cs
我包含以下活动类中的相关代码(为简洁起见,这些不是完整的文件,请询问您是否认为查看其余内容也会有所帮助。)
using System;
using Android.OS;
using Object = Java.Lang.Object;
namespace Celebrities
{
public sealed class GenericParcelableCreator<T> : Object, IParcelableCreator
where T : Object, new()
{
private readonly Func<Parcel, T> _createFunc;
public GenericParcelableCreator(Func<Parcel, T> createFromParcelFunc)
{
_createFunc = createFromParcelFunc;
}
public Object CreateFromParcel(Parcel source)
{
return _createFunc(source);
}
public Object[] NewArray(int size)
{
return new T[size];
}
}
}
(我从那里传递包裹)
NameEntryActivity.cs
public class NameEntryActivity : Activity
{
...
private GameSettings _gameSettings;
private List<string> _celebrityNames;
protected override void OnCreate (Bundle savedInstanceState)
{
...
_gameSettings = (Intent.Extras.GetParcelable ("GameSettings") as GameSettings);
_celebrityNames = new List<string> ();
...
}
...
private void MoveToNextCelebrity()
{
...
_gameSettings.CelebrityNames = _celebrityNames;
var intent = new Intent (this, typeof(GameRoundActivity));
intent.PutExtra("GameSettings", _gameSettings);
StartActivity (intent);
...
}
}
(我将包裹传递到的地方)
GameRoundActivity.cs
这是我第一次开发Android应用程序,所以很可能我在实现parcelling框架时遇到了错误或者误解了它。同样,我一直在看这段代码,以至于我可能只是错过了一个更普遍的愚蠢错误:)
提前致谢!
答案 0 :(得分:0)
我切换到使用字符串数组而不是列表,现在它正在使用Parcel.WriteStringArray()
和Parcel.CreateStringArray()
。
显然这并不适用于所有情况,但我仍然对为什么会发生这种情况感兴趣!