ViewHolder不起作用

时间:2016-10-06 10:29:08

标签: android android-viewholder

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编译它,所有的图像和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;
}
}

1 个答案:

答案 0 :(得分:1)

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