gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:04:15 25 4
gpt4 key购买 nike

我有一个带有 6 个字段的扩展 ParseObject 类。除 ParseFile getter 方法外,所有字段都返回正确的数据。以老式方式获取 ParseFiles 是可行的,但由于某种原因,当我使用扩展类时,调用 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 中的字段,这就是适配器中所有内容均为空的原因。将以下内容添加到您的 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);
}

旧答案

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

file.getUrl();

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

关于java - getParseFile(string key) 使用扩展的 ParseObject 类从 Parse.com 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27753769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com