• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface and constructor

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Im still very fresh in java
help needed
the question is within the coding comment
Please kindly advise and correct me if i'm wrong... thanks
*****************************
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class TryActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button pushButton = (Button)findViewById(R.id.push_button);

View.OnClickListener xx =new View.OnClickListener()
{
// as i know , the new keyword is for creating an object... View.OnClickListener() is a constructor... View.OnClickListener is a reference type
// the class View.OnClickListener is an interface
// the problem is an interface doesn't allow any object instantiated from it and that's why interface automatically don't have any any constructor implement in it own class
// but why the new View.OnClickListener() appear on above and creating an object with constructor... why???
// The API link is here http://developer.android.com/reference/android/view/View.OnClickListener.html

public void onClick(View v)
{
Intent intent = new Intent(TryActivity.this, Try2Activity.class);
startActivity(intent);
}

};

pushButton.setOnClickListener(xx);
}
}
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible to use this syntax ("new" with an interface) if you supply implementations of all methods required by the interface. This creates a so-called anonymous class (because it has no name). It's a shortcut to creating an actual class that implements the interface, and instantiating that. That's a feature of Java, not Android in particular.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooo okok.. i see i see... thanks
but where do the compiler find constructor View.OnClickListener() ?? is it a default constructor with onClick() method??
if i create it on this way as below, why must put the new keyword also?? can i code this without new keyword? why we need that anonymous class/object??
thanks
***************************
public class TryActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button pushButton = (Button)findViewById(R.id.push_button);
pushButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TryActivity.this, Try2Activity.class);
startActivity(intent);
}
});
}
}
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but where do the compiler find constructor View.OnClickListener() ? is it a default constructor with onClick() method?


Yes, the compiler inserts a default constructor into the newly created class.

if i create it on this way as below, why must put the new keyword also?


Because that's how you construct objects on Java.

can i code this without new keyword?


That's easy to test, no?

why we need that anonymous class/object?


You don't - nothing stops you from creating a regular, named class that implements the interface, and use that instead of an anonymous class. This is merely a convenient shortcut that lets you avoid that.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
one more question
i tried to change and modify but can't get the same result

question : how to code the same output with the different method which implements the View.OnClickListener without creating the anonymous class as below?
please show an example
thanks

***************************************
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class try1Activity extends Activity implements View.OnClickListener{


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button pushButton = (Button)findViewById(R.id.push_button);
// View.OnClickListener xx = null;

// pushButton.setOnClickListener(xx);

}

@Override
public void onClick(View v) {
Intent intent = new Intent(try1Activity.this, try2Activity.class);
startActivity(intent);

}
}
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pushButton.setOnClickListener(this) should do the trick.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wao... thanks guru... it runs perfectly...
Sorry for disturb again... i got one more question... which is

Im trying to get understand on the "this" keyword
for my understanding, the "this" keyword at here refer to current object whose method is being called.
my question are:
1) current object = object type of View.OnClickListener ?
2) why the compiler will choose only the 1st onClick method instead of 2nd onClick?

---------------------------------------------------------------------------------------

public class RedActivity extends Activity implements View.OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button greenButton = (Button)findViewById(R.id.green_button);

greenButton.setOnClickListener(this);
}
//chosen by compiler
public void onClick(View v) {
Log.w("1st",""+this+"");
}

//ignore by compiler
public void onClick(View v, int a) {
Log.w("2nd",""+this+"");
}
}
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) current object = object type of View.OnClickListener ?


Yes, because RedActivity -which is what "this" refers to- implements View.OnClickListener.

2) why the compiler will choose only the 1st onClick method instead of 2nd onClick?


Because that's the one that fulfills the View.OnClickListener interface.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the "this" keyword refer to current object whose method is being called



in the situation above
'this' refer to the object of type View.OnClickListener
then
whose method is being called... refer to.....???
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think I understand the question - the View.OnClickListener's method is called.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it i got it....
thanks for your help....
i've learn a lot... thanks again...
reply
    Bookmark Topic Watch Topic
  • New Topic