我正在尝试用 BaseAdapter 填充 ListView。列表在开始时正确加载,但是当我开始滚动时,我从 view.getViewById 得到空值。这是我的代码:
活动:
public class LightActivty extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.light);
listView=(ListView)findViewById(R.id.light_listview);
listView.setAdapter(new LightAdapter(this, Model.model.getFloors()));
}
@Override
public void setContentView(View view) {
// TODO Auto-generated method stub
super.setContentView(view);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.light_acitivty, menu);
return true;
}
}
基本适配器:
public class LightAdapter extends BaseAdapter {
List<BaseObject> items;
Context context;
LayoutInflater inflater;
public LightAdapter(Context context, List<Floor> floors) {
this.context = context;
this.items = initData(floors);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private List<BaseObject> initData(List<Floor> floors) {
List<BaseObject> lst = new ArrayList<BaseObject>();
for (Floor f : floors) {
lst.add(f);
for (Room r : f.getRooms()) {
lst.add(r);
for (Light l : r.getLights()) {
lst.add(l);
}
}
}
return lst;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int index) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Log.d("Test", "start:"+position);
BaseObject obj = (BaseObject) items.get(position);
if (obj.typ == EntityTypes.FLOOR) {
Floor f = (Floor) obj;
Log.d("Test", "floor"+f+";");
if (convertView == null)
view = inflater.inflate(R.layout.light_list_item_floor, null);
TextView textView = (TextView) view
.findViewById(R.id.light_list_item_floor_label);
textView.setText(f.bezeichnung);
} else if (obj.typ == EntityTypes.ROOM) {
Room r = (Room) obj;
//Log.d("Test", "room:"+r+";"+r.bezeichnung);
if (convertView == null){
view = inflater.inflate(R.layout.light_list_item_room, null);
}
TextView textView = (TextView) view
.findViewById(R.id.light_list_item_room_label);
Log.d("Test", r.bezeichnung+";"+textView+";"+view);
textView.setText(r.bezeichnung);
} else if (obj.typ == EntityTypes.LIGHT_ONOFF) {
LightOnOff l = (LightOnOff) obj;
Log.d("Test", "lightOnOFf"+l+";");
if (convertView == null)
view = inflater.inflate(R.layout.light_list_item_lightonoff,
null);
TextView textView = (TextView) view
.findViewById(R.id.light_list_item_lightonoff_label);
textView.setText(l.bezeichnung);
} else if (obj.typ == EntityTypes.LIGHT_DIM) {
LightDim l = (LightDim) obj;
Log.d("Test", "lightDim"+l+";");
if (convertView == null)
view = inflater
.inflate(R.layout.light_list_item_lightdim, null);
TextView textView = (TextView) view
.findViewById(R.id.light_list_item_lightdim_label);
textView.setText(l.bezeichnung);
} else if (obj.typ == EntityTypes.LIGHT_RGB) {
LightRGB l = (LightRGB) obj;
Log.d("Test", "lightRGB"+l+";");
if (convertView == null)
view = inflater
.inflate(R.layout.light_list_item_lightrgb, null);
TextView textView = (TextView) view
.findViewById(R.id.light_list_item_lightrgb_label);
textView.setText(l.bezeichnung);
}
Log.d("Test", "end:"+position);
return view;
}
}
布局xml(目前我使用的所有4个布局都是相同的,当然除了id)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/black">
<TextView
android:id="@+id/light_list_item_room_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:layout_marginTop="20dp"
android:textSize="15sp"
android:textColor="@color/white">
</TextView>
</LinearLayout>
错误发生在
else if (obj.typ == EntityTypes.ROOM)
在线
TextView textView = (TextView) view.findViewById(R.id.light_list_item_room_label);
textView 在这里为空,但并非总是如此,只有当我第一次滚动时,其中一行才会为 textview 返回 null。
有任何想法吗?谢谢弗洛里安