如何直接向arrayAdapter添加数据?

How add data directly to the arrayAdapter?

我正在尝试使用来自 Bluetooth - Android Developers

的代码
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
    // Loop through paired devices
    for (BluetoothDevice device : pairedDevices) {
        // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
}

但是我无法将数据添加到 myArrayAdapter,因为它被放置在 private void onCreate() 并且添加方法抛出错误,没有这样的适配器。

我需要将数据直接添加到 arrayAdapter,因为当发现一些新设备时它应该刷新我 activity 中的列表视图。

我也可以使用 myArrayAdapter.notifyDataSetChanged(),但就像我说的,我的 ArrayAdapter 放在 onCreate 中,所以它无法访问它。

所以我的问题是如何将 arrayAdapter 放置在 onCreate 之外而不会出现任何错误?

我的 java class 如果需要,请提供代码:

public class DeviceList extends AppCompatActivity {

    private final static int REQUEST_ENABLE_BT = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);

        BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();
        if (BTAdapter != null) {
            if (!BTAdapter.isEnabled()) {
                Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBT, REQUEST_ENABLE_BT);
            }
            if (BTAdapter.isDiscovering()) {
                BTAdapter.cancelDiscovery();
            }
            BTAdapter.startDiscovery();
        }

        ArrayAdapter<String> myArrayAdapter;
        ListView listView = (ListView) findViewById(R.id.inputELM);
        myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myEntries);
        listView.setAdapter(myArrayAdapter);

        // Register the BroadcastReceiver
        IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver, ifilter);
    }

    ArrayList<String> myEntries = new ArrayList<>();
    // Create a BroadcastReceiver for ACTION_FOUND
    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
                String devs = device.getName() + "\n" + device.getAddress();
                myEntries.add(devs);

            }
        }
    };
}

就在外面onCreate

public class DeviceList extends AppCompatActivity {

    private final static int REQUEST_ENABLE_BT = 1;

    // Declare here 
    private ArrayAdapter<String> myArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);

        BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();
        if (BTAdapter != null) {
            if (!BTAdapter.isEnabled()) {
                Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBT, REQUEST_ENABLE_BT);
            }
            if (BTAdapter.isDiscovering()) {
                BTAdapter.cancelDiscovery();
            }
            BTAdapter.startDiscovery();
        }

        ListView listView = (ListView) findViewById(R.id.inputELM);
        myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myEntries);
        listView.setAdapter(myArrayAdapter);

        // Register the BroadcastReceiver
        IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver, ifilter);
    }

    ArrayList<String> myEntries = new ArrayList<>();
    // Create a BroadcastReceiver for ACTION_FOUND
    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
                String devs = device.getName() + "\n" + device.getAddress();
                myEntries.add(devs);
            }
        }
    };
}

随便写

ArrayAdapter myArrayAdapter;

使用静态变量声明,这样您就可以从所有方法访问它。然后您可以从任何地方访问它。