请问在android开发中怎样将蓝牙搜索的设备放在ListView中呢 5
展开全部
public class DeviceListAdapter extends BaseAdapter {
private List<BluetoothDevice> mDeviceList;
private LayoutInflater mLayoutInflater;
private int mLayoutId;
private String pairInfo;
public DeviceListAdapter(Context context, List<BluetoothDevice> deviceList, int layoutId){
this.mDeviceList = deviceList;
this.mLayoutInflater = LayoutInflater.from(context);
this.mLayoutId = layoutId;
}
@Override
public int getCount() {
return mDeviceList.size();
}
@Override
public Object getItem(int position) {
return mDeviceList.get(position);
}
@Override
public long getItemId(int position) {
return mDeviceList.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
BluetoothDevice bluetoothDevice = mDeviceList.get(position);
Device device = null;
if(null == convertView){
device = new Device();
convertView = mLayoutInflater.inflate(mLayoutId, null);
device.mDeviceNameTV = (TextView) convertView.findViewById(R.id.deviceNameTV);
device.mIsPairTV = (TextView) convertView.findViewById(R.id.isPairTV);
device.mMacAddressTV = (TextView) convertView.findViewById(R.id.marAddressTV);
convertView.setTag(device);
}else {
device = (Device) convertView.getTag();
}
try {
device.mDeviceNameTV.setText(bluetoothDevice.getName());
} catch (Exception e) {
device.mDeviceNameTV.setText("null");
}
if(bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED){
pairInfo = "已配对";
}else {
pairInfo = "未配对";
}
device.mIsPairTV.setText(pairInfo);
device.mMacAddressTV.setText(bluetoothDevice.getAddress());
return convertView;
}
}
private List<BluetoothDevice> mDeviceList;
private LayoutInflater mLayoutInflater;
private int mLayoutId;
private String pairInfo;
public DeviceListAdapter(Context context, List<BluetoothDevice> deviceList, int layoutId){
this.mDeviceList = deviceList;
this.mLayoutInflater = LayoutInflater.from(context);
this.mLayoutId = layoutId;
}
@Override
public int getCount() {
return mDeviceList.size();
}
@Override
public Object getItem(int position) {
return mDeviceList.get(position);
}
@Override
public long getItemId(int position) {
return mDeviceList.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
BluetoothDevice bluetoothDevice = mDeviceList.get(position);
Device device = null;
if(null == convertView){
device = new Device();
convertView = mLayoutInflater.inflate(mLayoutId, null);
device.mDeviceNameTV = (TextView) convertView.findViewById(R.id.deviceNameTV);
device.mIsPairTV = (TextView) convertView.findViewById(R.id.isPairTV);
device.mMacAddressTV = (TextView) convertView.findViewById(R.id.marAddressTV);
convertView.setTag(device);
}else {
device = (Device) convertView.getTag();
}
try {
device.mDeviceNameTV.setText(bluetoothDevice.getName());
} catch (Exception e) {
device.mDeviceNameTV.setText("null");
}
if(bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED){
pairInfo = "已配对";
}else {
pairInfo = "未配对";
}
device.mIsPairTV.setText(pairInfo);
device.mMacAddressTV.setText(bluetoothDevice.getAddress());
return convertView;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询