Search code examples
androidandroid-listviewfindviewbyid

findViewById returning null after scrolling int a ListView


I'm trying to fill a ListView with an BaseAdapter. The List is loading properly at the beginning, but when I start to scroll, I get null back from view.getViewById. Here's my code:

Activity:

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

the BaseAdapter:

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

the layout xml (At the moment all of the 4 layout i using are the same, except the id of course)

<?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>

The error occurs in

else if (obj.typ == EntityTypes.ROOM)

at line

TextView textView = (TextView) view.findViewById(R.id.light_list_item_room_label);

the textView is null here, but not always, only if I'm, scrolling for the first time one of the rows returns null for the textview.

Any Ideas? Thanks Florian


Solution

  • You use different layouts in your getView() method. So you should implement getItemViewId() , getViewTypeCount() properly.

    As you used 4 layouts, your getViewTypeCount() should return 4 and you can implement getItemViewId() by checking obj.typ .

    getItemViewId() should return 0 to 3.

    ref: http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)