gpt4 book ai didi

android - ViewHolder 不起作用

转载 作者:行者123 更新时间:2023-11-30 00:57:44 28 4
gpt4 key购买 nike

public class ListViewAdapter extends BaseAdapter { 
private Context context;
private LocationDetails[] locationDetails;

public ListViewAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;

}

@Override
public int getCount() {
return locationDetails.length;
}

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (view == null) {
list = new View(context);
list = inflater.inflate(R.layout.listview_layout, null);
TextView name = (TextView) list.findViewById(R.id.location_name);
TextView desc = (TextView) list.findViewById(R.id.location_desc);
ImageView img = (ImageView) list.findViewById(R.id.location_image);

name.setText(locationDetails[i].getLocationName());
desc.setText(locationDetails[i].getLocationDesc());
img.setImageResource(locationDetails[i].getLocationIcon());
} else {
list = (View) view;
}

return list;
}
}

这段代码是我使用viewholder之前的代码,效果很好。然后我尝试用viewholder修改,用gradle编译,所有的image和textviews都不见了,不知道下面的代码有什么问题。为什么我无法在屏幕上显示任何内容?

public class LocationAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;

public LocationAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;

}

@Override
public int getCount() {
return locationDetails.length;
}

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list = view;
ViewHolder vh;


if (list == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = inflater.inflate(R.layout.listview_layout, null);

vh = new ViewHolder();

vh.name = (TextView) list.findViewById(R.id.location_name);
vh.desc = (TextView) list.findViewById(R.id.location_desc);
vh.img = (ImageView) list.findViewById(R.id.location_image);

list.setTag(vh);
} else {
vh = (ViewHolder) list.getTag();
}

return list;
}

static class ViewHolder {
TextView name;
TextView desc;
ImageView img;
}
}

最佳答案

public class LocationAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;
Object model;

public LocationAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;

}

@Override
public int getCount() {
return locationDetails.length;
}

@Override
public Object getItem(int i) {
return i;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list = view;
ViewHolder vh;


if (list == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = inflater.inflate(R.layout.listview_layout, null);

vh = new ViewHolder();

vh.name = (TextView) list.findViewById(R.id.location_name);
vh.desc = (TextView) list.findViewById(R.id.location_desc);
vh.img = (ImageView) list.findViewById(R.id.location_image);

list.setTag(vh);
} else {
vh = (ViewHolder) list.getTag();
}
model = getItem(i);

vh.name.setText(model.getLocationName());
vh.desc.setText(model.getLocationDesc());
// same as image...

return list;
}

static class ViewHolder {
TextView name;
TextView desc;
ImageView img;
}
}

关于android - ViewHolder 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39893619/

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