我有一个List<User> ,我想把这个集合列表在listView中,每一行一个对象,每一行里有属性。 class ListItemAdapter extends BaseAdapter{
private LayoutInflater layoutInflater;
public ListItemAdapter(Context context){
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return users.size();
} @Override
public Object getItem(int position) {
return null;
} @Override
public long getItemId(int position) {
return 0;
} @Override
public View getView(final int position, View convertView, ViewGroup parent) {
ListItemView itemView=null;
if(convertView==null){
itemView = new ListItemView();
convertView = layoutInflater.inflate(R.layout.user_listview_item, null);
itemView.userName = (TextView)findViewById(R.id.userName);
itemView.userSex = (TextView)findViewById(R.id.userSex);
itemView.userAge = (TextView)findViewById(R.id.userAge);
itemView.userType = (TextView)findViewById(R.id.userType);
itemView.checkBox = (CheckBox)findViewById(R.id.chkTodayAccountItem);
System.out.println("结果:"+users.get(position).getName().toString());
itemView.userName.setText(String.valueOf(users.get(position).getName()));
itemView.userSex.setText(String.valueOf(users.get(position).getSex()));
itemView.userAge.setText(String.valueOf(users.get(position).getAge()));
UserEnum type = UserEnum.getUserEnum(users.get(position).getType().getValue());
itemView.userType.setText(type.toString());
itemView.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
users.get(position).setSelected(isChecked);
}
});
convertView.setTag(itemView);
}else{
itemView= (ListItemView)convertView.getTag();
}
return convertView;
}
}
class ListItemView
{
public TextView userName;
public TextView userSex;
public TextView userAge;
public TextView userType;
public CheckBox checkBox;
}
但是在执行到itemView.userName.setText(String.valueOf(users.get(position).getName())); 就出错了。报nullPointerException异常。但String.valueOf(users.get(position).getName()) 是有值的。集合里也是有值的。不为空。
不知道那出了问题了
private LayoutInflater layoutInflater;
public ListItemAdapter(Context context){
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return users.size();
} @Override
public Object getItem(int position) {
return null;
} @Override
public long getItemId(int position) {
return 0;
} @Override
public View getView(final int position, View convertView, ViewGroup parent) {
ListItemView itemView=null;
if(convertView==null){
itemView = new ListItemView();
convertView = layoutInflater.inflate(R.layout.user_listview_item, null);
itemView.userName = (TextView)findViewById(R.id.userName);
itemView.userSex = (TextView)findViewById(R.id.userSex);
itemView.userAge = (TextView)findViewById(R.id.userAge);
itemView.userType = (TextView)findViewById(R.id.userType);
itemView.checkBox = (CheckBox)findViewById(R.id.chkTodayAccountItem);
System.out.println("结果:"+users.get(position).getName().toString());
itemView.userName.setText(String.valueOf(users.get(position).getName()));
itemView.userSex.setText(String.valueOf(users.get(position).getSex()));
itemView.userAge.setText(String.valueOf(users.get(position).getAge()));
UserEnum type = UserEnum.getUserEnum(users.get(position).getType().getValue());
itemView.userType.setText(type.toString());
itemView.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
users.get(position).setSelected(isChecked);
}
});
convertView.setTag(itemView);
}else{
itemView= (ListItemView)convertView.getTag();
}
return convertView;
}
}
class ListItemView
{
public TextView userName;
public TextView userSex;
public TextView userAge;
public TextView userType;
public CheckBox checkBox;
}
但是在执行到itemView.userName.setText(String.valueOf(users.get(position).getName())); 就出错了。报nullPointerException异常。但String.valueOf(users.get(position).getName()) 是有值的。集合里也是有值的。不为空。
不知道那出了问题了
改为:
itemView.userName = (TextView)convertView.findViewById(R.id.userName);
你用layoutInflater指定一个布局文件填充了一个view.所以布局文件里的组件都在该view中.