我有一个列表视图,显示一些文本和图像。 listview似乎工作正常。但今天我意识到滚动列表视图时存在内存泄漏。
我在logcat中收到了很多警告:
08-12 17:06:15.037: D/dalvikvm(10826): GC_FOR_ALLOC freed 3499K, 28% free 36413K/49991K, paused 30ms, total 32ms
这是我的代码:
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.event_item_typical, null);
holder = new ViewHolder();
holder.eventTitle = (TextView) convertView.findViewById(R.id.tv_EventTitle);
holder.eventTypeName = (TextView) convertView.findViewById(R.id.tv_EventTypeName);
holder.eventImg = (ImageView) convertView.findViewById(R.id.imgv_EventType);
holder.eventLocationName = (TextView) convertView.findViewById(R.id.tv_EventLocation);
holder.eventDate = (TextView) convertView.findViewById(R.id.tv_EventDate);
holder.eventTime = (TextView) convertView.findViewById(R.id.tv_EventTime);
holder.eventStatus = (TextView) convertView.findViewById(R.id.tv_EventStatus);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
MyEvent cEvent = getItem(position);
holder.eventTitle.setText(cEvent.getTitle());
ActionTypes recordedType = cEvent.getActionType();
if (recordedType != null)
{
holder.eventImg.setImageResource(recordedType.getImageId());
holder.eventTypeName.setText(recordedType.getFullName());
}
holder.eventLocationName.setText(cEvent.getLocationName());
holder.eventDate.setText(cEvent.getStartDate());
holder.eventTime.setText(cEvent.getStartTime());
holder.eventStatus.setText(cEvent.getStatus().name());
if (cEvent.getStatus() == EventStatus.Active) {
// holder.eventLocationName.setTextColor(MyContext.getResources().getColorStateList(R.color.confirmed));
holder.eventStatus.setBackgroundResource(R.color.confirmed);
} else if (cEvent.getStatus() == EventStatus.Expired){
// holder.eventLocationName.setTextColor(MyContext.getResources().getColorStateList(R.color.declined));
holder.eventStatus.setBackgroundResource(R.color.declined);
} else if (cEvent.getStatus() == EventStatus.Cancelled){
// holder.eventLocationName.setTextColor(MyContext.getResources().getColorStateList(R.color.pending));
holder.eventStatus.setBackgroundResource(R.color.pending);
}
holder.eventStatus.setTypeface(null, Typeface.ITALIC);
return convertView;
}
我知道更新ImageView时会发生内存泄漏:holder.eventImg.setImageResource(recordedType.getImageId());
我访问Drawables中的图像。
但是,如果我调用holder.eventImg.setImageResource(R.drawable.icon1),我不会得到这些内存泄漏。但我无法对图像进行硬编码。我需要在列表项上显示不同的图像。请帮我解决这个内存泄漏问题。
这是ActionTypes的代码。它是一个包含图像ID的Enum类。我只在枚举类中发布了一半的项目。
public enum ActionTypes {
Archery (ActionCategoty.Sports, "Archery", R.drawable.action_archery_250, null),
BabyShowerBoy (ActionCategoty.Social, "Baby Shower Boy", R.drawable.action_shower_boy_250, null),
BabyShowerGirl (ActionCategoty.Social, "Baby Shower Girl", R.drawable.action_babyshower_girl_250, null),
Badminton (ActionCategoty.Sports, "Badminton", R.drawable.action_batminton43, null),
Ballooning (ActionCategoty.Hobbies, "Ballooning", R.drawable.action_balooning_250, null),
Baseball (ActionCategoty.Sports,"Baseball", R.drawable.action_baseball_28, null),
Basketball (ActionCategoty.Sports,"Basketball", R.drawable.action_basketball6, null),
Bbq (ActionCategoty.Social,"BBQ", R.drawable.action_bbq_250, null),
Bowling (ActionCategoty.Sports,"Bowling", R.drawable.action_bowling_15, null),
Boxing (ActionCategoty.Sports,"Boxing", R.drawable.action_boxing_32, null),
private ActionCategoty category; // Id of the team for the action
private String fullName; // Complete name of the action
private final int imageId; // Id of the logo of the sport image in the resources
private String teamId; // Id of the team for the action
private ActionSkillLevel skillevel;
private ActionTypes(ActionCategoty _category, String _fullName, int _imageId, String _teamId) {
this.setCategory(_category);
this.fullName = _fullName;
this.imageId = _imageId;
this.teamId = _teamId;
}
public String getFullName(){
return fullName;
}
public int getImageId() {
return imageId;
}
public String getTeamId() {
if (teamId == null)
{
teamId = "null";
}
return teamId;
}
public void setTeamId(String _teamId) {
this.teamId = _teamId;
}
public ActionCategoty getCategory() {
return category;
}
public void setCategory(ActionCategoty category) {
this.category = category;
}
public ActionSkillLevel getSkillevel() {
if (this.skillevel == ActionSkillLevel.X){
this.skillevel = null;
}
return skillevel;
}
public void setSkillevel(ActionSkillLevel skillevel) {
if (skillevel != ActionSkillLevel.X){
this.skillevel = skillevel;
} else {
this.skillevel = null;
}
}
// Category
public enum ActionCategoty {Sports, Games, Social , Hobbies, Fitness;}
// Skill Level
public enum ActionSkillLevel { X("Select Your Skill Level"),
A("A - Professional"),
B("B - Elite"),
C("C - Advanced"),
D("D - Intermediate"),
E("E - Beginner");
private String definition;
private ActionSkillLevel(String _definition) {
this.definition = _definition;
}
public String getDefinition(){
return definition;
}
}
}