[英]getParseFile(string key) returning null from Parse.com with extended ParseObject class
我有6個字段的擴展ParseObject類。 除ParseFile getter方法外,所有字段均返回正確的數據。 以舊的方式獲取ParseFiles是可行的,但是由於某種原因,當我使用擴展類時,調用GetDataCallback時數據為null。 它具有URL和圖像名稱,但數據字段為null。
我的延伸課程:
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來獲取文件。 因此,在完成(..)方法中,您應該執行以下操作:
file.getUrl();
並將其設置為字符串。 繼續前進,如果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.