I'm using a custom adapter which populates a ListView with a custom widget;
the list shows bluetooth devices within range. I use a BroadcastReceiver to
find the bluetooth devices and dynamically populate the list when I do. I
call a function, addDevice(), from my custom adapter, which updates its
internal list of devices and calls the notifyDataSetChanged().
However, my problem is that instead of listing the devices in the order of
which it was added, it constantly shift the order of the items in the list,
as new items are added (i.e. A bluetooth device will appear first in the
list and then after notifyDataSetChanged() is called, it will appear 3rd,
then maybe 6th, and back up to 4th in the list). Does anyone have any idea
as to why this may be and how I might be able to fix it? Note the Adapter
and BroadcastReciever are in different classes.
Custom Adapter (*DeviceAdpater.java*):
public View getView(int position, View convertView, ViewGroup parent)
{
CheckBoxItemView itemView;
// Check to see if the view is null and inflate it if so
if (convertView == null)
{
itemView = new CheckBoxItemView(app_context);
// Bind the data to the new widget from the data from given list
itemView.setName(device_list.get(position).getName());
itemView.setText(device_list.get(position).getText());
itemView.setIconImage(device_list.get(position).getIconImage());
// Set the listener for the checkbox
itemView.getCheckBox().setOnCheckedChangeListener(
device_list.get(position));
Log.i("DEBUG", "View is null---- " + itemView.getName());
}
else
{
itemView = (CheckBoxItemView) convertView;
}
return itemView;
}
public void addDevice(CheckBoxItemView device, String address)
{
// Add the new device and its bluetooth address to the lists
device_list.add(device);
address_list.add(address);
// Tell the view to refresh itself
notifyDataSetChanged();
}
Main activity class:
private void addBluetoothDevice(String deviceName, String address,
boolean pairedWith)
{
CheckBoxItemView itemRow;
// Create a new checkbox item for the list of bluetooth devices; the
// name of the widget component and its text will be the same as
// the bluetooth device's name
itemRow = new CheckBoxItemView(this);
itemRow.setName(String.format("%s", deviceName));
itemRow.setText(String.format("%s", deviceName));
// Check to see if this device has already been paired with in a
// previous session
if (pairedWith == true)
{
itemRow.setIconImage(R.drawable.sample_0);
}
else
{
// Unknown bluetooth device so use the default image
itemRow.setIconImage(R.drawable.sample_1);
}
// Add the new row featuring the bluetooth device to the adapter
device_adapter.addDevice(itemRow, address);
}
private class ActivityReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
String action;
BluetoothDevice btDevice;
// Get the intended action and process it
action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_FOUND))
{
// Get the bluetooth device that was found
btDevice =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If the device hasn't been paired (which would mean that it's
// already in list), go ahead and add it now
if (btDevice.getBondState() != BluetoothDevice.BOND_BONDED)
{
addBluetoothDevice(btDevice.getName(), btDevice.getAddress(),
false);
}
}
}
}
Tracing through the steps, I also find that sometimes when a device is added
and notifyDataSetChanged() is called, getView will not be called for that
new device. I'm not sure if this may be related to the random list
population or if it's a bluetooth hardware thing (the bluetooth in range are
from other computers in my room and custom created hardware).
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en