soliman
soliman

Reputation: 105

Calling a non-static method in an android OnPreferenceClickListener

/**
 * A preference change listener to resynchronize a contact list
 *
 */
private static Preference.OnPreferenceClickListener resynchronizeContactsListener = new Preference.OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference preference) {
        new AlertDialog() {

        }
    }
}

In a code snippet such as the above, I need to call a non-static method, or create an AlertDialog(). Both of which I am having difficulty doing since the listener is a static method.

For example, the AlertDialog.Builder() constructor requires an android context object to be created, but since the method is static there is no context. I considered passing in the context as a parameter, however I am not sure where to do so without damaging the fact that I am overriding a method.

Thanks in advance

Upvotes: 2

Views: 1541

Answers (4)

Patel Dipak
Patel Dipak

Reputation: 1

Basically, A static method cannot call a non-static method, but we can use a reference, which include a non-static method to the static method.

public class StaticMethodTest{
void NonStaticMethod(){
    System.out.println("This is a non-sataic method.");
}
  static void StaticMethod(StaticMethodTest s){
   System.out.println("This is a static method.");
   s.NonStaticMethod();
}
public static void main(String[] args) {
    StaticMethodTest sObj=new StaticMethodTest();
    StaticMethod(sObj);  
}}

This is a java example, I think you can use this way to create a object, and use it reference into the static method. Hope it can help you.

Upvotes: 0

Charles Durham
Charles Durham

Reputation: 2495

Just remove the static keyword from your declaration.

A class or interface (note not the actual instantiated object, simply the class definition) is declared static when it's an inner class but has no reference to it's containing class. EG

public class Foo {
    public static class Bar {
    }
}

Bar cannot reference any of the state of Foo and can be instantiated independently with new Foo.Bar().

Upvotes: -1

Awanish Raj
Awanish Raj

Reputation: 2019

You can implement the Preference.OnPreferenceClickListener into your own class statically and initialise it from your activity code when ready. (I am assuming that you need the listener object to be static for some reason, you may do away with that!)

private static MyPrefListener myPrefListener = null;

private static class MyPrefListener implements Preference.OnPreferenceClickListener {

    private Context mContext;
    public MyPrefListener(Context context) {
        this.mContext = context;
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        //USE mContext as the context object here
        return false;
    }
}

Then in your Activity code, do this:

myPrefListener = new MyPrefListener(this);

I hope the structure of the code is clear.

Upvotes: 2

Kun
Kun

Reputation: 580

Basically, A static method cannot call a non-static method, but we can use a reference, which include a non-static method to the static method.

public class StaticMethodTest{
void NonStaticMethod(){
    System.out.println("This is a non-sataic method.");
}
  static void StaticMethod(StaticMethodTest s){
   System.out.println("This is a static method.");
   s.NonStaticMethod();
}
public static void main(String[] args) {
    StaticMethodTest sObj=new StaticMethodTest();
    StaticMethod(sObj);  
}}

This is a java example, I think you can use this way to create a object, and use it reference into the static method. Hope it can help you.

Upvotes: 2

Related Questions