I saw this code when looking at an Android example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editNumber;
Button btnCall = (Button) this.findViewById( R.id.btnCall);
editNumber = (EditText) this.findViewById(R.id.editNumber);
btnCall.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
call();
}
});
// ...
}
Here:
new OnClickListener() {
public void onClick(View v) {
call();
}
}
is passed to setOnClickListener() as a parameter. What I don't understand is what code inside {...} does here? if new OnClickListener() calls the constructor, and the constructor returns an object, yes, object can be passed to method as a parameter, but what is:
{
public void onClick(View v) {
call();
}
}
doing here? It looks like a method definition?
Thanks a lot for the help!
As #Perception said, it is an anonymous inner class. btnCall.setOnClickListenter() is expecting an argument that has the type OnClickListener. You could instantiate a concrete reference to an OnClickListener and pass that as an argument but if you are never going to refer to it again, sometimes it is easier to simply make an anonymous inner class.
Related
I have this:
final Button myButton = (Button) findViewById(R.id.mybutton);
How can I pass myButton into a method as a parameter? So I can use it inside a method to do something.
Like, I can call my method hideThis() as MyClass.hideThis(myButton) and it hides the given object.
So, how can i declare the method with the element as the parameter?
Well, you'd better refer to documentation and Java tutorials.
But in your case, you can try:
public class VisibilityManager {
public static void hide(View view){
view.setVisibility(View.GONE);
}
public static void show(View view){
view.setVisibility(View.VISIBLE);
}
}
And then call this like:
final Button mybutton = (Button) findViewById(R.id.mybutton);
// you can do it because `Button extends View`.
VisibilityManager.hide(mybutton);
Read more: View#setVisibility(int)
I want to modularize the usage of my class but I have problem in passing function. I want to be able to pass an OnClickListener from 1 activity to this CoachmarkActivity.
I tried 2 different method:
1. Passing an OnClickListener to Intent
2. Passing a class, FollowUpClass, implements Serializable, which has method onClick.
You can see the code below. It is not complete code, but you should be able to comprehend this.
public class CoachmarkActivity extends Activity {
public static final String RES_LAYOUT = "RES-LAYOUT";
public static final String LISTENER = "LISTENER";
public static final String FOLLOW_UP = "FOLLOW-UP";
#Override protected void onCreate(Bundle savedInstance) {
setContentView(getIntent.getIntExtra(RES_LAYOUT, R.layout.activity_default))
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
// 1ST ATTEMPT
// I want to modularize this
OnClickListener onClickPassedFromIntent = (OnClickListener) getIntent().getSerializableExtra(LISTENER);
button1.setOnClickListener(onClickPassedFromIntent);
// 2ND ATTEMPT
final FollowUpListener folllowup = (FollowUpListener) getIntent().getSerializableExtra(FOLLOW_UP);
button2.setOnClickListener(new OnClickListener() {
#Override void onClick() {
// !! Here is error, exception thrown
folllowup.onClick();
}
});
}
/**
* Public method to be used in other activity.
* Invocation wanna be:
* CoachmarkActivity.startThisActivity(getActivity(), R.layout.coachmark1, new OnClickListener() {
* #Override void onClick() {
* // Do something
* }
* });
*/
public static void startThisActivity(Context context, int resId, OnClickListener listener) {
Intent intent = new Intent(context, CoachmarkActivity.class);
intent.putExtra(RES_LAYOUT, resId);
// !! Line below is error, onClickListener is not serializable, no method can accomadate below
intent.putExtra(LISTENER, listener);
context.startActivity(intent);
}
/**
* Public method to be used in other activity.
* Invocation wanna be:
* CoachmarkActivity.startThisActivity(getActivity(), R.layout.coachmark1, new FollowUpListener() {
* #Override void onClick() {
* // Do something
* }
* });
*/
public static void startThisActivity(Context context, int resId, FollowUpListener folllowup) {
Intent intent = new Intent(context, CoachmarkActivity.class);
intent.putExtra(RES_LAYOUT, resId);
intent.putExtra(FOLLOW_UP, followup);
context.startActivity(intent);
}
}
The abstract class:
public abstract class FollowUpListener implements Serializable {
public abstract void onClick();
}
The problems are stated in the comment in source code above, with tag "!!" (Just CTRL+F "!!"). What I want to do is like passing a Delegate object (function in form of variable) in C#, but in Android Java.
Any idea? Thanks.
You are trying to add a Serializable extra to your Intent, but OnClickListener does not implement that interface. You can achieve what you want by creating a class that implements both of the interfaces you need.
private class SerializableClickListener implements View.OnClickListener, Serializable {
#Override public void onClick() {
// TODO handle click
}
}
However, just because you can doesn't mean you should. Sending a click listener to another activity is a horrible code smell, and you should really rethink how you could do this via Intents/Broadcasts.
I tried to pass the OnlclickListener and I couldn't. then I tried this solution.
I made a static click listener variable in a GlobalData class
public static View.OnClickListener btn;
Then when I call the startactivity to go to another activity I did this.
GlobalData.btn = new View.OnClickListener() {
#Override
public void onClick(View v) {
//Listern action
}
};
c.startActivity(new Intent(c, DialogActivity.class));
Then in the second activity, I can set the static listener reference which I used to assign a listener object in the first activity.
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(GlobalData.btn!=null){
GlobalData.btn1.onClick(v);
}
finish();
}
});
I didn't use it directly as a parameter so I can do other stuff in the second activity listener. this worked for me.
But you have to think more because you are using a static reference. this is not a 100% solution. but it's worth trying.
I have a variable that I get from shared preferences when I load the app.
I first initialize the variable
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String camera_type = booth_preferences.getString("camera_key", "back");
Then later down the line, I get that variable and do something with it
if(camera_type.equals("front")){
//do something
} else if(camera_type.equals("ext")){
//do something
} else {
//do something
}
Now, directly after that if statement, I have an onclick listener that is supposed to change and update that preference.
camera_button_front.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do something
edit_preferences.putString(camera_key, "front").commit();
}
});
camera_button_back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do something
edit_preferences.putString(camera_key, "back").commit();
}
});
camera_button_ext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do something
edit_preferences.putString(camera_key, "ext").commit();
}
});
But when I try to change the variable camera_type I get errors stating "cannot assign a value to final variable 'camera_type'".
camera_button_ext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do something
edit_preferences.putString(camera_key, "ext").commit();
camera_type = "ext";
}
});
I've ever reinstated the variable after the onclick hoping it would over write the variable completely.
camera_button_ext.setOnClickListener(new View.OnClickListener() {
String camera_type;
public void onClick(View v) {
//do something
edit_preferences.putString(camera_key, "ext").commit();
camera_type = "ext";
}
});
If I were to remove the final then I'm not able to use the variable in the if statement.
I'm new to Java, so this should be a simple answer, I'm just not sure which combination of wrong I'm doing.
You should make your variable a non-final field. Then you can use it within the onClick (and all other methods of the class) and also reassign it as you wish.
private String camera_type; // member variable
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
camera_type = booth_preferences.getString("camera_key", "back");
You cannot reassign value to a final variable. To make it accessible inside your onClick method, you could make camera_type a member variable of your activity.
You can make one element massive
String[] cameraType = new String[0];
and use
if("some_String".equals(cameraType[0])){
//do smth
}
This might help someone also: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
I am calling method from another class in onPostExecute().
I assume that onPostExecute() is called after doInBackground(String... params) and that is right, according to documentation and debugger.
Calling the method:
protected void onPostExecute(String result) {
CreateHangOut crtHO = new CreateHangOut();
crtHO.createHangOut(result);
}
Part of method called, causing NPE (first line of method):
public void createHangOut(String location) {
String city=autocompleteTV.getText().toString();
}
Autocomplete TextView(autocompleteTV) is initialized onCreate of the activity.
Here is how I call AsyncTask:
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new
HTTPRequest().execute((autocompleteTV.getText()).toString());
}
});
Method called onCreate (of activity from where button is clicked) :
private void initialize() {
gAPI= new GoogleAPIAutocomplete();
autocompleteTV = (AutoCompleteTextView)
findViewById(R.id.crtHOLocOptionsTV);
setUpAutocomplete();
create = (Button) findViewById(R.id.crtHOCreateBtn);
name =(EditText) findViewById(R.id.crtHONameET);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new
HTTPRequest().execute((autocompleteTV.getText()).toString());
}
});
}
Because createHangOut method is in CreateHangOut Activity so no need to create new object for accessing method just call it using method name if class which extends AsyncTask class is inner class of CreateHangOut :
protected void onPostExecute(String result) {
CreateHangOut.this.createHangOut(result);
}
Why can I not pass this as Context in following code?
I am getting error
The constructor DataManager(new View.OnClickListener(){}) is undefined
I am new to android programming, so sorry if question is weird!!
Any help would be great.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("vkx", "clk insert");
String qq="insert into users(id,name,password) values ('6','usrs','passs')";
DataManager dm=new DataManager(this);
dm.SqliteExecutenonquery(qq);
Log.d("vkx", "clk insert done");
}
});
I have my DataManger class
public DataManager(Context cont) {
super(cont, DBname, null, DBver);
}
....
So what you are doing is when instantiating DataManager, You are not passing a context as a paramater but actually your instance of OnClickListener.
DataManager dm=new DataManager(YOUR_CLASS_NAME.this);
YOUR_CLASS_NAME should be the Activity class name. ie
class MyClass extends Activity{
public onCreate(Bundle savedInstanceState){
Button btn = new Button();
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
DataManager dm=new DataManager(MyClass.this);
}
});
}
}
Give this a try and let me know how it goes
Just write MyClassName.this, assuming that 'MyClassName' is the Name of your Activity.
before setOnClickListener add this line:
final Context ThisContext = this;
and in your listener use this
DataManager dm=new DataManager(ThisContext);
You have written anonymous class new OnClickListener() {
There is no constructor for DataManager class with OnClickListener as type parameter.
I think instead of this, you need use getApplicationContext();
"this" is a reference to the current instance of the containing class.
Where you have used "this", it refers to an instance of an anonymous inner class of type onClickListener.
I believe that you might be wanting to pass the context of the Activity defining the onClickListener? If so, use MyActivity.this, where MyActivity is the name of the Activity class.