Frequently calling notifyDataSetChanged() causes expandable list view can't collapse and expand

I have a customize adapter extends BaseExpandableListAdapter, the activity reads android sensors data, once the sensor data has changed, I will set sensor data and call notifyDataSetChanged to my customised adapter to update its data, so that the list view can have the refreshed sensor value displayed.

Since it is reading MAGNETIC_FIELD sensor, the data is updated very frequently. I found that on this situation, the "expandable" list view can't work, neither collapse nor expand well.

piece of codes in my activity here:

private void setMagnetometerData(float x, float y, float z)
    StringBuilder sBuilderX = new StringBuilder();
    StringBuilder sBuilderY = new StringBuilder();
    StringBuilder sBuilderZ = new StringBuilder();
    magnetometerData = new float[3];
    magnetometerData[0] = x;
    magnetometerData[1] = y;
    magnetometerData[2] = z;

    sBuilderX.append("x = ");
    magnetometerStrX = sBuilderX.toString();

    sBuilderY.append("y = ");
    magnetometerStrY = sBuilderY.toString();

    sBuilderZ.append("z = ");
    magnetometerStrZ = sBuilderZ.toString();

    adapter.mChilds[1][0] = magnetometerStrX;
    adapter.mChilds[1][1] = magnetometerStrY;
    adapter.mChilds[1][2] = magnetometerStrZ;

public void onSensorChanged(SensorEvent event)

    float[] sensorsData = event.values;
    int sensorType = event.sensor.getType();


        case Sensor.TYPE_MAGNETIC_FIELD:
            setMagnetometerData(sensorsData[0], sensorsData[1], sensorsData[2]);



I think it is due to notifyDataSetChanged() too frequently called to response to user's click action on the list, is there any solution to this case? How to update customised adapter's data correctly and not making any side effect to the collapsing and expanding action?


You should throttle the sensorChanged events. That is, only call notifyDataSetChanged() if it's been at least a certain amount of time (e.g. 1 second) since the last update - this will give the UI thread enough time to handle user interactions.

See Filtering out noise from Android tilt (accelerometer and magnetic field) sensors? and How to slow down android sensor events

Need Your Help

object over-releases on button press

ios memory-leaks cocos2d-iphone release

This problem has been confusing me for days now. I have an NSString, 'spriteType'. It is declared using the property and synthesise method. In my layer, where the string is created, I also create a

Create a new Prestashop cart in custom php

php prestashop cart

In Prestashop, I've created a custom form where I show a list with all products and the user can fill the corresponding quantities. By submitting the form, I clear the cart and fill it with the new