【问题标题】:How can I grab an item from a collection in a serializable class?如何从可序列化类的集合中获取项目?
【发布时间】:2014-10-20 17:26:02
【问题描述】:

我有这样的东西(非常简化的版本):

 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 样式循环进行索引。

【问题讨论】:

  • 你应该解释values是什么类型,没人知道你的上下文
  • 抱歉,我的意思是:ContentValues values = new ContentValues();

标签: java arrays sqlite serialization


【解决方案1】:

想通了!我知道这是关于设置不正确地通过数组的问题。这是解决方案:

//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());
                        }

【讨论】:

    【解决方案2】:

    我看到您正在使用 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

    【讨论】:

    • 好吧,我现在的做法是,ID 和其他常规字符串未包含在此示例中。只是数组没有。可能是我没有正确设置数组
    • Btwm getData 不是一个简单的字符串。这是一个有其他领域的类
    猜你喜欢
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多