Started using an EasyObjectCursor to avoid the screen jumping up and

down
This commit is contained in:
Alexandros Schillings
2014-04-09 16:54:16 +01:00
parent df3e945383
commit fc69b6daa6
4 changed files with 31 additions and 17 deletions
Binary file not shown.
@@ -6,6 +6,7 @@ import uk.co.alt236.btlescan.adapters.LeDeviceListAdapter;
import uk.co.alt236.btlescan.containers.BluetoothLeDeviceStore; import uk.co.alt236.btlescan.containers.BluetoothLeDeviceStore;
import uk.co.alt236.btlescan.util.BluetoothLeScanner; import uk.co.alt236.btlescan.util.BluetoothLeScanner;
import uk.co.alt236.btlescan.util.BluetoothUtils; import uk.co.alt236.btlescan.util.BluetoothUtils;
import uk.co.alt236.easycursor.objectcursor.EasyObjectCursor;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.ListActivity; import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@@ -39,12 +40,13 @@ public class MainActivity extends ListActivity {
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
final BluetoothLeDevice deviceLe = new BluetoothLeDevice(device, rssi, scanRecord, System.currentTimeMillis()); final BluetoothLeDevice deviceLe = new BluetoothLeDevice(device, rssi, scanRecord, System.currentTimeMillis());
mDeviceStore.addDevice(deviceLe);
final EasyObjectCursor<BluetoothLeDevice> c = mDeviceStore.getDeviceCursor();
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
mDeviceStore.addDevice(deviceLe); mLeDeviceListAdapter.swapCursor(c);
mLeDeviceListAdapter.replaceData(mDeviceStore.getDeviceList());
updateItemCount(mLeDeviceListAdapter.getCount()); updateItemCount(mLeDeviceListAdapter.getCount());
} }
}); });
@@ -119,7 +121,7 @@ public class MainActivity extends ListActivity {
@Override @Override
protected void onListItemClick(ListView l, View v, int position, long id) { protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothLeDevice device = mLeDeviceListAdapter.getItem(position); final BluetoothLeDevice device = (BluetoothLeDevice) mLeDeviceListAdapter.getItem(position);
if (device == null) return; if (device == null) return;
@@ -181,7 +183,7 @@ public class MainActivity extends ListActivity {
mDeviceStore.clear(); mDeviceStore.clear();
updateItemCount(0); updateItemCount(0);
mLeDeviceListAdapter = new LeDeviceListAdapter(this, mDeviceStore.getDeviceList()); mLeDeviceListAdapter = new LeDeviceListAdapter(this, mDeviceStore.getDeviceCursor());
setListAdapter(mLeDeviceListAdapter); setListAdapter(mLeDeviceListAdapter);
mBluetoothUtils.askUserToEnableBluetoothIfNeeded(); mBluetoothUtils.askUserToEnableBluetoothIfNeeded();
@@ -1,34 +1,38 @@
package uk.co.alt236.btlescan.adapters; package uk.co.alt236.btlescan.adapters;
import java.util.List;
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice; import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
import uk.co.alt236.bluetoothlelib.device.IBeaconDevice; import uk.co.alt236.bluetoothlelib.device.IBeaconDevice;
import uk.co.alt236.bluetoothlelib.util.IBeaconUtils; import uk.co.alt236.bluetoothlelib.util.IBeaconUtils;
import uk.co.alt236.btlescan.R; import uk.co.alt236.btlescan.R;
import uk.co.alt236.btlescan.util.Constants; import uk.co.alt236.btlescan.util.Constants;
import uk.co.alt236.easycursor.objectcursor.EasyObjectCursor;
import android.app.Activity; import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
// Adapter for holding devices found through scanning. // Adapter for holding devices found through scanning.
public class LeDeviceListAdapter extends ArrayAdapter<BluetoothLeDevice> { public class LeDeviceListAdapter extends SimpleCursorAdapter {
private final LayoutInflater mInflator; private final LayoutInflater mInflator;
private final Activity mActivity; private final Activity mActivity;
public LeDeviceListAdapter(Activity activity, List<BluetoothLeDevice> list) { public LeDeviceListAdapter(Activity activity, EasyObjectCursor<BluetoothLeDevice> cursor) {
super(activity, R.layout.list_item_device, list); super(activity, R.layout.list_item_device, cursor, new String[0], new int[0], 0);
mInflator = activity.getLayoutInflater(); mInflator = activity.getLayoutInflater();
mActivity = activity; mActivity = activity;
} }
public void replaceData(List<BluetoothLeDevice> list){ @SuppressWarnings("unchecked")
// TODO: THIS IS REALLY HACKY AND BAD FOR PERFORMACE... @Override
clear(); public EasyObjectCursor<BluetoothLeDevice> getCursor(){
addAll(list); return ((EasyObjectCursor<BluetoothLeDevice>) super.getCursor());
}
@Override
public BluetoothLeDevice getItem(int i){
return getCursor().getItem(i);
} }
@Override @Override
@@ -60,7 +64,7 @@ public class LeDeviceListAdapter extends ArrayAdapter<BluetoothLeDevice> {
viewHolder = (ViewHolder) view.getTag(); viewHolder = (ViewHolder) view.getTag();
} }
final BluetoothLeDevice device = getItem(i); final BluetoothLeDevice device = getCursor().getItem(i);
final String deviceName = device.getName(); final String deviceName = device.getName();
final double rssi = device.getRssi(); final double rssi = device.getRssi();
@@ -16,6 +16,7 @@ import uk.co.alt236.bluetoothlelib.device.IBeaconDevice;
import uk.co.alt236.bluetoothlelib.util.ByteUtils; import uk.co.alt236.bluetoothlelib.util.ByteUtils;
import uk.co.alt236.bluetoothlelib.util.IBeaconUtils; import uk.co.alt236.bluetoothlelib.util.IBeaconUtils;
import uk.co.alt236.btlescan.util.CsvWriterHelper; import uk.co.alt236.btlescan.util.CsvWriterHelper;
import uk.co.alt236.easycursor.objectcursor.EasyObjectCursor;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
@@ -60,6 +61,13 @@ public class BluetoothLeDeviceStore {
return writer; return writer;
} }
public EasyObjectCursor<BluetoothLeDevice> getDeviceCursor(){
return new EasyObjectCursor<BluetoothLeDevice>(
BluetoothLeDevice.class,
getDeviceList(),
"address");
}
public List<BluetoothLeDevice> getDeviceList(){ public List<BluetoothLeDevice> getDeviceList(){
final List<BluetoothLeDevice> methodResult = new ArrayList<BluetoothLeDevice>(mDeviceMap.values()); final List<BluetoothLeDevice> methodResult = new ArrayList<BluetoothLeDevice>(mDeviceMap.values());