ListView一次只保存一个值

时间:2016-04-08 15:24:16

标签: java android listview bluetooth android-arrayadapter

我正在尝试使用蓝牙发现设备并将这些发现的蓝牙设备添加到ListView。但由于某种原因,我的软件会逐一将这些已发现的设备添加到列表中,并始终替换前一个设备,以便一次只能在列表中显示一个设备。

第二个问题是,即使我的软件找到蓝牙设备,它也会执行if语句,检查ArrayAdapter是否为空。

private final BroadcastReceiver Receiver = new BroadcastReceiver(){

        public void onReceive(Context context, Intent intent){

        ArrayList discoveredBluetoothDevices = new ArrayList();
        final ArrayAdapter discoveredBluetoothAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, discoveredBluetoothDevices);
        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, check first
            // it isn't already paired device
            if(device.getBondState() != BluetoothDevice.BOND_BONDED) {

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

                DiscoveredBluetoothDeviceList.setAdapter(discoveredBluetoothAdapter);

            }
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

            //Set the activity title
            setTitle(R.id.bluetooth);

            if(discoveredBluetoothAdapter.getCount() == 0){

                Toast.makeText(getApplicationContext(), "No devices found", Toast.LENGTH_SHORT).show();
            }

        }

    }
};

ListView DiscoveredBluetoothDeviceList = (ListView) findViewById(R.id.BluetoothDeviceList);

// Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(Receiver, filter);

// Register for broadcasts when discovery has finished
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(Receiver, filter);

我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bluetooth"
tools:context="com.example.jake.bluetooth.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/BluetoothDeviceList"
    android:background="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    android:divider="#000000"
    android:dividerHeight="1dp" />
</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/SetOnBluetooth"
    android:id="@+id/SetOnBluetoothButton"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="133dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/SetOffBluetooth"
    android:id="@+id/SetOffBluetoothButton"
    android:layout_above="@+id/SetOnBluetoothButton"
    android:layout_centerHorizontal="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Query"
    android:id="@+id/QueryButton"
    android:layout_above="@+id/ReceiveButton"
    android:layout_alignLeft="@+id/ReceiveButton"
    android:layout_alignStart="@+id/ReceiveButton" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/DiscoverDevices"
    android:id="@+id/DiscoverDevicesButton"
    android:layout_centerHorizontal="true"
    android:layout_above="@+id/SetOffBluetoothButton" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Connect"
    android:id="@+id/ConnectButton"
    android:layout_alignTop="@+id/SetOnBluetoothButton"
    android:layout_toLeftOf="@id/SetOnBluetoothButton" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Send"
    android:id="@+id/SendButton"
    android:layout_toLeftOf="@+id/SetOffBluetoothButton"
    android:layout_alignTop="@+id/SetOffBluetoothButton"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Receive"
    android:id="@+id/ReceiveButton"
    android:layout_alignTop="@+id/SetOnBluetoothButton"
    android:layout_toRightOf="@+id/SetOnBluetoothButton" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Visible"
    android:id="@+id/VisibleButton"
    android:layout_above="@+id/DiscoverDevicesButton"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Exit"
    android:id="@+id/ExitButton"
    android:layout_above="@+id/VisibleButton"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

如果你能帮助我,请提前感谢!

1 个答案:

答案 0 :(得分:0)

您每次都在onReceive的第一行创建一个新列表:ArrayList discoveredBluetoothDevices = new ArrayList();

将您的清单带到外面并添加到其中。

应该在广播接收器之外声明arraylist和adapter,更新你的列表只需在你的适配器上调用notifyDataSetChanged。