如何在 Android Studio 中使用 FirebaseUI 使用 Firebase 数据填充 RecyclerView
Firebase是当今使用的最流行的在线数据库之一,并且至少在未来几年内将保持不变。 Firebase 提供了一个实时数据库,可用于为所有用户实时存储和检索数据。在这篇文章中,让我们将实时数据库与 Android 应用程序连接起来,并使用FirebaseUI在 RecyclerView 中显示数据。 FirebaseUI 是一个适用于 Android 的开源库,可将常见的 UI 元素快速连接到 Firebase API。我们将在本文中介绍以下内容。
- 如何创建 Firebase 项目并手动添加一些数据以显示在应用程序中。
- 如何创建 Android 项目并在其中添加 Firebase 和 FirebaseUI 支持。
- 如何在 Android 应用中添加 RecyclerView 并在 Firebase 实时数据库中显示数据。
如何创建 Firebase 项目并手动添加数据以在应用中显示
- 第 1 步:创建 Firebase 项目
- 转到 https://console.firebase.google.com/u/0/
- 如果尚未登录,请使用您的 Google 帐户登录 Firebase。
- 点击创建项目。
- 第 2 步:为项目命名
- 写名字。
- 点击继续。
- 第 3 步:禁用 Google Analytics(此项目无需执行此操作)
- 单击切换按钮。
- 单击继续。
Firebase 将为您创建一个项目并为您打开它。
- 第 4 步:创建实时数据库:
- 转到侧边栏上的开发选项。
- 单击数据库。
- 在新屏幕中向下滚动,然后单击在实时数据库上创建数据库。
- 选择以测试模式启动(为了获得对数据库的读写访问权限)。
- 单击启用。
- 步骤5:使用数据库中的'+'符号以与图片中给出的相同方式将数据添加到数据库。
Note:
- All data values are stored in a string format for ease.
- Key-value can be any string but should not include spaces ” “.
- The same sub-keys should be present parent keys so that they can read by Recycler View without any error.
如何创建一个 android 项目并在其中添加 Firebase 和 FirebaseUI 支持
- 第 1 步:打开 Android Studio 并创建一个名为“ RecyclerView ”的新项目,其中包含一个空活动。
- 第 2 步:将您的 Firebase 项目与您的应用相关联。
- 第 3 步:在app/build.gradle文件中添加以下依赖项,以便在应用中获得 FirebaseUI 和 Firebase 实时数据库支持。
dependencies { // Dependency FirebaseUI for Firebase Realtime Database implementation 'com.firebaseui:firebase-ui-database:6.2.1' // Dependency for Firebase REaltime Database implementation 'com.google.firebase:firebase-database:19.3.1' }
如何在 android 应用中添加 RecyclerView 并在 Firebase 实时数据库中显示数据
- Step 1:添加以下依赖以获得应用中对Cardview的支持。
dependencies { // This dependency includes all material components of the android app. implementation 'com.google.android.material:material:1.1.0' }
- 第 2 步:首先,在activity_main.xml中添加 Recycler View 并将其命名为recycler1将给定的代码粘贴到 activity_main.xml 文件中以便这样做。
XML
XML
Java
// Your package name can be different depending // on your project name package com.example.recyclerview; public class person { // Variable to store data corresponding // to firstname keyword in database private String firstname; // Variable to store data corresponding // to lastname keyword in database private String lastname; // Variable to store data corresponding // to age keyword in database private String age; // Mandatory empty constructor // for use of FirebaseUI public person() {} // Getter and setter method public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
Java
package com.example.recyclerview; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.firebase.ui.database.FirebaseRecyclerAdapter; import com.firebase.ui.database.FirebaseRecyclerOptions; // FirebaseRecyclerAdapter is a class provided by // FirebaseUI. it provides functions to bind, adapt and show // database contents in a Recycler View public class personAdapter extends FirebaseRecyclerAdapter< person, personAdapter.personsViewholder> { public personAdapter( @NonNull FirebaseRecyclerOptions
options) { super(options); } // Function to bind the view in Card view(here // "person.xml") iwth data in // model class(here "person.class") @Override protected void onBindViewHolder(@NonNull personsViewholder holder, int position, @NonNull person model) { // Add firstname from model class (here // "person.class")to appropriate view in Card // view (here "person.xml") holder.firstname.setText(model.getFirstname()); // Add lastname from model class (here // "person.class")to appropriate view in Card // view (here "person.xml") holder.lastname.setText(model.getLastname()); // Add age from model class (here // "person.class")to appropriate view in Card // view (here "person.xml") holder.age.setText(model.getAge()); } // Function to tell the class about the Card view (here // "person.xml")in // which the data will be shown @NonNull @Override public personsViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.person, parent, false); return new personAdapter.personsViewholder(view); } // Sub Class to create references of the views in Crad // view (here "person.xml") class personsViewholder extends RecyclerView.ViewHolder { TextView firstname, lastname, age; public personsViewholder(@NonNull View itemView) { super(itemView); firstname = itemView.findViewById(R.id.firstname); lastname = itemView.findViewById(R.id.lastname); age = itemView.findViewById(R.id.age); } } } Java
package com.example.recyclerview; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import com.firebase.ui.database.FirebaseRecyclerOptions; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.Query; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; personAdapter adapter; // Create Object of the Adapter class DatabaseReference mbase; // Create object of the // Firebase Realtime Database @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create a instance of the database and get // its reference mbase = FirebaseDatabase.getInstance().getReference(); recyclerView = findViewById(R.id.recycler1); // To display the Recycler view linearly recyclerView.setLayoutManager( new LinearLayoutManager(this)); // It is a class provide by the FirebaseUI to make a // query in the database to fetch appropriate data FirebaseRecyclerOptions
options = new FirebaseRecyclerOptions.Builder () .setQuery(mbase, person.class) .build(); // Connecting object of required Adapter class to // the Adapter class itself adapter = new personAdapter(options); // Connecting Adapter class with the Recycler view*/ recyclerView.setAdapter(adapter); } // Function to tell the app to start getting // data from database on starting of the activity @Override protected void onStart() { super.onStart(); adapter.startListening(); } // Function to tell the app to stop getting // data from database on stopping of the activity @Override protected void onStop() { super.onStop(); adapter.stopListening(); } } - 第 3 步:现在,让我们在布局目录中创建另一个 XML 文件来存储来自特定人员数据库的数据。我们将文件命名为person.xml 。将以下代码复制到创建的文件中。
XML
- 第四步:在这之后,我们要创建一个Java文件,从数据库中获取和存储特定人的数据,并一一交给Recycler View。创造人。 Java在同一个文件夹中 主要活动。 Java文件存在。将以下代码粘贴到文件中。
Java
// Your package name can be different depending // on your project name package com.example.recyclerview; public class person { // Variable to store data corresponding // to firstname keyword in database private String firstname; // Variable to store data corresponding // to lastname keyword in database private String lastname; // Variable to store data corresponding // to age keyword in database private String age; // Mandatory empty constructor // for use of FirebaseUI public person() {} // Getter and setter method public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
- 第5步:为了显示来自人的数据。在Java中,我们必须创建一个 Adapter 类。创建另一个名为personAdapter 的Java文件。 Java在与MainActivity 相同的文件夹中。 Java并粘贴以下代码。
Java
package com.example.recyclerview; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.firebase.ui.database.FirebaseRecyclerAdapter; import com.firebase.ui.database.FirebaseRecyclerOptions; // FirebaseRecyclerAdapter is a class provided by // FirebaseUI. it provides functions to bind, adapt and show // database contents in a Recycler View public class personAdapter extends FirebaseRecyclerAdapter< person, personAdapter.personsViewholder> { public personAdapter( @NonNull FirebaseRecyclerOptions
options) { super(options); } // Function to bind the view in Card view(here // "person.xml") iwth data in // model class(here "person.class") @Override protected void onBindViewHolder(@NonNull personsViewholder holder, int position, @NonNull person model) { // Add firstname from model class (here // "person.class")to appropriate view in Card // view (here "person.xml") holder.firstname.setText(model.getFirstname()); // Add lastname from model class (here // "person.class")to appropriate view in Card // view (here "person.xml") holder.lastname.setText(model.getLastname()); // Add age from model class (here // "person.class")to appropriate view in Card // view (here "person.xml") holder.age.setText(model.getAge()); } // Function to tell the class about the Card view (here // "person.xml")in // which the data will be shown @NonNull @Override public personsViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.person, parent, false); return new personAdapter.personsViewholder(view); } // Sub Class to create references of the views in Crad // view (here "person.xml") class personsViewholder extends RecyclerView.ViewHolder { TextView firstname, lastname, age; public personsViewholder(@NonNull View itemView) { super(itemView); firstname = itemView.findViewById(R.id.firstname); lastname = itemView.findViewById(R.id.lastname); age = itemView.findViewById(R.id.age); } } } - 第 6 步:然后我们调用数据库并从中获取数据。这将在MainActivity 中完成。Java本身。
Java
package com.example.recyclerview; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import com.firebase.ui.database.FirebaseRecyclerOptions; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.Query; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; personAdapter adapter; // Create Object of the Adapter class DatabaseReference mbase; // Create object of the // Firebase Realtime Database @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create a instance of the database and get // its reference mbase = FirebaseDatabase.getInstance().getReference(); recyclerView = findViewById(R.id.recycler1); // To display the Recycler view linearly recyclerView.setLayoutManager( new LinearLayoutManager(this)); // It is a class provide by the FirebaseUI to make a // query in the database to fetch appropriate data FirebaseRecyclerOptions
options = new FirebaseRecyclerOptions.Builder () .setQuery(mbase, person.class) .build(); // Connecting object of required Adapter class to // the Adapter class itself adapter = new personAdapter(options); // Connecting Adapter class with the Recycler view*/ recyclerView.setAdapter(adapter); } // Function to tell the app to start getting // data from database on starting of the activity @Override protected void onStart() { super.onStart(); adapter.startListening(); } // Function to tell the app to stop getting // data from database on stopping of the activity @Override protected void onStop() { super.onStop(); adapter.stopListening(); } } - 第 7 步:运行项目之前 在AndroidManifest.xml中,需要包含以下权限,才能访问互联网:
“uses-permission android:name=”android.permission.INTERNET”
输出: