gpt4 book ai didi

java - ParseQueryAdapter 加载图像不正确

转载 作者:行者123 更新时间:2023-11-29 21:11:36 24 4
gpt4 key购买 nike

我设置了 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;
}

}

Activity

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);
}
}

最佳答案

如果 photoFile == null,您必须以某种方式清除 ParseImageView。例如将透明色设置为图像资源。因此,按以下方式修改 LocalSalesAdapter 类的 getItemView(...) 方法:

@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);
}

...
}

关于java - ParseQueryAdapter 加载图像不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602645/

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