2017-10-07 18 views
1

我试图建立一个android应用程序,扫描蓝牙设备并将它们添加到recyclerview。当我尝试清除我的ArrayList并在适配器中向它添加新数据时,我最终得到了一个空数组列表。当我不清除列表我结束了一个包含每个设备两次这样的列表:[**:71:9F:B6:59:**, **:71:9F:B6:59:**]在logcat中。我也认为我在这里用我的notifydatasetchanged调用做错了什么,因为我的recyclerview中没有任何显示。我敢肯定,这是简单的,我失踪,但我不能看到它。我发布了我的主要活动的代码以及我的recyclerview.adapter,希望有人能够向我指出问题。提前致谢。这是我的主要活动:这是写在科特林不是Java的找到重复的蓝牙设备。我在这里双重浸泡?也能够更新recyclerview

public class ToofActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { 

private static final String TAG = "ToofActivity"; 
private static final int REQUEST_COARSE_LOCATION = 222; 

private static BluetoothAdapter btAdapter; 

private TextView mStatusLabel; 
private Switch mStatusSwitch; 
private ProgressBar mProgressBar; 
private RecyclerView mDeviceRecylcer; 
private static ArrayList<BluetoothDevice> mDevices = new ArrayList<>(); 
private DeviceAdapter mAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_toof); 

    checkLocationPermission(); 

    btAdapter = BluetoothAdapter.getDefaultAdapter(); 

    mStatusLabel = (TextView) findViewById(R.id.status_view); 
    mStatusSwitch = (Switch) findViewById(R.id.status_switch); 
    mStatusSwitch.setChecked(btAdapter.isEnabled()); 
    mStatusSwitch.setOnCheckedChangeListener(this); 
    mProgressBar = (ProgressBar) findViewById(R.id.search_progress_bar); 
    mDeviceRecylcer = (RecyclerView) findViewById(R.id.device_recycler); 

    mDeviceRecylcer.setLayoutManager(new LinearLayoutManager(this)); 
    mAdapter = new DeviceAdapter(mDevices, getApplicationContext()); 
    mDeviceRecylcer.setAdapter(mAdapter); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.options_menu, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.searchOption: 
      DiscoveryTask discovery = new DiscoveryTask(btAdapter, this, mDevices, mProgressBar); 
      discovery.setOnCompleteListener(new DiscoveryTask.OnCompletedListener() { 
       @Override 
       public void onComplete() { 
        Log.d(TAG, "devices found : " + mDevices.toString()); 
        mAdapter.updateItems(mDevices); 
       } 
      }); 
      discovery.execute((Void) null); 
      break; 
    } 

    return true; 
} 

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    if (isChecked) { 
     btAdapter.enable(); 
     mStatusLabel.setText("Active"); 
    } else { 
     btAdapter.disable(); 
     mStatusLabel.setText("Inactive"); 
    } 
} 

public static void setDeviceList(ArrayList<BluetoothDevice> deviceList) { 
    mDevices = deviceList; 
} 

protected void checkLocationPermission() { 
    if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     ActivityCompat.requestPermissions(this, 
       new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 
       REQUEST_COARSE_LOCATION); 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case REQUEST_COARSE_LOCATION: { 
      if (grantResults.length <= 0 
        || grantResults[0] != PackageManager.PERMISSION_GRANTED) { 
       checkLocationPermission(); 
       break; 
      } 
     } 
    } 
} 
} 

,也是我的适配器类:

class DeviceAdapter(val mDevices: ArrayList<BluetoothDevice>, val mContext : Context) : RecyclerView.Adapter<DeviceAdapter.DeviceHolder>(){ 

fun updateItems(list: ArrayList<BluetoothDevice>){ 
    //mDevices.clear() 
    mDevices.addAll(list) 
    Log.d(TAG, "updating items : $mDevices") 
    notifyDataSetChanged() 
} 

override fun onBindViewHolder(holder: DeviceHolder, position: Int) { 
    holder.bindItems(mDevices.get(position)) 
} 

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): DeviceHolder?{ //return is not to be NULL! 
    val v = LayoutInflater.from(mContext).inflate(R.layout.device_item, parent, false) 
    return DeviceHolder(v) 
} 

override fun getItemCount(): Int { 
    return mDevices.size 
} 

inner class DeviceHolder(itemView: View) : RecyclerView.ViewHolder(itemView){ 
    val nameView = itemView.findViewById(R.id.nameView) as TextView 
    val addrView = itemView.findViewById(R.id.addressView) as TextView 

    fun bindItems(btDevice: BluetoothDevice){ 
     nameView.text = btDevice.name 
     addrView.text = btDevice.address 
    } 
} 

companion object { 
    val TAG = "DeviceAdapter" 
} 

} 

回答

3

的问题是,DeviceAdapter.mDevices是相同的对象ToofActivity.mDevices。所以当你清除它时,它变为空的,当你mDevices.addAll(mDevices)时,元素加倍。

DeviceAdapter的构造函数参数中删除mDevices。 在DeviceAdapter中为它创建一个新的ArrayList