简体   繁体   English

如何将列表视图中我从 Firebase 实时数据库中调用的数据移动到 android 工作室中的另一个活动?

[英]How to move the data that I have called from the Firebase real-time database, in a listview to another activity in android studio?

So, I have a simple listview application.所以,我有一个简单的列表视图应用程序。 When I click an item, it will send the data to the next activity using Bundle.当我单击一个项目时,它将使用 Bundle 将数据发送到下一个活动。 In the first time, I use this JSON structure第一次用这个JSON结构

"teacher": [
    {
      "name": "Carl",
      "age": "40"
    },
    {
      "name": "Johnson",
      "age": "44"
    }
  ],

And this is my Java code:这是我的 Java 代码:

Query query = FirebaseDatabase.getInstance().getReference("teacher");
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot data : dataSnapshot.getChildren()) {
                TeacherHelperClass teacherHelperClass = new TeacherHelperClass();
                String name = data.child("name").getValue(String.class);
                String age = data.child("age").getValue(String.class);
                teacherHelperClass.setName(name);
                teacherHelperClass.setAge(age);
                arrayList.add(teacherHelperClass);
            }
            teacherAdapter.notifyDataSetChanged();
            listView.setAdapter(teacherAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
    
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

            Query _query = FirebaseDatabase.getInstance().getReference("teacher");
            _query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    String _name = dataSnapshot.child(String.valueOf(position)).child("name").getValue(String.class);
                    String _age = dataSnapshot.child(String.valueOf(position)).child("age").getValue(String.class);

                    Bundle bundle = new Bundle();
                    bundle.putString("dataName", _name);
                    bundle.putString("dataAge", _age);
                    TeacherDetailBottomSheet bottomSheetDialog = new TeacherDetailBottomSheet();
                    bottomSheetDialog.setArguments(bundle);
                    bottomSheetDialog.show(getSupportFragmentManager(), "exampleBottomSheet");

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

        }
    });

The code works fine.代码工作正常。 But now I want to change the database to keyed JSON:但现在我想将数据库更改为键控 JSON:

"teacher": {
    "001": {
      "name": "Carl",
      "age": "40"
    },
    "002": {
      "name": "Johnson",
      "age": "44"
    }
  }

And maybe add another child也许再加一个孩子

"teacher": {
    "001": {
      "name": "Carl",
      "age": "40",
      "email": "....",
      "phone-number": "...."
    },
    "002": {
      "name": "Johnson",
      "age": "44",
      "email": "....",
      "phone-number": "...."
    }
  }

What should I change in the Java code?我应该在 Java 代码中更改什么? especially in the setOnItemClick method.特别是在 setOnItemClick 方法中。 I want to show name & age child in the listview, and send all the data (name, age, email, phone) to the next activity.我想在列表视图中显示孩子的姓名和年龄,并将所有数据(姓名、年龄、email、电话)发送到下一个活动。 I've searched for the similar question, but haven't found a clear solution.我搜索了类似的问题,但没有找到明确的解决方案。

The code should be like:代码应该是这样的:

Teacher Model class Should be like below:老师 Model class 应该如下:

Public class TeacherModel{
    String key,name ,age;

 // CREATE empty constructor ,GETTER and SETTER FOR THESE VARIABLES
}

Now, Create a list of TeacherModel class现在,创建一个 TeacherModel class 列表

List<TeacherModel> teachersList = new ArrayList<>();


FirebaseDatabase.getInstance().getReference("teacher").
addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
           if(dataSnapshot.exists())
          {
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                  TeacherModel teacher =           
                              dataSnapshot.getValue(TeacherModel.class);
                            teacher.setKey(dataSnapshot.getKey());

                  
                    teachersList.add(teacher);
            }
            teacherAdapter.notifyDataSetChanged();
            listView.setAdapter(teacherAdapter);
          }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

And to send Data from one activity to another, you can use shared ViewModel or can make a public class and a variable of that data type and set the value to that variable and access it in another activity;并且要将数据从一个活动发送到另一个活动,您可以使用共享 ViewModel 或可以创建公共 class 和该数据类型的变量并将值设置为该变量并在另一个活动中访问它;

Public class Common{
  Public static List<TeacherModel> teacherList;
}

and in onClickListener you can set value to teacherList like Common.teacherList = teacherList;在 onClickListener 中,您可以将值设置为teacherList,例如Common.teacherList = teacherList;

Now in the another activity you can get that list using Common.teacherList;现在在另一个活动中,您可以使用 Common.teacherList 获取该列表;

Feel free to ask if something is unclear.随意询问是否有不清楚的地方。 And, kindly mark this as the correct answer if it helps you.如果对您有帮助,请将此标记为正确答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Firebase 上显示来自实时数据库的数据 - Displaying data from Real-time database on Firebase 适用于 Android 的 Firebase 实时(在线)数据库的安全性如何? - How Secure is Firebase Real-time (Online) database for Android? 创建另一个相同ID的节点后如何从firebase实时数据库中删除一个节点 - How to delete a node from the firebase real-time database after creating another node with same IDs 我无法在 recyclerView 上显示来自我的 firebase 实时数据库的数据 - I can't show data from my firebase real-time database on a recyclerView 从 Firebase 读取实时数据 - Read real-time data from Firebase 如何从android中的firebase实时数据库中获取数据 - How to fetch data from firebase real time database in android 如何从实时 Firebase 检索使用 Geofire 设置的多个位置,并在 Android Studio 的 map 上显示它们? - How to retrieve multiple location which set by using Geofire from Real-time Firebase and show them on map in Android Studio? 从Firebase实时数据库检索数据到ListView - Retrieving Data from Firebase real time Database to ListView Android firebase 实时数据库只显示上次提交 - Android firebase real-time database only shows last submit 插入数据到firebase实时数据库后看不到数据 - Can't see the data after I inserted the data to firebase real-time database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM