user387184
user387184

Reputation: 11053

accessing a calling activity's method from within an object it created

MyI don't understand why I get a compile error for this:

   public class Main_screen extends ListActivity {

       List<Object> completeList;
       private My_ArrayAdapter adapter;


    public void onCreate(Bundle icicle) {
       super.onCreate(icicle);
       completeList = getCompleteList();
       adapter = new My_ArrayAdapter(this, completeList);
       setListAdapter(adapter);
       }

    public void doSth() {
                ...
        }
    }

and in My_ArryAdapter:

 public class My_ArrayAdapter extends ArrayAdapter<Object> {

        private final List<Object> list;
        private final Activity context;


        public My_ArrayAdapter(Activity context, List<Object> list) {
           this.context = context;
           this.list = list;

        }

        public void tryIt() {
           context.doSth();   // <-------- THIS DOES NOT WORK, this method can not be called
        }
    }

Please explain, is there something fundamental I have not understood. I am just passing the context into the ArrayAdapter instance I create. And from within this instance I would like to acccess the caller's method.

Why shoudl this not be possible?

Many thanks!

Upvotes: 1

Views: 2030

Answers (4)

abhinavrathore019
abhinavrathore019

Reputation: 6635

How I did it

StaticCommonDataClass -> maintains static data here I will keep the instance of Activity one in it.

ActivityOneClass -> Contains the method that I have to access in ActivityTwo actually.

ActivityTwoClass => Will access the ActivityOne Method.

What I hate is to pass two many parameters from one function to other function or one class to other class, that too when it has to be done for similar values again and again.

Here i will store refrence of ActivityOneClass in static Variable.

public class CommonStaticData {

    private static Activity _activity;
    private static Context _context;

    public static void setactivity(Activity activity) {
        _activity = activity;
    }

    public static Activity getactivity() {
        return _activity;
    }

    public static void setcontext(Context context) {
        _context = context;
    }

    public static Context getcontext() {
        return _context;
    }



}


public class ActivityOneClass extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);

        CommonStaticData.setactivity(ActivityOneClass.this); //will keep the instance alive for this activity
    }

    Public void activityOneMethod()
    {
        //Set of statements 
    }
}



public class ActivityTwoClass extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);

        ((ActivityOneClass) CommonStaticData.getactivity()).activityOneMethod();  

        //we need to typecast the instance stored in CommonStaticData.getactivity() to "ActivityOneClass" thats is the 
        //activity containing the method so as to access the method otherwise it will not come in the intellisense window and will generate Compiler Error
    }
}

Upvotes: 0

Adil Soomro
Adil Soomro

Reputation: 37729

Actually you are making Activity context object and passing a child of Activity (i.e Main_Screen), Its called upward cast (Implicit Casting).

So the Activity (as parent) has no method of doSth(). So you need downward Casting (Explicit casting) to make it a Main_Screen.

Two ways to do this.

make an Object of Main_Screen context instead of Activity context

or

cast it as Main_Screen in tryIt() method to avail Main_Screen methods like this way:

if(context.isInstance(Main_Screen.class))
{
    ((Main_Screen)context).doSth()
}

you can also use try catch to minimize the chances of ClassCastException

Upvotes: 1

Vladimir
Vladimir

Reputation: 3812

try this:

public void tryIt() {
           ((Main_screen)context).doSth();  
        }

context is Activity and it hasn't doSth(), but Main_screen has, so you should cast to this class

Upvotes: 6

Binoy Babu
Binoy Babu

Reputation: 17119

You can use the below code. Obviously class context don't contain an object doSth(). doSth() is declared in class Main_screen.

 public class My_ArrayAdapter extends ArrayAdapter<Object> {

        private final List<Object> list;
        private final Activity context;


        public My_ArrayAdapter(Activity context, List<Object> list) {
           this.context = context;
           this.list = list;

        }

        public void tryIt() {
           Main_screen.doSth();
        }
    }

Upvotes: 0

Related Questions