Categories
Mastering Development

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.

Leave a Reply

Your email address will not be published. Required fields are marked *