1

我正在尝试使用Simple 2.7.1序列化android.location.Address对象。

我已经尝试创建一个转换器...

public class AddressConverter implements Converter<Address> {

@Override
public Address read(InputNode node) throws Exception {
...
//Not included because this part works!!!!

}

@Override
public void write(OutputNode node, Address addr) throws Exception {
node.setAttribute("Locale", addr.getLocale().toString().replace("_", "-"));

OutputNode child = null;

if (addr.getMaxAddressLineIndex() > 0) {
    for (int index = 0; index <= addr.getMaxAddressLineIndex(); index++) {
    child = node.getChild("AddressLine");

    child.setAttribute("Index", String.valueOf(index));
    child.setValue(addr.getAddressLine(index));
    }
}

if (addr.getFeatureName() != null && addr.getFeatureName().length() > 0) {
    child = node.getChild("FeatureName");
    child.setValue(addr.getFeatureName());
    child.commit();
}

if (addr.getPremises() != null && addr.getPremises().length() > 0) {
    child = node.getChild("Premises");
    child.setValue(addr.getPremises());
    child.commit();
}

if (addr.getSubThoroughfare() != null && addr.getSubThoroughfare().length() > 0) {
    child = node.getChild("SubThoroughfare");
    child.setValue(addr.getSubThoroughfare());
    child.commit();
}

if (addr.getThoroughfare() != null && addr.getThoroughfare().length() > 0) {
    child = node.getChild("Thoroughfare");
    child.setValue(addr.getThoroughfare());
    child.commit();
}

if (addr.getSubLocality() != null && addr.getSubLocality().length() > 0) {
    child = node.getChild("SubLocality");
    child.setValue(addr.getSubLocality());
    child.commit();
}

if (addr.getLocality() != null && addr.getLocality().length() > 0) {
    child = node.getChild("Locality");
    child.setValue(addr.getLocality());
    child.commit();
}

if (addr.getSubAdminArea() != null && addr.getSubAdminArea().length() > 0) {
    child = node.getChild("SubAdminArea");
    child.setValue(addr.getSubAdminArea());
    child.commit();
}

if (addr.getAdminArea() != null && addr.getAdminArea().length() > 0) {
    child = node.getChild("AdminArea");
    child.setValue(addr.getAdminArea());
    child.commit();
}

if (addr.getPostalCode() != null && addr.getPostalCode().length() > 0) {
    child = node.getChild("PostalCode");
    child.setValue(addr.getPostalCode());
    child.commit();
}

if (addr.getCountryCode() != null && addr.getCountryCode().length() > 0) {
    child = node.getChild("CountryCode");
    child.setValue(addr.getCountryCode());
    child.commit();
}

if (addr.getCountryName() != null && addr.getCountryName().length() > 0) {
    child = node.getChild("CountryName");
    child.setValue(addr.getCountryName());
    child.commit();
}

if (addr.getLatitude() != 0) {
    child = node.getChild("Latitude");
    child.setValue(String.valueOf(addr.getLatitude()));
    child.commit();
}

if (addr.getLongitude() != 0) {
    child = node.getChild("Longitude");
    child.setValue(String.valueOf(addr.getLongitude()));
    child.commit();
}

if (addr.getPhone() != null && addr.getPhone().length() > 0) {
    child = node.getChild("Phone");
    child.setValue(addr.getPhone());
    child.commit();
}

node.commit();
}

@SuppressLint("DefaultLocale")
private Locale getLocale(String strLocale) {
String[] splits = strLocale.split("-", 3);

switch (splits.length) {
case 3:
    return new Locale(splits[0].toLowerCase(), splits[1].toUpperCase(), splits[2].toUpperCase());
case 2:
    return new Locale(splits[0].toLowerCase(), splits[1].toUpperCase());
default:
    return new Locale(splits[0].toLowerCase());
}
}

}

我无法write编写子元素。

有人可以帮帮我吗?!

任何有关如何使用 OutputNode 创建子节点的建议也将不胜感激,正如您所见,我已经尝试过文档,但我无法弄清楚!!!

4

1 回答 1

0

我发现这确实序列化了android.location.Address对象,但它改变了对象在 XML 中的放置顺序!!!

如果有人试图解决这个问题,我很抱歉,我只能为浪费您的时间道歉!!!

于 2013-09-18T17:15:02.110 回答