0

I am trying to make listview with available bluetooth devices. I trying to use arrayAdapter. I have made string array, but I can't add anything to this array with my code:

String[] values;

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // Add the name and address to an array adapter to show in a ListView
                values.add(device.getName() + "\n" + device.getAddress());
            }
        }
    }; 

Android studio says that it cant resolve method add and markups it.

The whole code:

enter image description here

How to fix this and make this work?

Thanks.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
supercoder
  • 1
  • 1
  • 3

2 Answers2

0

You can use List instead of Array like this:

While declaring:

List<String> values = new ArrayList<String>();

While adding:

values.add(device.getName() + "\n" + device.getAddress());
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
0

Basically an Array is a container object that holds a fixed number of values of a single type, so if its initialized to a particular value you cannot add nor remove from it. Thats why you can't call such method.

What you can do instead is use Lists which are specifically designed to do this:

List<String> values = new ArrayList<String>();
values.add(device.getName() + "\n" + device.getAddress());