Один и тот же адаптер obj показывался несколько раз, но почему? - PullRequest
0 голосов
/ 29 мая 2019

Я сделал собственный 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 = new
IntentFilter (BluetoothDevice.ACTION_FOUND);

'''

1 Ответ

1 голос
/ 29 мая 2019

Может быть onRecieve вызывается несколько раз для одного и того же устройства Bluetooth.

Попробуйте это ... Замените

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();
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...