[英]How can Serializable.class not be assignable from Class.class?
[英]How can I grab an item from a collection in a serializable class?
我有这样的东西(非常简化的版本):
public class TransferData implements Serializable{
private Collection[] collections;
private String ID;
public TransferData( Collection[] collections){
this.ID = ID;
this.collections = collections;
}
public String getID(){
return ID;
}
public Collection[] getCollections(){
return collections;
}
}
这通常是我抓取物品的方式:
//Save object in db
ContentValues values = new ContentValues();
values.put("id", td.getID());
但是,我在理解如何从可序列化类的集合/数组中获取项目时遇到麻烦?
这没有道理:
ContentValues values = new ContentValues();
values.put("collectionitem1", td.getCollections()); ??? //need to index the array, how?
我尝试过这样的事情:
for (int i=0; i <= td.getCollections().length; i++) {
System.out.println(i);
}
但是奇怪的是,它只给我3个索引,而不是我数组中的4个索引,但这对我没有帮助。 另外,我的数组包含字符串和整数,因此可能很难通过foreach样式循环进行索引。
弄清楚了! 我知道这是关于错误地设置遍历数组的事情。 解决方法如下:
//Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
for (Collection collection : td.getCollections()) {
values.put("reminders", collection.getNumberOfReminders());
}
我看到您正在使用SQLite。 在我看来,您要解决所有这些错误。 似乎您需要使用实体类:
/**
*
* @author kkennedy
*/
@Entity(name = "TransferData")
@Table(name = "TransferData")
public class TransferData implements Serializable {
@Id
@Basic(optional = false)
@Column(name = "ID", nullable = false, length = 8)
private String ID;
@Basic(optional = false)
@Column(name = "data", nullable = false, length = 8)
private String data;
/**
* Default Constructor
*/
public TransferData() {
}
/**
* Getter
*
* @return
*/
public String getID() {
return ID;
}
/**
* Setter
*
* @param ID
*/
public void setID(String ID) {
this.ID = ID;
}
/**
* Getter
*
* @return
*/
public String getData() {
return data;
}
/**
* Setter
*
* @param data
*/
public void setData(String data) {
this.data = data;
}
}
我确定这不是您实际使用的,但是通过使用实体类,JDBC驱动程序完成了将数据实际放入数据库和从数据库移出的所有工作。 您只需将其输入到类中,然后根据需要持久化并查询数据库。
不确定这是否是最好的起点,但请尝试此处以获取更多信息: http : //docs.oracle.com/javaee/5/tutorial/doc/bnbqw.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.