如何直接将数据添加到arrayAdapter?

时间:2016-10-02 12:46:00

标签: java android bluetooth android-arrayadapter

我尝试使用Bluetooth - Android Developers

中的代码
myArrayAdapter

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

我需要将数据直接添加到arrayAdapter,因为当找到一些新设备时,它应该在我的活动中刷新listview。

我也可以使用ArrayAdapter,但我已将onCreate放在onCreate中,因此无法访问它。

所以我的问题是如何将arrayAdapter置于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); } } }; } 之外而没有任何错误?

我的java类,如果需要代码:

x += y

2 个答案:

答案 0 :(得分:0)

只是位置在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);
            }
        }
    };
}

答案 1 :(得分:0)

只需写下

  

ArrayAdapter myArrayAdapter;

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