如何将自定义对象保存到Parse cloud Android

时间:2015-02-17 06:27:39

标签: java android parse-platform

现在我正在尝试将自定义对象保存到解析云中。我跟着这个:

https://parse.com/docs/android_guide#objects

http://blog.parse.com/2013/05/30/parse-on-android-just-got-classier/

不幸的是,我收到了这个错误:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spicycurryman.hitup/com.spicycurryman.hitup.MainActivity}: java.lang.IllegalArgumentException: invalid type for value: class com.spicycurryman.hitup.model.HitupEvent



  Caused by: java.lang.IllegalArgumentException: invalid type for value: class com.spicycurryman.hitup.model.HitupEvent
            at com.parse.ParseObject.put(ParseObject.java:2746)
            at com.spicycurryman.hitup.MainActivity.addEvent(MainActivity.java:257)
            at com.spicycurryman.hitup.MainActivity.onCreate(MainActivity.java:84)

我猜这是由我创建的自定义对象类引起的:

package com.spicycurryman.hitup.model;

/**
 * Created by Spicycurryman on 12/31/14.
 */

import java.util.ArrayList;

public class HitupEvent {
    private String title, thumbnailUrl;
    private int year;
    private double rating;
    private ArrayList<String> genre;

    public HitupEvent() {
    }

    public HitupEvent(String name, String thumbnailUrl, int year, double rating,
                 ArrayList<String> genre) {
        this.title = name;
        this.thumbnailUrl = thumbnailUrl;
        this.year = year;
        this.rating = rating;
        this.genre = genre;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String name) {
        this.title = name;
    }

    public String getThumbnailUrl() {
        return thumbnailUrl;
    }

    public void setThumbnailUrl(String thumbnailUrl) {
        this.thumbnailUrl = thumbnailUrl;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public double getRating() {
        return rating;
    }

    public void setRating(double rating) {
        this.rating = rating;
    }

    public ArrayList<String> getGenre() {
        return genre;
    }

    public void setGenre(ArrayList<String> genre) {
        this.genre = genre;
    }

}

我正在存储自定义对象:

   HitupEvent hitupEvent = new HitupEvent();




        hitupEvent.setTitle("Rohit "+ "wants to "+WhatEvent_String_main+ " at "+ WhereEvent_String_main +" on "+ WhenEventDate_String_main +" ,"+ WhenEventTime_String_main   );





        hitupEvent.setThumbnailUrl(roro_photo);

        hitupEvent.setGenre(singleAddress);


        hitupEventList.add(hitupEvent);

        adapter.notifyDataSetChanged();


        ParseObject event = new ParseObject("event");
        event.put("newHitupEvent", hitupEvent);


        event.saveInBackground();
        event.pinInBackground();

存储自定义对象的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

如果您希望以这种方式使用它,您应该创建HitUpEvent子类ParseObject,

您可以查看解析文档:https://parse.com/docs/android_guide#subclasses以了解有关此内容的更多信息。

对ParseObject进行子类化

  
    

创建ParseObject子类:

         

声明扩展ParseObject的子类。添加@ParseClassName注释。它的值应该是你传入的字符串     ParseObject构造函数,并使所有未来的类名引用     不必要。

  
     

确保您的子类具有公共默认值(即零参数)   构造函数。您不能修改此中的任何ParseObject字段   构造函数。

     在调用Parse.initialize()之前,在您的Application构造函数中使用CallParseObject.registerSubclass(YourClass.class)。

另一种选择是使用通用的ParseObject并执行以下操作:

event.put("genre", "hip-hop");
event.put("title", "tigga");