java - 在 Android 中使用 Fragment 的 SQLite 数据库

标签 java android database sqlite android-fragments

我正在制作一个包含多个 fragment 的应用。其中一个 fragment 显示了数据库中的数据。该 fragment 从 MainActivity 调用并重定向到外部类“fragment_database”。

但是,每当我尝试在此外部类“Fragment_Database.java”中添加函数时,都会生成范围错误或缺少函数错误。所有功能都要求该类扩展 Activity 类,但我扩展了 Fragment 类......

这是我的外部 fragment_database 类:

public class Fragment_Database extends Fragment{
public Fragment_Database() {

    // HERE??

}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_database, container, false);

    // HERE??

    return v;
}

我应该将数据库函数放在此类中还是放在 MainActivity 中?我对项目的结构方面有点迷茫......

谢谢

最佳答案

如果您需要添加任何需要 Activity 的内容,您可以使用 fragment 中的 getActivity() 方法获取对它的引用。

最好放入onActivityCreated()方法是这样的

Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle).

关于java - 在 Android 中使用 Fragment 的 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22647861/

相关文章:

PHP:如何从mysql检索数据并插入到特定用户

android - 使用 Google 登录时 SignInConfiguration 的 ClassNotFoundException

java - 为什么 Java 对 Set 和 ArrayList 实现不同的 hashcode 方法?

java - Chrome 开发入门

java - 在 Windows 命令行参数中使用空格

带有外部模拟 ContentProvider 的 androidTest

android - 将 JSON 数据从一个 Fragment 传递到另一个 Fragment 的最佳方式是什么

java - EJB3 Mappings/onetomany/manytoone 如何处理关系

mysql - 创建事件在 MySQL 中出现语法错误

java - 内部类异常