0
我使用ParseQueryAdapter像這樣:用數據更新ListView的正確方法?
public class MyPostedSalesAdapter extends ParseQueryAdapter<Sale> {
static String mTAG = "LocalSalesAdapter";
static ParseUser mCurrentUser = ParseUser.getCurrentUser();
public MyPostedSalesAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<Sale>() {
@SuppressWarnings("unchecked")
public ParseQuery<Sale> create() {
@SuppressWarnings("rawtypes")
ParseQuery query = new ParseQuery("Sales");
query.orderByDescending("createdAt");
query.whereEqualTo("postedBy", mCurrentUser.getUsername());
try {
int salesAmount = query.count();
ProfileActivity.setTextView(salesAmount);
} catch (ParseException e) {
e.printStackTrace();
}
return query;
}
});
}
@Override
public View getItemView(Sale sale, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.listview_cell, null);
}
super.getItemView(sale, v, parent);
ParseImageView saleImage = (ParseImageView) v.findViewById(R.id.icon);
ParseFile photoFile = sale.getParseFile("photo");
if (photoFile != null) {
saleImage.setParseFile(photoFile);
saleImage.loadInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
}
});
} else {
// Clear ParseImageView if the object has no photo, set placeholder.
saleImage.setImageResource(R.drawable.placeholder);
}
TextView titleTextView = (TextView) v.findViewById(R.id.textView_listView_saleTitle);
titleTextView.setText(sale.getSaleTitle());
TextView priceTextView = (TextView) v.findViewById(R.id.textView_listView_salePrice);
priceTextView.setText(sale.getSalePrice());
return v;
}
}
,簡單的onResume,設置適配器的ListView像這樣:
@Override
protected void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
// Setting up currentUser to current logged in user
// If user is not logged in, present them with Login Activity
if (mCurrentUser == null || !isUserOnline())
{
presentUserWithLogin();
}
else
{
mOwnSalesAdapter = new MyPostedSalesAdapter(this);
mOwnSalesAdapter.loadObjects();
mUserSales.setAdapter(mOwnSalesAdapter);
}
}
我這樣做的onResume,想着每次活動被加載時,新數據被拖動使用查詢並顯示在ListView中。但事實並非如此,有時候會這樣。通常我必須關閉整個應用程序才能正確更新ListView並顯示數據的任何更新。即使如果我要註銷當前解析用戶,並在不關閉應用程序的情況下登錄另一個帳戶,以前的用戶帖子有時也會出現在列表視圖中。我究竟做錯了什麼?我在iOS上從未遇到過這樣的問題。還注意到如果我要從Parse中刪除一個Item並返回,通常該項目仍然出現在ListView中。