[英]I want so use a Database from different classes, how can I do that in JAVA? (AndroidStudio/Java)
Im struggling with the following problem:我正在努力解决以下问题:
I have to classes, MainClass and SecondClass.我要上课,MainClass 和SecondClass。
In the MainClass I create a database (mDatabase) and I am inserting data to the database.在 MainClass 中,我创建了一个数据库 (mDatabase),并将数据插入到数据库中。
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.现在我想在不同的 Activity 中显示来自数据库(mDatabase)的数据。 So, I have to use the variable mDatabase from another class, because I am using a recyclerView in a different Activity, this is why:所以,我必须使用来自另一个 class 的变量 mDatabase,因为我在不同的 Activity 中使用了 recyclerView,这就是为什么:
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.我想要一个 Activity 来为数据库输入数据,我想要第二个 Activity 来显示插入的数据。
Do you know how I can use the mDatabase from two classes?你知道我如何使用两个类的 mDatabase 吗?
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.变量和对象不能在活动之间共享,除非它们是static
(并且与该用例兼容:需要活动上下文的对象如果设为静态将导致问题和泄漏),或者可Serializable
并且您将其作为参数提供。
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.您还可以考虑Fragments :这将允许在您的包含Activity
中创建mDatabase
连接并从您的Fragments
重用它,或者如果数据库连接可以使用应用程序上下文初始化,则将数据库连接放在应用程序级别。
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;
}
}
Add you Application class to AndroidManifest under Application tag将您的应用程序 class 添加到应用程序标签下的 AndroidManifest
<application android:name="TestApplication"
Use it from your activities:在您的活动中使用它:
TestApplication.getDb()
If it helped please mark as answered:)如果有帮助,请标记为已回答:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.