getParseFile(string key) 使用扩展的 ParseObject class 从 Parse.com 返回 null

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

我有一个带有 6 个字段的扩展 ParseObject class。除了 ParseFile getter 方法之外,所有字段 return 都是正确的数据。以老式方式获取 ParseFiles 是可行的,但由于某种原因,当我使用扩展 class 时,调用 GetDataCallback 时数据为空。它有一个 URL 和图像名称,但数据字段为空。

我的扩展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");
    }
}

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

在适配器中:

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) {

编辑: 我相信我的根本误解是如何设置扩展的 ParseObject 的值。正如下面的答案所示,此处的 put Parse 方法实际上将值放入对象中。我的印象是 put 只用于实际的数据库操作,因此我的 setter 方法没有正确设置 ParseObject。

呃……我们太亲密了。我发现了问题,您没有将值设置为 ParseObject 中的字段,这就是为什么适配器中的所有内容都为 null 的原因。将以下内容添加到您的清单 class 并按如下方式更改 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);
}

旧答案

我可能错了,但我很确定你应该保存 ParseFile 的 URL 而不是 ParseFile 本身并使用 URL 来获取文件。所以在 done (..) 方法中你应该做这样的事情:

file.getUrl();

并将其设置为字符串。继续前进,如果