Android 开发,ListView点击事件传数据
禾曦走廊 2017-11-15 05:25:30 listview放搜索到的蓝牙设备,设置监听事件,点击 一个蓝牙就连接跳转到下一个界面。问题 是点击后就 闪退,求大神帮忙解决下,拜托了。
代码如下
public class Main5Activity extends AppCompatActivity {
ListView mListView;
ArrayAdapter<String>arrayAdapter;
ArrayList<String> datas = new ArrayList<String>();
ArrayList<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();
BluetoothAdapter mBluetoothAdapter;
// 创建一个BroadcastReceiver进行action_found
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// 当发现发现一个设备
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// 从信息传到蓝牙设备对象
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 添加名称和数组适配器在列表视图中显示的地址
datas.add(device.getName() + "\n" + device.getAddress());
devices.add(device);
ArrayAdapter<String> adapter = (ArrayAdapter<String>) mListView.getAdapter();
adapter.notifyDataSetChanged();
}
}
};
private void initData() {
if (mBluetoothAdapter==null) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
if (!mBluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
} //看蓝牙能否使用
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) // 如果有配对设备
{
for (BluetoothDevice device : pairedDevices)// 循环配对设备
{
datas.add(device.getName() + "\n" + device.getAddress());// 添加名称和数组适配器在列表视图中显示的地址
devices.add(device);
}
ArrayAdapter<String> adapter = (ArrayAdapter<String>) mListView.getAdapter();
adapter.notifyDataSetChanged();
}
}
@Override
//打开或关闭蓝牙
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode==RESULT_OK)//ressultCode是Activity的一个常量
{
Toast.makeText(this, "蓝牙打开成功", Toast.LENGTH_SHORT).show();
}else if (resultCode==RESULT_CANCELED) {
Toast.makeText(this, "蓝牙打开失败", Toast.LENGTH_SHORT).show();
}
}
/**
* 检测蓝牙设备。
* 1、如果我们的手机没有开启蓝牙功能,提示用户开启蓝牙<br/>
* 2、将系统已经绑定的蓝牙设备添加到listView中
*/
public void checkBluetooth(View view){
initData();
}
/**
* 搜索蓝牙设备,将搜索到的蓝牙设备添加到listView
*/
public void searchBluetooth(View view){
// 登记BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // 别忘了注销在onDestroy
boolean startDiscovery = mBluetoothAdapter.startDiscovery();
if (startDiscovery) {
datas.clear();
devices.clear();
Toast.makeText(this, "开始扫描设备...", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "扫描设备失败", Toast.LENGTH_SHORT).show();
}
}
/* 与设备配对
*
* @param device
*/
public void createBond (BluetoothDevice device) {
try {
Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
createBondMethod.invoke(device);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
protected void onCreate(Bundle savedInstanceState) {
Main5Activity.super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();//获取蓝牙默认的适配器
mListView= (ListView) findViewById(R.id.lv);//获取ListView组件
// 为listview设置字符换数组适配器
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
// 为listView绑定适配器
mListView.setAdapter(arrayAdapter);
// 为listView设置item点击事件侦听
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BluetoothDevice bluetoothDevice = devices.get(position);
Intent intent = new Intent();
intent.setClass(Main5Activity.this, Main6Activity.class);
intent.putExtra("device", bluetoothDevice);
startActivity(intent);
}
});
}
}