I made the android Bluetooth-Chat tutorial. Instead of using the OnClickListeners in the main acitivity, I want to use the OnClickListeners in an other class (MyCursorAdapter.class).
I think its a very simple java problem. But I don’t know how to implement this (how can I use mHandler from the other activity in my class).
Can anybody helpe me?
Cheers Felix
Source in the MainActivity.class (is working well)
public class MainActivity extends ListActivity {
private BluetoothService mChatService = null;
...
private void setupChat() {
mButton1 = (Button) findViewById(R.id.button1);
mButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
byte[] out = {32};
mChatService.write(out);
mChatService = new BluetoothService(this, mHandler);
}
});
}
Source In MyCursorAdapter.class (here I want to implement the same function)
public class MyCursorAdapter extends ResourceCursorAdapter implements OnClickListener {
...
private class OffItemClickListener implements OnClickListener {
private int position;
public OffItemClickListener(int position) {
super();
this.position = position;
}
@Override
public void onClick(View v) {
mCursor.moveToPosition(position);
// byte[] out = {32};
// MainAcitivity.talkChat(out);
// error: "non-static variable this cannot be referenced from a static context"
// Here i want to the the same like in the MainActivity class.
// But how can I connect to the mHandler?
}
}
}
Comment:
Instance of MyCursorAdapter in MainActivity
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
adapter = new MyCursorAdapter(this, notesCursor);
setListAdapter(adapter);
}
Comment comment:
Or how can I call this MainActivity method from MyCursorAdapter?:
public void talkChat(byte[] out) {
mChatService.write(out);
mChatService = new BluetoothService(this, mHandler);
}
Create an instance of mhandler in your MyCursorAdapter class
And Reference it from your MainActivity
}