我已经设置了ParseQueryAdapter,但看起来图像没有正确加载。我总共有10个对象,最新的3个有图像。
我的适配器正在使用适配器将所有这些对象拉到ListView中,但看起来不仅有3个对象显示图片,而且其他一些对象正在显示这些相同的图片,即使它们不是因为没有对象的图像。
这可能是什么原因?请注意,当我滚动下面的几个时,将从列表中的前三个项目中取出图片。
适配器
public class LocalSalesAdapter extends ParseQueryAdapter<Sale> {
public LocalSalesAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<Sale>() {
@SuppressWarnings("unchecked")
public ParseQuery<Sale> create() {
@SuppressWarnings("rawtypes")
ParseQuery query = new ParseQuery("Sales");
query.orderByDescending("createdAt");
return query;
}
});
}
@Override
public View getItemView(Sale sale, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.listview_local_sales_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) {
}
});
}
TextView titleTextView = (TextView) v.findViewById(R.id.textView_listView_localSalesSubject);
titleTextView.setText(sale.getSaleTitle());
TextView descriptionTextView = (TextView) v.findViewById(R.id.textView_listView_localSalesDescription);
descriptionTextView.setText(sale.getSaleDescription());
TextView priceTextView = (TextView) v.findViewById(R.id.textView_listView_localSalesPrice);
priceTextView.setText(sale.getSalePrice());
return v;
}
}
活动
public class LocalSalesActivity extends ListActivity {
ListView mLocalSales;
ParseUser mCurrentUser;
String mTAG = "LocalSalesActivity";
private LocalSalesAdapter localSalesAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_sales);
mLocalSales = (ListView) findViewById(android.R.id.list);
localSalesAdapter = new LocalSalesAdapter(this);
setListAdapter(localSalesAdapter);
}
@Override
protected void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
}
@SuppressLint("InlinedApi")
public void presentUserWithLogin() {
// Displaying the Login Activity to the user
Intent i = new Intent(this, LoginActivity.class);
// Logging in = New Task, Old Task = Clear so back button cannot be used to go back into Profile Activity if logged out.
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.local_sales, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_log_out:
// Logging out the current user and presenting them with the login activity.
ParseUser.logOut();
presentUserWithLogin();
break;
case R.id.action_post_sale:
// Launching new intent to start Post Sale Activity
Intent i = new Intent(this, PostSaleActivity.class);
startActivity(i);
break;
case R.id.action_profile_activity:
finish();
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:2)
如果photoFile == null
,你必须以某种方式清除你的ParseImageView。例如,将透明颜色设置为图像资源。因此,请按以下方式修改getItemView(...)
类的LocalSalesAdapter
方法:
@Override
public View getItemView(Sale sale, View v, ViewGroup 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 an object doesn't have a photo
saleImage.setImageResource(android.R.color.transparent);
}
...
}