我尝试使用ViewHolder实现ListView相对布局。在getView()中,我遇到了NullReference。我在Stack Overflow上找到的示例之后对代码进行了建模。这是代码:
private ViewHolder mViewHolder;
public View getView(int position, View convertView, ViewGroup parent) {
final ToDoItem toDoItem = mItems.get(position);
if (convertView==null){
mViewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.todo_item, parent, false);
RelativeLayout itemLayout = (RelativeLayout) convertView.findViewById(R.id.RelativeLayout1);
mViewHolder.tvTitle = (TextView) convertView.findViewById((R.id.title));
mViewHolder.cbStatus = (CheckBox) convertView.findViewById(R.id.statusCheckBox);
mViewHolder.tvPriority = (TextView) convertView.findViewById(R.id.priority);
mViewHolder.tvDate = (TextView) convertView.findViewById(R.id.date);
convertView.setTag(mViewHolder);
}else{
mViewHolder = (ViewHolder) convertView.getTag();
}
--->mViewHolder.tvTitle.setText(toDoItem.getTitle());
mViewHolder.tvPriority.setText(toDoItem.getPriority().toString());
mViewHolder.tvDate.setText(toDoItem.getDate().toString());
mViewHolder.cbStatus.setChecked(false);
if (toDoItem.getStatus().toString() == "DONE") mViewHolder.cbStatus.setChecked(true);
final TextView titleView = mViewHolder.tvTitle;
final CheckBox statusView = mViewHolder.cbStatus;
final TextView priorityView = mViewHolder.tvPriority;
final TextView dateView = mViewHolder.tvDate;
itemLayout.addView(titleView);
itemLayout.addView(statusView);
itemLayout.addView(priorityView);
itemLayout.addView(dateView);
return convertView;
NullReference抛出
的右括号后面的行if(convertView == null)
并标有---->
我尝试将不同的实体分配给ViewHolder,但收效甚微。我很感激任何人提供的任何帮助。提前谢谢!
Markb
答案 0 :(得分:0)
toDoItem
如果列表中的对象为空,也可以为null。