我对android编程很新。我遇到了一些将图像(位图)加载到ListView中的问题。它完美无缺地工作,直到我加载了大约5个图像,然后它重新使用我加载的第一个图像(假设从缓存)。我已经尝试了很多解决方案和位图采样,但仍然没有运气。这是我的代码:
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);
Contact currentContact = Contacts.get(position);
TextView name = (TextView) view.findViewById(R.id.contactName);
name.setText(currentContact.getName());
TextView phone = (TextView) view.findViewById(R.id.phoneNumber);
phone.setText(currentContact.getPhone());
TextView email = (TextView) view.findViewById(R.id.email);
email.setText(currentContact.getEmail());
TextView address = (TextView) view.findViewById(R.id.cAddress);
address.setText(currentContact.getAddress());
Context mContext = getApplicationContext();
File file = mContext.getDir("imageDir", Context.MODE_PRIVATE);
String path = "file:" + file.getPath() + "/" + currentContact.getImage() + ".png";
ImageView contactListImage = (ImageView) view.findViewById(R.id.contactListImage);
Picasso.with(getApplicationContext())
.load(path)
.centerInside()
.fit()
.error(R.drawable.user_2)
.into(contactListImage)
}
return view;
}
我确实尝试过使用
recycleMemoryCache()
谢谢你提前!
答案 0 :(得分:0)
您未正确使用回收的视图。
你在那里的view
参数是null,这意味着你必须给新的一个膨胀,或者不 null,这意味着你应该重新使用 it - 为所有字段等分配新值。
你需要将结束的大括号移到return view;
之上,直到你的观点膨胀之后:
if (view == null) {
view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);
}
顺便说一句,我会查看视图模式 - 或者只是开始使用RecyclerView,因为它强制执行它(并且是Android上列表未来的方式)。