Advertisement
Guest User

LeDeviceListAdapter

a guest
Sep 24th, 2013
14,870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. // Adapter for holding devices found through scanning.
  2. private class LeDeviceListAdapter extends BaseAdapter {
  3. private ArrayList<BluetoothDevice> mLeDevices;
  4. private LayoutInflater mInflator;
  5.  
  6. public LeDeviceListAdapter() {
  7. super();
  8. mLeDevices = new ArrayList<BluetoothDevice>();
  9. mInflator = DeviceScanActivity.this.getLayoutInflater();
  10. }
  11.  
  12. public void addDevice(BluetoothDevice device) {
  13. if(!mLeDevices.contains(device)) {
  14. mLeDevices.add(device);
  15. }
  16. }
  17.  
  18. public BluetoothDevice getDevice(int position) {
  19. return mLeDevices.get(position);
  20. }
  21.  
  22. public void clear() {
  23. mLeDevices.clear();
  24. }
  25.  
  26. @Override
  27. public int getCount() {
  28. return mLeDevices.size();
  29. }
  30.  
  31. @Override
  32. public Object getItem(int i) {
  33. return mLeDevices.get(i);
  34. }
  35.  
  36. @Override
  37. public long getItemId(int i) {
  38. return i;
  39. }
  40.  
  41. @Override
  42. public View getView(int i, View view, ViewGroup viewGroup) {
  43. ViewHolder viewHolder;
  44. // General ListView optimization code.
  45. if (view == null) {
  46. view = mInflator.inflate(R.layout.listitem_device, null);
  47. viewHolder = new ViewHolder();
  48. viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
  49. viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
  50. view.setTag(viewHolder);
  51. } else {
  52. viewHolder = (ViewHolder) view.getTag();
  53. }
  54.  
  55. BluetoothDevice device = mLeDevices.get(i);
  56. final String deviceName = device.getName();
  57. if (deviceName != null && deviceName.length() > 0)
  58. viewHolder.deviceName.setText(deviceName);
  59. else
  60. viewHolder.deviceName.setText(R.string.unknown_device);
  61. viewHolder.deviceAddress.setText(device.getAddress());
  62.  
  63. return view;
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement