title: Listview and OnItemClickListener(Bluetooth device)
body: <p>I have a problem with OnItemClickListener.
  For example, i have 4 bluetooth devices in the list,i click on one element and the application try to pair only with the last element of the list not with the element that i clicked</p>
  
  <pre><code>import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import java.lang.reflect.Method;
  import java.util.ArrayList;
  
  
  public class bluetooth extends AppCompatActivity {
      private ListView listView;
      private ArrayList&lt;String&gt; mDeviceList = new ArrayList&lt;String&gt;();
      private BluetoothAdapter mBluetoothAdapter;
      private final static int REQUEST_ENABLE_BT=1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.bluetooth);
  
          listView = (ListView) findViewById(R.id.listView);
          mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
          mBluetoothAdapter.startDiscovery();
          IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
          registerReceiver(mReceiver, filter);
          if(!mBluetoothAdapter.isEnabled())
          {
              Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
              startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
              mBluetoothAdapter.startDiscovery();
              registerReceiver(mReceiver, filter);
          }
  
      }
  
      @Override
      protected void onDestroy() {
          unregisterReceiver(mReceiver);
          super.onDestroy();
      }
      private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
          public void onReceive(Context context, final Intent intent) {
              final BluetoothDevice device = intent
                  .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
              String action = intent.getAction();
              if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                  BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                  mDeviceList.add(bluetoothDevice.getName() + "\n"
                      + bluetoothDevice.getAddress());
                  listView.setAdapter(new ArrayAdapter&lt;String&gt;(context,
                      android.R.layout.simple_list_item_1, mDeviceList));
              }
              listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView&lt;?&gt; adapter, View view, int pos, long id){
                      String devicep=listView.getAdapter().getItem(pos).toString();
                      Log.i("device",devicep);
                      pairDevice(device);
                  }
              });
          }
      };
  
      private void pairDevice(BluetoothDevice b1) {
          try {
              Method method = b1.getClass().getMethod("createBond", (Class[]) null);
              method.invoke(b1, (Object[]) null);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>