如何使用Firebase Firestore实时仅从集合中提取新创建的文档?

时间:2020-03-30 08:30:49

标签: android database firebase google-cloud-firestore

因此,我正在尝试为我的课堂项目创建一个应用程序,它实际上只是一个住房应用程序,它显示带有属性列表的摘要。我的问题是onCreate我用集合中的所有属性填充数组,并添加了快照侦听器。但是,当我开始第二个活动以向数据库添加新属性时,返回mainActivity提要时,snapshotListener不会对添加到集合中的新属性文档做出反应,也不会将其添加到数组中。那么,有什么方法可以使当集合添加新文档时,snapshotListener做出反应?我只希望它添加在快照之间添加的新文档,但是也许我不知道如何调用新快照?这是供参考的代码。

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

private FirebaseFirestore db;
private CollectionReference properties;
private CardViewAdapter adapter;

List<Property> cards;
SwipeFlingAdapterView flingAdapterView;

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

    Log.d(TAG, "onCreate: started");

    initFirestore();

    cards = new ArrayList<>();
    adapter = new CardViewAdapter(this, R.layout.property_card, cards);

    flingAdapterView = findViewById(R.id.frame);
    flingAdapterView.setAdapter(adapter);
    flingAdapterView.setFlingListener(new SwipeFlingAdapterView.onFlingListener()
    {
        Property property;

        @Override
        public void removeFirstObjectInAdapter()
        {
            property = cards.remove(0);
            cards.add(property);
            adapter.notifyDataSetChanged();
        }
    });

    flingAdapterView.setOnItemClickListener
            (new SwipeFlingAdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClicked(int i, Object o)
                {
                    Intent intent = new Intent(MainActivity.this, ViewContactInfoActivity.class);
                    intent.putExtra(Property.PARCELABLE_PROPERTY, cards.get(0));
                    startActivity(intent);
                }
            });

    properties.addSnapshotListener(this, new EventListener<QuerySnapshot>()
    {
        @Override
        public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e)
        {
            if (e != null)
            {
                return;
            }

            for (DocumentChange dc : queryDocumentSnapshots.getDocumentChanges())
            {
                switch (dc.getType())
                {
                    case ADDED:
                        Log.d(TAG, "New property: " + dc.getDocument().getData());
                        cards.add(dc.getDocument().toObject(Property.class));
                        break;
                    case MODIFIED:
                        Log.d(TAG, "Modified property: " + dc.getDocument().getData());
                        cards.add(dc.getDocument().toObject(Property.class));
                        break;
                    case REMOVED:
                        Log.d(TAG, "Removed property: " + dc.getDocument().getData());
                        break;
                }
            }

            adapter.notifyDataSetChanged();
        }
    });
}

//Initialize FireStore
private void initFirestore()
{
    db = FirebaseFirestore.getInstance();
    properties = db.collection("properties");
}

//Add New Property to DB
public void createListing(View view)
{
    Intent intent = new Intent(MainActivity.this, CreateListingActivity.class);
    startActivity(intent);
}

}

0 个答案:

没有答案