The list view does not update when I return to the activity

advertisements
private void startBt () {

    listPairedBT = (ListView) findViewById(R.id.listPairedBT);
    listPairedBT.setOnItemClickListener(this);
    listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
    btStatus = (TextView) findViewById(R.id.tvBtStatus);
    listPairedBT.setAdapter(listAdapter);
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    pairedDevices = new ArrayList<String>();
    filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    receiver = new BroadcastReceiver()
    {
        public void onReceive(android.content.Context context, Intent intent) {
            String action = intent.getAction();
            System.out.println("123456789");
            if(BluetoothDevice.ACTION_FOUND.equals(action))
            {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                System.out.println("123456789 " + device.getName()+" "+" "+ "\n"+device.getAddress());

                String s = "";
                for(int a=0; a < pairedDevices.size(); a++)
                {
                    if(device.getName().equals(pairedDevices.get(a)))
                    {
                        s = "(Paired)";
                        break;
                    }
                }
                listAdapter.add(device.getName()+" "+s+" "+ "\n"+device.getAddress());
            }
            else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
            {

            }
            else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
            {
                //btAdapter.cancelDiscovery();//added to test

            }
            else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action))
            {
                if(btAdapter.getState()==btAdapter.STATE_OFF)
                {
                    turnOnBluetooth();
                }
            }
        }
    };
    registerReceiver(receiver, filter);
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    registerReceiver(receiver, filter);
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(receiver, filter);
    filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(receiver, filter);

    btnPreference.setOnClickListener(this);

    if(btAdapter==null)
    {
        Toast.makeText(getApplicationContext(), "Bluetooth does not support!", 0).show();
        finish();
    }
    else
    {
        if(!btAdapter.isEnabled())
        {
            turnOnBluetooth();
        }
        getPairedDevices();
        startDiscovery();
    }
    mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch(msg.what)
            {
            case SUCCESS_CONNECT:
                ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
                btStatus.setText("Connected");
                connectedThread.start();

                break;

            case MESSAGE_READ:

                byte[] readBuf = (byte[]) msg.obj;
                String btReceivedTemp = new String(readBuf, 0, msg.arg1);                 // create string from bytes array
                stringBuilder.append(btReceivedTemp);                                                // append string
                int endOfLineIndex = stringBuilder.indexOf("\r\n");                            // determine the end-of-line
                if (endOfLineIndex > 0) {                                           // if end-of-line,
                    String btReceived = stringBuilder.substring(0, endOfLineIndex);               // extract string
                    stringBuilder.delete(0, stringBuilder.length());                                      // and clear
                    btStatus.setText("Data from Arduino: " + btReceived);               // update TextView
                    //if accident string got caught, call accident % method

                    // check the event type and add event to the event manager
                    if (btReceived.equalsIgnoreCase("1")) { // switch event
                        eventsManager.addEvent("switch_pressed");
                    } else if (btReceived.equalsIgnoreCase("kaboom")) { // vibration sensor event
                        eventsManager.addEvent("vibration_sensor");
                    }
                }                                                           

            }
        }
    };
}

I put the code in to startBt method so i can call it at onCreate() and OnResume(). Bluetooth connection receives data when i come back to the activity from another activity but it does not update the list view. It keeps empty


In your onActivityResult() method you need to call listPairedBT.notifyDataSetChanged().