如何从列表<object>中读取并将其保存为字符串?</object>

时间:2014-12-23 12:27:14

标签: java notifications

我有一个列表,我必须从中获取元素到字符串。 我现在一直在网上搜索,但仍然没有运气。

所以我从chapter.getFacts(Language.en)获取我的列表 - 对象看起来像这样(com.xxx.models.Fact@4215fd98,com.xxx.models.Fact@435291e8,com.xxx.models。事实@ 43529318 ...)。该对象的每一个都包含地址,电话号码,网站等。 我应该阅读他们的内容,并将它们保存到一个我可以代表用户的字符串。

你可以帮帮我吗? TNX

public class Fact implements Serializable {

private static final long serialVersionUID = -8839726094517932950L;
public static final String COLUMN_LANGUAGE = "language";
public static final String COLUMN_LOCATION_ID = "locationId";
public static final String COLUMN_GUIDE_ID = "guideId";
public static final String COLUMN_TRIP_ID = "tripId";

/** the locationId it is associated with **/
@DatabaseField(uniqueCombo = true)
private int locationId;

@DatabaseField(uniqueCombo = true)
private int guideId;

@DatabaseField(uniqueCombo = true)
private int tripId;

/** the language of the Guide **/
@DatabaseField(dataType = DataType.ENUM_STRING, uniqueCombo = true)
private Language language;

/** the name of the guide in the specified language **/
@DatabaseField()
private String value;

/** **/
@DatabaseField()
private String type;

/**
 * Actually, this field holds enum {@link com.tripwolf.models.FactType},
 * but ORMLites unknownEnumName mechanism uses reflection each time to detect enum value if it was not found,
 * which adds unacceptable peformance impact,
 * so we need to use custom getter and setter to handle unknown FactTypes
 */
@DatabaseField(uniqueCombo = true)
private int typeId;

/**
 * @return the locationId
 */
public int getLocationId() {
    return locationId;
}

/**
 * @param locationId the locationId to set
 */
public void setLocationId( int locationId ) {
    this.locationId = locationId;
}

/**
 * @return the language
 */
public Language getLanguage() {
    return language;
}

/**
 * @param language the language to set
 */
public void setLanguage( Language language ) {
    this.language = language;
}

/**
 * @return the value
 */
public String getValue() {
    return value;
}

/**
 * @param value the value to set
 */
public void setValue( String value ) {
    this.value = value;
}

/**
 * @return the type
 */
public String getType() {
    return type;
}

public FactType getTypeEnum() {
    return FactType.getType(typeId);
}

/**
 * @param type the type to set
 */
public void setType( String type ) {
    this.type = type;
}

/**
 * @return the typeId
 */
public FactType getTypeId() {
    return FactType.getType(typeId);
}

/**
 * @param typeId the typeId to set
 */
public void setTypeId( FactType typeId ) {
    this.typeId = typeId.ordinal();
}

public int getGuideId() {
    return guideId;
}

public void setGuideId( int guideId ) {
    this.guideId = guideId;
}

public int getTripId() {
    return tripId;
}

public void setTripId( int tripId ) {
    this.tripId = tripId;
}

}

1 个答案:

答案 0 :(得分:1)

在你的事实类中,实现toString方法,当你说如上所述尝试打印时间时调用它,因为它不存在,它调用Object的toString默认实现,你会看到带有哈希码的输出。班级名称。定义toString,如下所示:

class Fact {
    String address;
    String website;
    //other business logic and getter/setter constructor stuff if any
    public String toString() {
        return address + " " + website;
    }
}

执行此操作并打印清单后,您会看到类似以下内容的输出:

[myaddress1 website1, myaddress2 website2]