-1

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?

  • Try [this answer](https://stackoverflow.com/a/51344957/9473786) – Tyler V May 21 '20 at 13:24
  • 1
    Does this answer your question? [How to Keep ArrayList Value When Changing Activity in Android](https://stackoverflow.com/questions/51344683/how-to-keep-arraylist-value-when-changing-activity-in-android) – Tyler V May 21 '20 at 13:24

2 Answers2

0

Open a new database connection in the other activity.

Variables and objects cannot be shared between activities unless they are static (and compatible with that use case: objects that need an Activity Context will cause problems and leaks if made static), or Serializable and you provide it as a parameter.

You can also consider Fragments: that will allow to create the mDatabase connection in your containing Activity and reuse it from your Fragments, or place the Database connection at the Application Level, if the database connection can be initialized with an Application context.

rupps
  • 9,712
  • 4
  • 55
  • 95
0
  1. Create your own Application class.
    public class TestApplication extends Application { 

            public static SQLDatabase dbHelper; 
            public static SQLiteDatabase mDatabase;

            @Override
            public void onCreate() {
                super.onCreate();
                initDb();
            }

            public void initDb() {
                dbHelper = new SQLDatabase(this);
            mDatabase = dbHelper.getWritableDatabase();
            mDatabase.insert(....);
            }

            public static SQLiteDatabase getDb()  {
                return mDatabase;
            }
        }
  1. Add you Application class to AndroidManifest under Application tag

    <application android:name="TestApplication"

  2. Use it from your activities:

    TestApplication.getDb()

If it helped please mark as answered :)

Abdullah Bahattab
  • 612
  • 1
  • 16
  • 32
Sebastian M
  • 629
  • 7
  • 17
  • 1
    Thanks for your answer. I will try this solution and will let you know, if it helped me. – codingguy May 21 '20 at 14:12
  • I got a question to number 3. Dont you think, that you have to create an object from the class Testapplication first? Your trying to use a method directly from a class. – codingguy May 21 '20 at 22:25
  • Thanks man, I figured it out with your help. I appreciate it. Because its a static method I dont have to create an object from the class. With this solution I am able to build my app in seperate activities. – codingguy May 22 '20 at 00:54
  • Glad to here that :) – Sebastian M May 22 '20 at 10:40