getActivity()它不适用于适配器

时间:2015-08-30 02:32:49

标签: android listview adapter

这是我的代码。在itemClickListener的部分我有一个适配器的调用,但我想它需要另一个参数,问题是我不想它是什么

public class Lista_Activity extends Activity implements OnItemClickListener {

ArrayAdapter<String> listAdapter;
ListView listView;
BluetoothAdapter btAdapter;
Set<BluetoothDevice> devicesArray;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
IntentFilter filter;
BroadcastReceiver receiver;
String tag = "debugging";
Handler mHandler = new Handler(){
	@Override
	public void handleMessage(Message msg){
		// TODO Auto-generated method stub
		Log.i(tag, "in handler");
		super.handleMessage(msg);
		switch(msg.what){
		case SUCCESS_CONNECT:
			// DO something
			ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
			Toast.makeText(getApplicationContext(), "CONECTADO", 0).show();
			String s = "Conexion realizada";
			connectedThread.write(s.getBytes());
			Log.i(tag, "Conectado");
			break;
		case MESSAGE_READ:
			byte[] readBuf = (byte[])msg.obj;
			String string = new String(readBuf);
			Toast.makeText(getApplicationContext(), string, 0).show();
			break;
		}
	}
};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lista);
    init();
    if(btAdapter==null){
    	Toast.makeText(getApplicationContext(), "No se detectan dispositivos Bluetooth", 0).show();
    	finish();
    }
    else{
    	if(!btAdapter.isEnabled()){
    		turnOnBT();
    	}
    	
    	getPairedDevices();
    	startDiscovery();
    }


}
private void startDiscovery() {
	// TODO Auto-generated method stub
	btAdapter.cancelDiscovery();
	btAdapter.startDiscovery();
	
}
private void turnOnBT() {
	// TODO Auto-generated method stub
	Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
	startActivityForResult(intent, 1);
}
private void getPairedDevices() { //AGREGAMOS LOS DISPOSITIVOS CERCANOS A LA LISTA
	// TODO Auto-generated method stub
	devicesArray = btAdapter.getBondedDevices();
	if(devicesArray.size()>0){
		for(BluetoothDevice device:devicesArray){
			pairedDevices.add(device.getName());
			
		}
	}
}
private void init() {
	// TODO Auto-generated method stub
	listView=(ListView)findViewById(R.id.listView);
	listView.setOnItemClickListener(this);
	listAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
	listView.setAdapter(listAdapter);
	btAdapter = BluetoothAdapter.getDefaultAdapter();
	pairedDevices = new ArrayList<String>();
	filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
	devices = new ArrayList<BluetoothDevice>();
	receiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			
			if(BluetoothDevice.ACTION_FOUND.equals(action)){
				BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				devices.add(device);
				String s = "";
				for(int a = 0; a < pairedDevices.size(); a++){
					if(device.getName().equals(pairedDevices.get(a))){
						//append 
						s = "(Agregado)";
						break;
					}
				}
				listAdapter.add(device.getName()+s+"\n"+"Dirección MAC: ("+device.getAddress()+")");
			}
			else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
				// run some code
			}
			else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
				// run some code
			}
			else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
				if(btAdapter.getState() == btAdapter.STATE_OFF){
					turnOnBT();
				}
			}
		}
	};
	registerReceiver(receiver, filter);
	 filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
	registerReceiver(receiver, filter);
	 filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
	registerReceiver(receiver, filter);
	 filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
	registerReceiver(receiver, filter);
}


@Override
protected void onPause() {
	// TODO Auto-generated method stub
	super.onPause();
	unregisterReceiver(receiver);
}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_CANCELED){
			Toast.makeText(getApplicationContext(), "Bluetooth must be enabled to continue", Toast.LENGTH_SHORT).show();
			finish();
		}
	}
	private void cargarLista(){
	Adapter_lista adapter = new Adapter_lista(getActivity());
	
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new OnItemClickListener() {
    	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		
		if(btAdapter.isDiscovering()){
			btAdapter.cancelDiscovery();
		}
		if(listAdapter.getItem(arg2).contains("Agregado")){
			BluetoothDevice selectedDevice = devices.get(arg2);
			ConnectThread connect = new ConnectThread(selectedDevice);
			connect.start();
			Log.i(tag, "in click listener");
			
		}
		else{
			BluetoothDevice selectedDevice = devices.get(arg2);
			ConnectThread connect = new ConnectThread(selectedDevice);
			connect.start();
			Log.i(tag, "in click listener");
			Toast.makeText(getApplicationContext(), "Agregando dispositivo...", 0).show();
		}
		}
    	});
    }
	
	private class ConnectThread extends Thread {
	
		private final BluetoothSocket mmSocket;
	    private final BluetoothDevice mmDevice;
	 
	    public ConnectThread(BluetoothDevice device) {
	        // Usa un objeto temporal que despues se asigna a mmSocket,
	        BluetoothSocket tmp = null;
	        mmDevice = device;
	        Log.i(tag, "construct");
	        // Get a BluetoothSocket to connect with the given BluetoothDevice
	        try {
	            // MY_UUID es el UUID string de la app, tambien usado por el server code
	            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
	        } catch (IOException e) { 
	        	Log.i(tag, "get socket failed");
	        	
	        }
	        mmSocket = tmp;
	    }
	 
	    public void run() {
	        // Cancel discovery because it will slow down the connection
	        btAdapter.cancelDiscovery();
	        Log.i(tag, "connect - run");
	        try {
	            // Connect the device through the socket. This will block
	            // until it succeeds or throws an exception
	            mmSocket.connect();
	            Log.i(tag, "connect - succeeded");
	        } catch (IOException connectException) {	Log.i(tag, "connect failed");
	            // Unable to connect; close the socket and get out
	            try {
	                mmSocket.close();
	            } catch (IOException closeException) { }
	            return;
	        }
	 
	        // Do work to manage the connection (in a separate thread)
	   
	        mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
	    }
	 


		/** Will cancel an in-progress connection, and close the socket */
	    public void cancel() {
	        try {
	            mmSocket.close();
	        } catch (IOException e) { }
	    }
	}

	private class ConnectedThread extends Thread {
	    private final BluetoothSocket mmSocket;
	    private final InputStream mmInStream;
	    private final OutputStream mmOutStream;
	 
	    public ConnectedThread(BluetoothSocket socket) {
	        mmSocket = socket;
	        InputStream tmpIn = null;
	        OutputStream tmpOut = null;
	 
	        // Get the input and output streams, using temp objects because
	        // member streams are final
	        try {
	            tmpIn = socket.getInputStream();
	            tmpOut = socket.getOutputStream();
	        } catch (IOException e) { }
	 
	        mmInStream = tmpIn;
	        mmOutStream = tmpOut;
	    }
	 
	    public void run() {
	        byte[] buffer;  // buffer store for the stream
	        int bytes; // bytes returned from read()

	        // Keep listening to the InputStream until an exception occurs
	        while (true) {
	            try {
	                // Read from the InputStream
	            	buffer = new byte[1024];
	                bytes = mmInStream.read(buffer);
	                // Send the obtained bytes to the UI activity
	                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
	                        .sendToTarget();
	               
	            } catch (IOException e) {
	                break;
	            }
	        }
	    }
	 
	    /* Call this from the main activity to send data to the remote device */
	    public void write(byte[] bytes) {
	        try {
	            mmOutStream.write(bytes);
	        } catch (IOException e) { }
	    }
	 
	    /* Call this from the main activity to shutdown the connection */
	    public void cancel() {
	        try {
	            mmSocket.close();
	        } catch (IOException e) { }
	    }
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		
	}
}

该行是Adapter_lista适配器......

2 个答案:

答案 0 :(得分:0)

我认为错误在于

Adapter_lista adapter = new Adapter_lista(getActivity());

将其更改为

Adapter_lista adapter = new Adapter_lista(Lista_Activity.this);

希望它能解决你的问题。另请检查适配器构造函数及其参数。

答案 1 :(得分:0)

将getActivity()更改为Lista_Activity.this

在Fragments中使用getActivity()

相关问题