2016-10-01 49 views
0

我正在嘗試使用可用的藍牙設備進行列表視圖。我試圖使用arrayAdapter。我已經做字符串數組,但這個數組我的代碼,我不能添加任何東西:如何在Android上向字符串數組添加元素

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工作室表示,它不能解析方法add和標記它。

整個代碼:

enter image description here

如何解決這個問題,使這項工作?

謝謝。

+2

http://stackoverflow.com/questions/2843366/how-to-add-new-elements-to-數組 – CommonsWare

回答

0

可以使用List代替陣列是這樣的:

雖然宣稱:

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

同時加入:

values.add(device.getName() + "\n" + device.getAddress()); 
0

基本上是一個Array是保持固定數量的容器對象的單一類型的值,所以如果它被初始化爲一個特定的值,你不能添加也不能從中刪除。這就是爲什麼你不能調用這種方法。

你可以做什麼,而不是是是專門做這個的使用列表:

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