同一适配器obj多次显示,但是为什么呢?

时间:2019-05-29 11:42:29

标签: java android android-listview android-arrayadapter

我做了一个自定义的Listview和Listviewadapter。不知何故,同一数据在Listview中多次显示,但我不知道为什么。

我尝试对其进行调试,但似乎并没有对其进行两次添加。

如您所见,我使用.contains控制了适配器的输入,但这无济于事。

BrodcastReceiver

   private BroadcastReceiver BR_BT_Device= new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           String action_BR_BT_Device= intent.getAction();

            if(action_BR_BT_Device.equals(BluetoothDevice.ACTION_FOUND))
            {
                BluetoothDevice device = 
      intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (!device.equals(null)) {
                    String sDevice_Address = device.getAddress();
                    if (!(sDevice_Address == null)) {
                        if (device.getName() == null){
                            mDeviceName = "Kein Name";
                        }
                        else {
                            mDeviceName = device.getName();
                        }
                        cBT_DeviceList mDevice = new 
       cBT_DeviceList(mDeviceName, sDevice_Address);

                        if (!(cBT_popup.mBTDevice.contains(mDevice))) {
                            cBT_popup.mBTDevice.add(mDevice);

       cBT_popup.cBTDeviceListAdapter.notifyDataSetChanged();
                        }
                    }
                }
                Log.d("Resiver", "onReceive: "+device.getAddress());
            }
        }
    };

Listview obj的活动

    public class cBT_popup extends MainActivity {
        public static ArrayList<cBT_DeviceList> mBTDevice = new
                ArrayList<cBT_DeviceList>();
        public ListView lv_devices;
        public static cBT_DeviceList_Adapter cBTDeviceListAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.bt_popup);
            lv_devices = findViewById(R.id.lv_devices);
            cBTDeviceListAdapter = new cBT_DeviceList_Adapter(this,
                    R.layout.lrv_bt_listview, mBTDevice);
            lv_devices.setAdapter(cBTDeviceListAdapter);
            lv_devices.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

如果您需要更多信息,请告诉我。

如果这很重要:不可能突出显示所选项目,则不知道为什么。

用于广播的IntentFilter

´´´     IntentFilter BT_Device_filter =新
    IntentFilter(BluetoothDevice.ACTION_FOUND);

´´´

1 个答案:

答案 0 :(得分:1)

可能在同一蓝牙设备上多次调用onRecieve。

尝试一下...替换

if (!(cBT_popup.mBTDevice.contains(mDevice))) {
        cBT_popup.mBTDevice.add(mDevice);

        cBT_popup.cBTDeviceListAdapter.notifyDataSetChanged();
    }

boolean alreadyExist = false;
    for(cBT_DeviceList mBTDeviceObj : mBTDevice){
        if(mDevice.getName().equals(mBTDeviceObj.getName())){
            alreadyExist = true;
        }
    }
    if (!alreadyExist) {
        cBT_popup.mBTDevice.add(mDevice);
        cBT_popup.cBTDeviceListAdapter.notifyDataSetChanged();
    }