简体   繁体   English

getParseFile(string key)从带有扩展ParseObject类的Parse.com返回null

[英]getParseFile(string key) returning null from Parse.com with extended ParseObject class

I have an extended ParseObject class with 6 fields. 我有6个字段的扩展ParseObject类。 All of the fields return the correct data except for the ParseFile getter method. 除ParseFile getter方法外,所有字段均返回正确的数据。 Getting ParseFiles the old fashioned way works but for some reason when I use an extended class, the data is null when GetDataCallback is called. 以旧的方式获取ParseFiles是可行的,但是由于某种原因,当我使用扩展类时,调用GetDataCallback时数据为null。 It has an URL and image name but the data field is null. 它具有URL和图像名称,但数据字段为null。

My extended class: 我的延伸课程:

package [mypackage];

import com.parse.ParseClassName;
import com.parse.ParseFile;
import com.parse.ParseObject;
import java.io.Serializable;

@ParseClassName("Listing")
public class Listing extends ParseObject implements Serializable {
    private boolean  active;
    private String description, title, username;
    private int price;
    private ParseFile file;

    public Listing() {
        super();
    }

    public void setDetail(boolean active, String description, String title,
                          String username, int price, ParseFile file) {
        this.active = active;
        this.description = description;
        this.price = price;
        this.title = title;
        this.username = username;
        this.file = file;
    }

    /* getter methods */
    public boolean getIsActive() {
        return getBoolean("active");
    }

    public String getDescription() {
        return getString("description");
    }

    public int getPrice() {
        return getInt("price");
    }

    public String getListingTitle() { /* getTitle() reserved by android */
        return getString("title");
    }

    public String getUsername() {
        return getString("username");
    }

    public ParseFile getFile() {
        return getParseFile("image");
    }
}

Where the getter methods are called: 调用getter方法的位置:

public void getListings() {
        ParseQuery<Listing> query = ParseQuery.getQuery(Listing.class);
        /* only retrieve active listings */
        query.whereEqualTo("active", true);
        query.findInBackground(new FindCallback<Listing>() {

            @Override
            //public void done(List<ParseObject> listingList, ParseException e) {
            public void done(List<Listing> listingList, ParseException e) {

                if (e == null) {
                    Log.d("listing", "Retrieved " + listingList.size() + " listings");
                    /* clear adapter before populating */
                    adapter.clear();
                    /* iterate through listings and create listing objects */
                    for (Listing listingObject : listingList) {
                        boolean active;
                        String description, title, username;
                        int price;

                        active = listingObject.getIsActive();
                        username = listingObject.getUsername();
                        description = listingObject.getDescription();
                        title = listingObject.getListingTitle();
                        price = listingObject.getPrice();
                        file =  listingObject.getFile();

                        /* create a listing object to be added to a ListView */    
                        Listing listing = new Listing();
                        listing.setDetail(active, description, title, username, price, file);
                        listings.add(listing);

                    } /* end for loop */
                }
                else {
                    Log.d("listing", "Error: " + e.getMessage());
                }
            }
        });
    }

In the adapter: 在适配器中:

public View getView(int position, View convertView, ViewGroup parent){
        if(convertView == null){
            LayoutInflater mLayoutInflater = LayoutInflater.from(context);
            convertView = mLayoutInflater.inflate(R.layout.listing_row_item, null);
        }

        Listing listing = listings.get(position);
        TextView titleView = (TextView) convertView.findViewById(R.id.listing_title);
        TextView priceView = (TextView) convertView.findViewById(R.id.listing_price);
        final ParseImageView imageView = (ParseImageView) convertView.findViewById(R.id.ivPicture);

        titleView.setText(listing.getListingTitle());
        priceView.setText("$" + String.valueOf(listing.getPrice()));

        listing.getFile().getDataInBackground(new GetDataCallback() { //getFile() returns null

            public void done(byte[] data, ParseException e) {

EDIT: I believe my fundamental misunderstanding was how to set the values of the extended ParseObject. 编辑:我相信我的基本误解是如何设置扩展ParseObject的值。 As the answer below shows, the put Parse methods here actually put the values in the object. 如下面的答案所示,此处的put Parse方法实际上将值放入对象中。 I was under the impression that put was only ever for actual database operations and my setter method was therefore not setting up the ParseObject correctly. 我的印象是put仅用于实际的数据库操作,因此我的setter方法未正确设置ParseObject。

Uggh... We were so close. 嗯...我们好近。 I found the problem, you werent setting the values to the fields in your ParseObject, which is why everything was null in the adapter. 我发现了问题,您没有将值设置为ParseObject中的字段,这就是适配器中所有内容均为空的原因。 Add the following to your Listing class and change the setDetail method as follows: 将以下内容添加到您的Listing类中,并如下更改setDetail方法:

public void setDetail(boolean active, String description, String title,
                      String username, int price, ParseFile file) {

    setIsActive(active);
    setIsActive(description);
    setPrice(price);
    setListingTitle(title);
    setUsername(username);
    setFile(file);
}

public void setIsActive(boolean a) {
    put("active", a);
}

public void setDescription(String s) {
    put("description", s);
}

public void setPrice(int p) {
    put("price", p);
}

public void setListingTitle(String t) {
    put("title", t);
}

public void setUsername(String u) {
    put("username", u);
}

public void setFile(ParseFile f) {
    put("image", f);
}

Old Answer 旧答案

I maybe wrong but I'm pretty sure that you're supposed to save the URL of the ParseFile and not the ParseFile itself and use the URL to fetch the file. 我可能错了,但我很确定您应该保存ParseFile的URL,而不是ParseFile本身,并使用URL来获取文件。 So in the done (..) method you should do something like: 因此,在完成(..)方法中,您应该执行以下操作:

file.getUrl();

And set it as a String. 并将其设置为字符串。 Moving on, if 继续前进,如果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM