对于同一XML文件中已发现和配对的蓝牙设备,我有两个ListView
,但OnItemClickListener
仅对配对设备ListView
上的点击做出反应。我想我应该为OnItemClickListener
两个拥有ListView
。实际上我尝试了这个选项没有成功,但也许我在那里犯了一些错误。
private AdapterView.OnItemClickListener discoveredDeviceClickListener
= new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int position, long id) {
// Cancel discovery because it's costly and we're about to connect
cBluetoothAdapter.cancelDiscovery();
// Get the device MAC address, which is the last 17 chars in the View
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
// Start new activity and send the MAC address
Intent BluetoothTransferIntent = new Intent(MainActivity.this, BluetoothTransferActivity.class);
BluetoothTransferIntent.putExtra(EXTRA_DEVICE_ADDRESS, address);
startActivity(BluetoothTransferIntent);
// Set result and finish this Activity
//setResult(Activity.RESULT_OK, BluetoothTransferIntent);
}
};
pairedBluetoothDeviceList = (ListView) findViewById(R.id.paired_device_list);
pairedBluetoothDeviceList.setOnItemClickListener(discoveredDeviceClickListener);
discoveredBluetoothDeviceList = (ListView) findViewById(R.id.discovered_device_list);
discoveredBluetoothDeviceList.setOnItemClickListener(discoveredDeviceClickListener);
discoveredBluetoothDevices = new ArrayList();
discoveredBluetoothAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, discoveredBluetoothDevices);
pairedBluetoothDevices = new ArrayList();
pairedBluetoothAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, pairedBluetoothDevices);
discoveredBluetoothDeviceList.setAdapter(discoveredBluetoothAdapter);
pairedBluetoothDeviceList.setAdapter(pairedBluetoothAdapter);
和XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bluetooth"
tools:context="com.example.jake.bluetooth.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/discovered_device_list"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="#000000"
android:dividerHeight="1dp" >
</ListView>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/paired_device_list"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="#000000"
android:dividerHeight="1dp" >
</ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SetOnBluetooth"
android:id="@+id/SetOnBluetoothButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="133dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SetOffBluetooth"
android:id="@+id/SetOffBluetoothButton"
android:layout_above="@+id/SetOnBluetoothButton"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Query"
android:id="@+id/QueryButton"
android:layout_above="@+id/ReceiveButton"
android:layout_alignLeft="@+id/ReceiveButton"
android:layout_alignStart="@+id/ReceiveButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DiscoverDevices"
android:id="@+id/DiscoverDevicesButton"
android:layout_centerHorizontal="true"
android:layout_above="@+id/SetOffBluetoothButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Connect"
android:id="@+id/ConnectButton"
android:layout_alignTop="@+id/SetOnBluetoothButton"
android:layout_toLeftOf="@id/SetOnBluetoothButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Send"
android:id="@+id/SendButton"
android:layout_toLeftOf="@+id/SetOffBluetoothButton"
android:layout_alignTop="@+id/SetOffBluetoothButton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Receive"
android:id="@+id/ReceiveButton"
android:layout_alignTop="@+id/SetOnBluetoothButton"
android:layout_toRightOf="@+id/SetOnBluetoothButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Visible"
android:id="@+id/VisibleButton"
android:layout_above="@+id/DiscoverDevicesButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Exit"
android:id="@+id/ExitButton"
android:layout_above="@+id/VisibleButton"
android:layout_centerHorizontal="true"/>
提前非常感谢!
编辑:在我按下ListView
中的项目后,我不知道这个帮助,但Android Studio的Android监视器会显示此消息:ViewPostImeInputStage ACTION_DOWN
答案 0 :(得分:0)
是的,你必须使用两个OnClickListener,因为你必须使用两个列表视图。
答案 1 :(得分:0)
您没有按正确的顺序编写代码。我重新写了。一试吧
pairedBluetoothDevices = new ArrayList();
pairedBluetoothAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, pairedBluetoothDevices);
pairedBluetoothDeviceList = (ListView) findViewById(R.id.paired_device_list);
pairedBluetoothDeviceList.setAdapter(pairedBluetoothAdapter);
pairedBluetoothDeviceList.setOnItemClickListener(discoveredDeviceClickListener);
discoveredBluetoothDeviceList = (ListView) findViewById(R.id.discovered_device_list);
discoveredBluetoothDeviceList.setOnItemClickListener(discoveredDeviceClickListener);
discoveredBluetoothDevices = new ArrayList();
discoveredBluetoothAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, discoveredBluetoothDevices);
discoveredBluetoothDeviceList.setAdapter(discoveredBluetoothAdapter);
private AdapterView.OnItemClickListener discoveredDeviceClickListener
= new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int position, long id) {
// Cancel discovery because it's costly and we're about to connect
cBluetoothAdapter.cancelDiscovery();
// Get the device MAC address, which is the last 17 chars in the View
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
// Start new activity and send the MAC address
Intent BluetoothTransferIntent = new Intent(MainActivity.this, BluetoothTransferActivity.class);
BluetoothTransferIntent.putExtra(EXTRA_DEVICE_ADDRESS, address);
startActivity(BluetoothTransferIntent);
// Set result and finish this Activity
//setResult(Activity.RESULT_OK, BluetoothTransferIntent);
}
};