java—如何使用valueeventlistener仅在添加新子级时生成tost,而不为现有子级生成tost?有什么想法吗?

w9apscun  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(281)

因此,我正在用firebase创建一个android应用程序,其中我使用valueeventlistener()来显示所有子级的列表。但是我还想在添加新的子对象时生成一个toast(稍后我将把它转换为通知)。为此,我使用了一个childeventlistener(),但这里的问题是,当我启动应用程序时,它还会为所有已经存在的子级生成toast(我不希望这样)。我决定使用childeventlistener(),因为它的onchildadded()方法只提供有关新子级的信息。有人能提出任何想法/解决方案吗?我想在没有firebase函数的情况下实现它。这是活动。

package com.example.draft_app;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class FaceLogActivity extends AppCompatActivity {

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_log);

        listView = findViewById(R.id.listView);

        ArrayList<String> list = new ArrayList<>();
        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_item, list);
        listView.setAdapter(adapter);

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("App").child("Faces");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list.clear();
                for(DataSnapshot snapshot: dataSnapshot.getChildren()){
                    Faces face = snapshot.getValue(Faces.class);
                    String txt = face.getIdentity() + " : " + face.getTime();
                    list.add(txt);
                }
                adapter.notifyDataSetChanged();
            }

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

            }
        });

        reference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                Faces face = snapshot.getValue(Faces.class);
                String txt = face.getIdentity() + " : " + face.getTime();
                Toast.makeText(FaceLogActivity.this, txt, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

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

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

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

            }
        });

    }
}
vhipe2zx

vhipe2zx1#

如果你两个都听 child_ 以及 value 同一节点上的事件 value 事件总是最后发生。您可以使用这些知识来构建您的用例,因为您可以使用 onDataChange 设置一个标志,然后签入 onChildAdded .
比如:

// add a field to the class
bool isInitialDataReceived = false

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("App").child("Faces");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            list.clear();
            for(DataSnapshot snapshot: dataSnapshot.getChildren()){
                Faces face = snapshot.getValue(Faces.class);
                String txt = face.getIdentity() + " : " + face.getTime();
                list.add(txt);
            }
            adapter.notifyDataSetChanged();
            isInitialDataReceived = true;
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
           throw error.toException(); // never ignore errors
        }
    });

    reference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
            if (isInitialDataReceived) {
                Faces face = snapshot.getValue(Faces.class);
                String txt = face.getIdentity() + " : " + face.getTime();
                Toast.makeText(FaceLogActivity.this, txt, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { }
        @Override
        public void onChildRemoved(@NonNull DataSnapshot snapshot) { }
        @Override
        public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
           throw error.toException(); // never ignore errors
        }
    });

}

相关问题