我有一个扩展的ParseObject类与6个字段。除了ParseFile getter方法外,所有字段都会返回正确的数据。获得ParseFiles这种老式的方式有效,但由于某种原因,当我使用扩展类时,调用GetDataCallback时数据为空。它具有URL和图像名称,但数据字段为空。getParseFile(字符串键)从Parse.com返回null与扩展ParseObject类
我的扩展类:
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。
您的方法可能会工作,但这不是我想要做的。我就像他们在Mealspotting应用程序教程中那样做。 – Johnny
请注意,如果我没有错,教程有一段时间没有更新。 – Aashir
那么现在我要回去不使用我的对象来检索数据。这仍然可以正常工作。令人沮丧的是,除了ParseFile之外,所有的数据库操作都可以工作。如果你不能使用它,它有点违背了你自己班级的目的。 – Johnny