我有一个应用程序可以发现蓝牙设备并使用它们填充listView。问题是它只填充第一个位置(当接收新的bluetoth设备取代第一个位置时)
public void addItem(VSMDevice entity) {
if (entity == null)
return;
// Update the information available int the advertising frame if the device has been already discovered
if (data.contains(entity)) {
// Update the advertising information only
final VsmDescriptor desc = data.get(data.indexOf(entity)).getDeviceDescriptor();
desc.setAdvertisementInfo(entity.getRssi(), entity.getDeviceDescriptor().scanRecord());
desc.setAdvertisingMode(entity.getDeviceDescriptor().advertisingMode());
}
// Add a new discovered device
else {
data.add(entity);
}
notifyDataSetChanged();
}
这是addItem代码
这是adapterCode
public class DeviceListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<VSMDevice> data;
public DeviceListAdapter(Context context) {
data = new ArrayList<>();
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
}
public void clearItems() {
data.clear();
notifyDataSetChanged();
}
public void addItem(VSMDevice entity) {
if (entity == null)
return;
// Update the information available int the advertising frame if the device has been already discovered
if (data.contains(entity)) {
// Update the advertising information only
final VsmDescriptor desc = data.get(data.indexOf(entity)).getDeviceDescriptor();
desc.setAdvertisementInfo(entity.getRssi(), entity.getDeviceDescriptor().scanRecord());
desc.setAdvertisingMode(entity.getDeviceDescriptor().advertisingMode());
}
// Add a new discovered device
else {
data.add(entity);
}
notifyDataSetChanged();
}
public Object getItem(int position) {
if (data.size() >= position + 1) {
return data.get(position);
}
return null;
}
public List<VSMDevice> getItems() {
return data;
}
public long getItemId(int position) {
return position;
}
public int getCount() {
return data.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
DeviceListViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_device, null);
holder = new DeviceListViewHolder();
holder.deviceName = (TextView) convertView.findViewById(R.id.textView_device_name);
holder.deviceIcon = (ImageView) convertView.findViewById(R.id.imageView_icon);
holder.connectionState = (TextView) convertView.findViewById(R.id.textView_connect);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (DeviceListViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
VsmDescriptor device = data.get(position).getDeviceDescriptor();
holder.deviceName.setText(device.name());
if (data.get(position).isConnected()) {
holder.connectionState.setBackgroundColor(Color.RED);
holder.connectionState.setText("DISCONNECT");
} else {
holder.connectionState.setBackgroundColor(convertView.getContext().getResources().getColor(R.color.buttonColor));
holder.connectionState.setText("CONNECT");
}
return convertView;
}
}