Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6910335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:48:04+00:00 2026-05-27T08:48:04+00:00

I made the android Bluetooth-Chat tutorial. Instead of using the OnClickListeners in the main

  • 0

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); 
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:48:04+00:00Added an answer on May 27, 2026 at 8:48 am

    Create an instance of mhandler in your MyCursorAdapter class

         public class MyCursorAdapter extends ResourceCursorAdapter implements  
        OnClickListener {
    
        private Handler mHandler;
    
        public void setMHandler(Handler mHandler){
            this.mHandler = mHandler;
    
        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"
    
                 Use your mhandler...
               // Here i want to the the same like in the MainActivity class. 
               // But how can I connect to the mHandler?         
        }
        }
        }
    

    And Reference it from your MainActivity

    private void fillData() {
      Cursor notesCursor = mDbHelper.fetchAllNotes();
     startManagingCursor(notesCursor);
    
    adapter = new MyCursorAdapter(this, notesCursor);
    adapter.setMHandler(mHandler); //Reference  
    setListAdapter(adapter);
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i made news applicaion in android using RSS feed and now i want to
Has anyone made use of ASE ( Android scripting enviroment ) for anything other
I made an application that is using Android SDK 2.1. This application uses bluetooth
i was made a bluetooth printer application (android based) for printing some text using
I made an android project using eclipse. It looks like: src/ ActivityMain.java lib/ Square.java
I made Android project on Windows 7 using Android SDK and Eclipse. After moving
I made an android game and I want to know how to save player
i made android application. In application i have web view where i want to
I have made an Android app where items can be purchased using in-app-billing .
I made an android game. Now i want to store game score of each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.