Skip to content Skip to sidebar Skip to footer

How To Access Public Variables Declared In An Activity Class From Another Class In Android

I have some public variables define in my activity class: public class RentListingFeed extends ActionBarActivity { private ParseQueryAdapter mainAdapter;

Solution 1:

Declare them as static

publicstaticString typeQuery;

Now you can use them in your other class

RentListingFeed.typeQuery

Solution 2:

You should not declare class variables as public. The right way to do is, is to make them private and get / set them via their respective methods. For the type it should be like this:

publicvoidsetTypeQuery(String typeQuery) {
    this.typeQuery = typeQuery;
}

publicStringgetTypeQuery() {
    returnthis.typeQuery;
}

Now you can create an object of your class in your CustomAdapter and set / get these variables as you like:

RentListingFeedrentListingFeed=newRentListingFeed();
rentListingFeed.getTypeQuery();

Post a Comment for "How To Access Public Variables Declared In An Activity Class From Another Class In Android"