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)

    @Override
    public void onSensorChanged(SensorEvent event) {
        RealTimeSensor realTimeSensor = new RealTimeSensor();
        realTimeSensor.setName(AvailableSensors.getType(event.sensor.getType()).toUpperCase());
    
        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();
        intent.setAction("sensorDataMap");
        context.sendBroadcast(intent);
    
        adaptor.notifyDataSetChanged();
    }
    
  2. Broadcast receiver class

    @Override
    public void onReceive(Context context, Intent intent) {
    
        TempStore.realTimeSensors.clear();
        TempStore.realTimeSensors.addAll(TempStore.sensorDataMap.values());
    }
    
  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);
        }
    
    
        realTimeSensorChangeReceiver.updateOnChange(adaptor1);
        listView.setAdapter(adaptor1);
    
  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;
        this.data = data;
    
    }
    
    
    @Override
    public int getCount() {
        return data.size();
    }
    
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return 0;
    }
    
    
    @Override
    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();
            holder.name = (TextView) view.findViewById(R.id.name);
            holder.valuesX = (TextView) view.findViewById(R.id.X);
            holder.valuesY = (TextView) view.findViewById(R.id.Y);
            holder.valuesZ = (TextView) view.findViewById(R.id.Z);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
    
        RealTimeSensor data = this.data.get(position);
    
        holder.name.setText(data.getName());
        holder.valuesX.setText(data.getValueX());
        holder.valuesY.setText(data.getValueY());
        holder.valuesZ.setText(data.getValueZ());
    
        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.

Answers


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: