1
我想使用的ListView ViewHolder模式(我知道RecyclerView但我想它在ListView的!)。安卓:convertView ListView中總是空
我創造了它,但我注意到,在convertView getView()總是空。每次調用getView()。我究竟做錯了什麼?這一個
viewHolder = (ViewHolder) convertView.getTag();
:
public class ScheduleListAdapter extends BaseAdapter {
private LayoutInflater lInflater;
private List<ScheduleSubject> scheduleListItems;
public ScheduleListAdapter(Context context, List<ScheduleSubject> scheduleListItems) {
this.scheduleListItems = scheduleListItems;
lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return scheduleListItems.size();
}
@Override
public Object getItem(int position) {
return scheduleListItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private static class ViewHolder {
TextView name, place;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = lInflater.inflate(R.layout.schedule_item_show, parent, false);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.schedule_item_name_textView);
viewHolder.place = (TextView) convertView.findViewById(R.id.schedule_item_place_textView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ScheduleSubject scheduleListItem = getScheduleListItem(position);
viewHolder.number.setText(String.valueOf(scheduleListItem.getLessonNumber()));
viewHolder.name.setText(scheduleListItem.getName());
viewHolder.place.setText(scheduleListItem.getPlace());
return convertView;
}
public ScheduleSubject getScheduleListItem(int position) {
return ((ScheduleSubject) getItem(position));
}
}
請將logcat添加到您的問題。 – JoelFernandes