简体   繁体   中英

Firebase addChildEventListener doesn't work in ListView android studio

I have a simple listview app and 1 data inside it

"1614156499206": {
  "title": "This is title",
  "description": "This is description",
  "date": "24 February 2021",
  "timestamp": "1614156499206"
}

And this is the Java code

...

TextAdapter textAdapter = new TextAdapter(getContext(), arrayList);
ArrayList<TextHelperClass> arrayList = new ArrayList<>();
TextListView.setAdapter(TextAdapter);

FirebaseDatabase.getInstance().getReference("text").addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        for (DataSnapshot timestamp : dataSnapshot.getChildren()) {
            TextHelperClass textHelperClass = new TextHelperClass();
            String title = timestamp.child("title").getValue(String.class);
            String date = timestamp.child("date").getValue(String.class);
            String description = timestamp.child("description").getValue(String.class);
            String times = timestamp.child("timestamp").getValue(String.class);

            textHelperClass.setTitle(title);
            textHelperClass.setDate(date);
            textHelperClass.setDescription(description);
            textHelperClass.setTimestamp(times);
            arrayList.add(textHelperClass);
        }
        textAdapter.notifyDataSetChanged();
    }

    @Override
    public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        textAdapter.notifyDataSetChanged();
    }

    @Override
    public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

    }

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

    }
});

There's no error in the code, but when I run it, this is the output:

输出

And when I change addChildEventListener to addValueEventListener, it works fine. What is the problem in my code when I use addChildEventListener? and how to refresh the listview everytime I delete the child?

textListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        new AlertDialog.Builder(getActivity(), R.style.AlertDialog)
                .setMessage("Are you sure want to delete this text?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String time = arrayList.get(position).getTimestamp();
                        FirebaseDatabase.getInstance().getReference("text").child(time).removeValue();
                    }
                })
                .setNegativeButton("No", null)
                .show();
        return true;
    }
});

When you add a ValueEventListener to a path, its onDataChange gets called with a snapshot of all data at that path at once. So if there are multiple child nodes under that path, you need to loop over snapshot.getChildren() to get to the each child node.

When add a ChildEventListener to the same path, its onChildAdded is called once for each individual child node. So you no longer need to loop over snapshot.getChildren() .

So my guess is that you need to get rid of the loop on your onChildAdded :

FirebaseDatabase.getInstance().getReference("text").addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot timestamp, @Nullable String s) {
        TextHelperClass textHelperClass = new TextHelperClass();
        String title = timestamp.child("title").getValue(String.class);
        ...
        textAdapter.notifyDataSetChanged();
    }

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