我有一个需要在 2 之间传递的对象Activity
。该对象具有 HashTable、String[] 等。无法弄清楚如何使其成为 Parcalebale,以便我可以填充对象的数组并将单个 obj 或数组传递给其他 Activity。我的课是:
public class TrainRoute { // implements Parcelable
private String RouteName;
private int RouteIndex;
private Context ctx;
private Hashtable<String, String> stationsList;
private String[] stationsNames;
private String[] trainTimings;
private boolean hasSpecialMessages;
private Hashtable<String, String> specialMessages;
private String firstStation;
private String CURRENT_ROUTE_PATH;
public TrainRoute(Context ctx, String routeName, int routeIndex, String routeUpOrDownPath, boolean hasSpecialMessages) {
this.ctx = ctx;
this.RouteName = routeName;
this.RouteIndex = routeIndex;
this.CURRENT_ROUTE_PATH = routeUpOrDownPath;
this.hasSpecialMessages = hasSpecialMessages;
}
public String getName() { return RouteName; }
public int getIndex() { return RouteIndex; }
public String[] getStationsNames()
{
if(this.stationsList == null)
{
String file = this.CURRENT_ROUTE_PATH + "/" + CONSTANTS.SUBPATH_STATIONS + "/" + this.RouteIndex + ".txt";
String[][] tempList = ResourceReader.GetValues(ctx, file, 2);
this.stationsList = new Hashtable<String, String>(tempList.length);
this.stationsNames = new String[tempList.length];
for(int i=0;i<tempList.length;i++)
{
this.stationsList.put(tempList[i][0], tempList[i][1] );
this.stationsNames[i] = tempList[i][0];
}
this.firstStation = this.stationsNames[0];
}
return stationsNames;
}
public String[] getTimings(String stationName)
{
if(this.trainTimings == null)
{
String file = this.CURRENT_ROUTE_PATH + "/" + CONSTANTS.SUBPATH_TIMINGS + "/" + this.RouteIndex + ".txt";
this.trainTimings = ResourceReader.GetLines(ctx, file);
for(int i=0;i<this.trainTimings.length;i++)
{
this.trainTimings[i] = this.trainTimings[i].substring(0, 2) + ":" + this.trainTimings[i].substring(2, 4);
}
}
int minutesFromOrigin =
Integer.parseInt(this.stationsList.get(stationName).toString())
- Integer.parseInt(this.stationsList.get(this.firstStation).toString());
String[] trainTimingsForStation = new String[this.trainTimings.length];
for (int i = 0; i < trainTimingsForStation.length; i++) {
trainTimingsForStation[i] = AddMinutes(this.trainTimings[i], minutesFromOrigin);
}
return trainTimingsForStation;
}
public void loadSpecialMessages()
{
if(this.hasSpecialMessages == true && this.specialMessages == null)
{
this.specialMessages = new Hashtable<String, String>();
String specialMessagesFilePath = this.CURRENT_ROUTE_PATH + "/" + CONSTANTS.SPECIALMESSAGES_FOLDER_NAME + "/" + this.RouteIndex + ".txt";
String[][] tempSpecialMessages = ResourceReader.GetValues(ctx, specialMessagesFilePath, 2);
for (int i = 0; i < tempSpecialMessages.length; i++)
{
tempSpecialMessages[i][1] = tempSpecialMessages[i][1].replaceAll("\\n", "\n"); // StringFunctions.replace(tempSpecialMessages[i][1], "\\n", "\n");
tempSpecialMessages[i][0] = tempSpecialMessages[i][0].substring(0, 2) + ":" + tempSpecialMessages[i][0].substring(2, 4);;
this.specialMessages.put(tempSpecialMessages[i][0], tempSpecialMessages[i][1]);
}
}
}
public boolean hasSpecialMessage(int trainIndex)
{
if(this.hasSpecialMessages == true)
{
return this.specialMessages.containsKey(this.trainTimings[trainIndex - 1]);
}
else
return false;
}
public String getSpecialMessage(int trainIndex)
{
return this.specialMessages.get((this.trainTimings[trainIndex - 1]).toString()).toString();
}
在实现 Parcelable 时,我不知道如何在实现的函数中添加对象字段。
由于有这么多的 HashTable、String[],在这种情况下实施 Serialiazable 是否比 Parcelable 更可取?如果添加 Serializable 那么如何实现相同的 ts 方法。
编辑: 在上面的 obj 中,我添加了一个方法:
public Hashtable<String, String> getAllSpecialMessages() {
return this.specialMessages;
}
我试图将这个 HashTable 从我的 Activity 传递给另一个 Activity :
intent.putExtra("specialMsgs", curTrainRoute.getAllSpecialMessages());
// OTHER ACTIVTY
Serializable data = sender.getSerializableExtra("specialMsgs");
if (data != null) {
spMsgs = new Hashtable<String, String>((HashMap<String, String>)data);
Log.i(TAG, " GOT spMsgs : Len = " + spMsgs.size());
if (spMsgs.size() > 0) {
java.util.Enumeration<String> keys = spMsgs.keys();
while(keys.hasMoreElements()) {
Log.i(TAG, "SP= " + spMsgs.get(keys));
}
}
}
在这里,我得到“GOT spMsgs:Len = 4。但在 Log SP= 中,我得到无限空值。为什么找不到通过的 Hashtable 的输入?我还缺少什么?
自过去 2 天以来,我一直在寻找有关此问题的一些指导,但无法弄清楚如何处理此对象。
非常感谢任何帮助。