Not removing elements from the ListView when changing the dataset

I'm creating an Android application which lets user to select the available sensors in the device, list the the selected sensors with the actual sensor readings.

Here I use a custom adapter extending the BaseAdaptor class. I asked a question on updating the list view realtime when dataset is changing and I was able to fix it.

Method I used to fix it:-

  1. Define a public class which has 2 public fields to store data. A hashMap and a List (To be used for Adapter)
  2. When the sensor is changed, put the sensor value and the sensor name to a hashmap and send a broadcast.
  3. In the broadcast receiver class, if there are previous records in the list, clear it and put the readings to the list.

I pass the adapter to the sensor reading class so that I call adapter.notifydataSetChanged() when sensor changed.

Here is the UI workflow.

Here are my source codes.

  1. Sensor reader class. (RealtimeSensor is a class which stores the sensor data)

    public void onSensorChanged(SensorEvent event) {
        RealTimeSensor realTimeSensor = new RealTimeSensor();
        realTimeSensor.setValueX(event.values[0] + "");
        realTimeSensor.setValueY(event.values[1] + "");
        realTimeSensor.setValueZ(event.values[2] + "");
        TempStore.sensorDataMap.put(AvailableSensors.getType(event.sensor.getType()), realTimeSensor);
        Intent intent = new Intent();
  2. Broadcast receiver class

    public void onReceive(Context context, Intent intent) {
  3. onDialogPositiveClick method

    SensorViewAdaptor adaptor1 = new                                                                          SensorViewAdaptor(getApplicationContext(), TempStore.realTimeSensors);
        sensorReader = new RealTimeSensorReader(this, adaptor1);
        for (Sensor s : sensors) {
            sensorManager.registerListener(sensorReader, s, SensorManager.SENSOR_DELAY_NORMAL);
  4. The adapter class

    public class SensorViewAdaptor extends BaseAdapter {
    private Context context;
    private List<RealTimeSensor> data;
    public SensorViewAdaptor(Context context, List<RealTimeSensor> data) {
        this.context = context; = data;
    public int getCount() {
        return data.size();
    public Object getItem(int position) {
        return data.get(position);
    public long getItemId(int position) {
        return 0;
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view;
        if (convertView == null) {
            view = inflater.inflate(R.layout.display_sensor_values, parent, false);
            holder = new ViewHolder();
   = (TextView) view.findViewById(;
            holder.valuesX = (TextView) view.findViewById(;
            holder.valuesY = (TextView) view.findViewById(;
            holder.valuesZ = (TextView) view.findViewById(;
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        RealTimeSensor data =;
        return view;
    private class ViewHolder {
        public TextView name;
        public TextView valuesX;
        public TextView valuesY;
        public TextView valuesZ;


My problem is, when I deselect one of the selected sensors, it does not remove from the view. What would be the reason for this?

Thank you very much.


when you are deselecting the sensor are you removing that from the adapter's list too?

Need Your Help

Image save in database

php sql-server-2000

I want to save an uploaded image to database. Now my question is:

Android NameValuePair not Accepting String

android http-post

I am using HTTPPost to send values to a server. The script works fine with what I have below. However if I change the name value pair to this I get a force close: