wakaka
wakaka

Reputation: 553

How do I refer to an Interface in an inner method in Java

I am still a newbie in Java programming and I just learnt how to create my own listener and I am stuck at the following. I have a class that implements an interface with I created in another class. The code is as below.

public class MainActivity extends Activity  implements AsyncClassSocket.Listener{   
AsyncClassSocket thesocketclass = new AsyncClassSocket();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    thesocketclass.registerListener(this);

    //More codes
    }
}

@Override
public void onReplyFromServer(boolean state) {
    //Codes
}

The above codes is working fine. However, when I put the codes into an inner method like below

showProgressDialog.setOnClickListener(new View.OnClickListener() {          
        @Override
        public void onClick(View v) {
            thesocketclass = new AsyncClassSocket();
            thesocketclass.registerListener(this); <---- Problem
            showProgressDialog();   
            thesocketclass.execute();               
        }
    });     
} 

I can no longer refer to my interface because it will now refer to View.OnClickListener(). My question would be how do I refer back to the interface in an inner method?

Upvotes: 0

Views: 59

Answers (1)

Tseng
Tseng

Reputation: 64170

Simply use MainActivity.this to get the main activity reference.

Upvotes: 3

Related Questions