【问题标题】:Same Adapter obj shown multiple times, but why?多次显示相同的适配器 obj,但为什么呢?
【发布时间】:2019-05-29 11:42:29
【问题描述】:

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

我试着调试了一下,但是好像没有加双。

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

广播接收者

   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);

´´´

【问题讨论】:

  • 添加您的完整代码。你的 BroadcastReceiver 什么时候会被调用?
  • 我尝试上传完整代码,但 Stack overvlow 不希望我这样做。
  • 在这种情况下,我可以假设您的 onReceive 会为同一个 BluetoothDevice 调用多次。
  • 这是可能的,但是“.contains”应该阻止这个吗?
  • No cBT_popup.mBTDevice.contains(mDevice) 不会限制已添加的设备,因为 mDevice 是新创建的对象,它不会与您的列表项匹配。我会给出答案的代码,试一次。

标签: java android android-listview android-arrayadapter


【解决方案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();
    }

【讨论】:

  • 它将接收到的设备名称与所有添加的设备名称进行比较。
  • 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多