Im struggling with the following problem:
I have to classes, MainClass and SecondClass.
In the MainClass I create a database (mDatabase) and I am inserting data to the database.
public class MainActivity extends AppCompatActivity{
public SQLDatabase dbHelper; //created class to initialize my database
public SQLiteDatabase mDatabase;
public void onCreate(Bundle savedInstanceState){
dbHelper = new SQLDatabase(this);
mDatabase = dbHelper.getWritableDatabase();
mDatabase.insert(....);
}
Now I want to display the data from the database (mDatabase) in a different Activity. So, I have to use the variable mDatabase from another class, because I am using a recyclerView in a different Activity, this is why:
public class SecondActivity{
public void onCreate(){
recyclerView=findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new AdapterClass(this, getAllItems());
recyclerView.setAdapter(mAdapter);
}
public Cursor getAllItems(){
return mDatabase.query(....);
}
}
I want to have one Activity where I type in data for the database, and I want to have a second Activity where I can display the inserted data.
Do you know how I can use the mDatabase from two classes?