2016-04-09 5 views
0

同じXMLファイル内で検出されペアリングされたBluetoothデバイスの場合、ListViewが2つありますが、OnItemClickListenerはペアデバイスのクリックに反応しますListView。私はListViewの両方に自分自身のOnItemClickListenerを持っているのだろうかと思います。実際には私はそのオプションを成功せずに試みましたが、たぶん私はちょうど間違いを犯しました。AdapterView.OnItemClickListenerが別のリストビューで機能しない

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"/> 

ビッグありがとう!

編集:このヘルプが、AndroidのメーカーのAndroidモニターは、私がListViewでアイテムを押した後にこのメッセージを与える場合、私は知らない。ViewPostImeInputStage ACTION_DOWN

答えて

0

次の2つを使用する必要がありますので、はいuは2 OnClickListenerを使用する必要がありますリストビュー。

+0

ありがとうございました!しかし、私はまだ発見されたデバイス 'ListView'のクリックを登録することができません – jakeheik90

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); 
    } 
}; 
+0

返信いただきありがとうございます – jakeheik90

関連する問題