0

So I have one java class called Details, which contains a non-static method called loadFavourites() and another java class called Favourites, which is implemented when the Favourites activity is started in my app.

I want to be able to call the method loadFavourites() from Details class in Favourites onCreate() method, however I am not sure how to do this...

I understand that I cannot create an instance of the Details class in order to access the method e.g.

Details details = new Details();
details.loadFavourites();

...as this does not work. Also, I do not want my class or my method to be static as I have dynamic data involved.

Therefore, does anyone know a way in which I can call this non-static method from another class?

Here is a simplified version of my code...

In Details Activity:

FloatingActionButton addFav = (FloatingActionButton) findViewById(R.id.addFavBtn); 
addFav.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 
    loadFavourites(); 
}});

public void loadFavourites() {
SQLiteDatabase db = dbHelper.getReadableDatabase();

    String[] columns = {"favouriteId", "title", "owner", "url_m", "ownerPic", "description", "dateTaken"};
    Cursor cursor = db.query("favourites", columns, null, null, null, null, "favouriteId");

    Log.d("FavouritesDB", "" + cursor.getCount());

    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {

        favID = cursor.getInt(0);
        title = cursor.getString(1);
        owner = cursor.getString(2);
        url_m = cursor.getString(3);
        ownerPic = cursor.getString(4);
        description = cursor.getString(5);
        date = cursor.getString(6);

        // Add data to the array list for the recyclerview
        ImageInfo favourite = new ImageInfo();

        favourite.dateTaken = date;
        favourite.description = description;
        favourite.ownerPic = ownerPic;
        favourite.url_m = url_m;
        favourite.owner = owner;
        favourite.title = title;
        NetworkMgr.getInstance(this).favouritesImageList.add(favourite);

        // Move to next entry
        cursor.moveToNext();
    }

    cursor.close();
    db.close();
}

In Favourites Activity:

public class Favourites extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.favourites_layout);

    *this is where I want loadFavourites()*
    }
}
S.Claire
  • 3
  • 5
  • Are `Details` and `Favorites` activities? – Henry Dec 20 '17 at 01:24
  • Check this https://stackoverflow.com/questions/19666572/how-to-call-a-method-in-another-activity-from-activity I suggest the first comment, read this https://developer.android.com/guide/components/fundamentals.html – AndroidRuntimeException Dec 20 '17 at 01:27
  • Yes they are...The loadFavourites() method is called in Details activity when a button in that activity is clicked, however I also want the method to be called in Favourites activity too. – S.Claire Dec 20 '17 at 01:28
  • Please add your code. Read [ask]. – Jorgesys Dec 20 '17 at 01:31
  • Possible duplicate of [how to call a method in another Activity from Activity](https://stackoverflow.com/questions/19666572/how-to-call-a-method-in-another-activity-from-activity) – Henry Dec 20 '17 at 01:48
  • Are the two Activities related in any way? What does loadFavourites() contain? – n_r Dec 20 '17 at 08:59
  • loadFavourites() adds the data to the database, which is then displayed in Favourite's recyclerview – S.Claire Dec 20 '17 at 12:13

0 Answers0