Question

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

Was it helpful?

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)

OTHER TIPS

First implement correctly the methods. GetItem(), GetItemId.. return what is needed to return.

Then for the inflater try :

_view = inflater.inflate(R.layout.youhou, group, false);

you should implement the getItem and getItemId

if (convertView == null){
            view = inflater.inflate(R.layout.light_list_item_room, null);
        }

If the view is not null and has inflated with R.layout.light_list_item_floor then when the obj type is ROOM, it will not inflate the "new type". When you try to fetch the textview room from the already inflated view floor, it will return "null".

Try to account that you're not using a different view when recycling.

And like other answers above, you should implement the getItem because at the moment is returning null every time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top