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()*
}
}