简体   繁体   中英

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. In the first time, I use this JSON structure

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

And this is my Java code:

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:

"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? especially in the setOnItemClick method. I want to show name & age child in the listview, and send all the data (name, age, email, phone) to the next activity. 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:

Public class TeacherModel{
    String key,name ,age;

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

Now, Create a list of 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;

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

and in onClickListener you can set value to teacherList like Common.teacherList = teacherList;

Now in the another activity you can get that list using Common.teacherList;

Feel free to ask if something is unclear. And, kindly mark this as the correct answer if it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM